Skip to content

Commit

Permalink
added view toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
LagradOst committed Feb 5, 2024
1 parent 18c3c05 commit 857da5e
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 24 deletions.
1 change: 1 addition & 0 deletions app/src/main/java/com/lagradost/quicknovel/DataStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const val EPUB_READER_TYPE: String = "reader_reader_type"
const val EPUB_CURRENT_POSITION: String = "reader_epub_position"
const val EPUB_CURRENT_POSITION_SCROLL: String = "reader_epub_position_scroll"
const val EPUB_CURRENT_POSITION_SCROLL_CHAR: String = "reader_epub_position_scroll_char"
const val EPUB_CURRENT_POSITION_READ_AT: String = "reader_epub_position_read"
const val RESULT_BOOKMARK: String = "result_bookmarked"
const val RESULT_BOOKMARK_STATE: String = "result_bookmarked_state"
const val HISTORY_FOLDER: String = "result_history"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,12 @@ class ReadActivityViewModel : ViewModel() {
desiredIndex = scrollIndex
currentIndex = scrollIndex.index

setKey(
EPUB_CURRENT_POSITION_READ_AT,
"${book.title()}/${scrollIndex.index}",
System.currentTimeMillis()
)

setKey(
EPUB_CURRENT_POSITION_SCROLL_CHAR,
book.title(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,15 @@
package com.lagradost.quicknovel.ui.result

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.core.view.isGone
import androidx.core.view.isVisible
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.lagradost.quicknovel.ChapterData
import com.lagradost.quicknovel.MainActivity
import com.lagradost.quicknovel.MainActivity.Companion.loadResult
import com.lagradost.quicknovel.SearchResponse
import com.lagradost.quicknovel.databinding.LoadingBottomBinding
import com.lagradost.quicknovel.databinding.SearchResultGridBinding
import com.lagradost.quicknovel.databinding.SimpleChapterBinding
import com.lagradost.quicknovel.util.UIHelper.setImage
import com.lagradost.quicknovel.util.toPx
import com.lagradost.quicknovel.widget.AutofitRecyclerView
import kotlin.math.roundToInt
class ChapterAdapter(val viewmodel : ResultViewModel) : ListAdapter<ChapterData, RecyclerView.ViewHolder>(DiffCallback()) {

class ChapterAdapter(val viewModel : ResultViewModel) : ListAdapter<ChapterData, RecyclerView.ViewHolder>(DiffCallback()) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return ChapterAdapterHolder(SimpleChapterBinding.inflate(LayoutInflater.from(parent.context),parent,false))
}
Expand All @@ -29,20 +18,32 @@ class ChapterAdapter(val viewmodel : ResultViewModel) : ListAdapter<ChapterData,
when(holder) {
is ChapterAdapterHolder -> {
val currentItem = getItem(position)
holder.bind(currentItem, viewmodel)
holder.bind(currentItem, viewModel)
}
}
}

class ChapterAdapterHolder(private val binding : SimpleChapterBinding) : RecyclerView.ViewHolder(binding.root) {
fun bind(card : ChapterData, viewmodel : ResultViewModel) {
private fun refresh(card : ChapterData, viewModel : ResultViewModel) {
binding.apply {
root.alpha = if (viewModel.hasReadChapter(chapter = card)) 0.5F else 1.0F
}
}
fun bind(card : ChapterData, viewModel : ResultViewModel) {
binding.apply {
name.text = card.name
releaseDate.text = card.dateOfRelease
releaseDate.isGone = card.dateOfRelease.isNullOrBlank()
root.setOnClickListener {
viewmodel.streamRead(card)
viewModel.streamRead(card)
refresh(card,viewModel)
}
root.setOnLongClickListener {
viewModel.setReadChapter(chapter = card, !viewModel.hasReadChapter(card))
refresh(card,viewModel)
return@setOnLongClickListener true
}
refresh(card,viewModel)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.lagradost.quicknovel.APIRepository
import com.lagradost.quicknovel.BaseApplication.Companion.getKey
import com.lagradost.quicknovel.BaseApplication.Companion.removeKey
import com.lagradost.quicknovel.BaseApplication.Companion.setKey
import com.lagradost.quicknovel.BookDownloader2
import com.lagradost.quicknovel.BookDownloader2Helper.generateId
Expand All @@ -21,6 +22,7 @@ import com.lagradost.quicknovel.DownloadActionType
import com.lagradost.quicknovel.DownloadProgressState
import com.lagradost.quicknovel.DownloadState
import com.lagradost.quicknovel.EPUB_CURRENT_POSITION
import com.lagradost.quicknovel.EPUB_CURRENT_POSITION_READ_AT
import com.lagradost.quicknovel.EPUB_CURRENT_POSITION_SCROLL
import com.lagradost.quicknovel.EPUB_CURRENT_POSITION_SCROLL_CHAR
import com.lagradost.quicknovel.HISTORY_FOLDER
Expand All @@ -47,6 +49,37 @@ class ResultViewModel : ViewModel() {
loadResponse.postValue(null)
}

fun hasReadChapter(chapter: ChapterData): Boolean {
val streamResponse =
(load as? StreamResponse) ?: return false
val index = chapterIndex(chapter) ?: return false
return getKey<Long>(
EPUB_CURRENT_POSITION_READ_AT,
"${streamResponse.name}/$index"
) != null
}

fun setReadChapter(chapter: ChapterData, value: Boolean): Boolean {
val streamResponse =
(load as? StreamResponse) ?: return false
val index = chapterIndex(chapter) ?: return false

if (value) {
setKey(
EPUB_CURRENT_POSITION_READ_AT,
"${streamResponse.name}/$index",
System.currentTimeMillis()
)
} else {
removeKey(
EPUB_CURRENT_POSITION_READ_AT,
"${streamResponse.name}/$index",
)
}

return true
}

lateinit var repo: APIRepository

var isGetLoaded = false
Expand Down Expand Up @@ -78,7 +111,7 @@ class ResultViewModel : ViewModel() {
val reviews: MutableLiveData<Resource<ArrayList<UserReview>>> by lazy {
MutableLiveData<Resource<ArrayList<UserReview>>>()
}
var currentReviews: ArrayList<UserReview> = arrayListOf()
private var currentReviews: ArrayList<UserReview> = arrayListOf()

private val reviewPage: MutableLiveData<Int> by lazy {
MutableLiveData<Int>(0)
Expand Down Expand Up @@ -117,7 +150,7 @@ class ResultViewModel : ViewModel() {
}
}

fun switchTab(index: Int?, position : Int? ) {
fun switchTab(index: Int?, position: Int?) {
val newPos = index ?: return
currentTabPosition.postValue(position ?: return)
currentTabIndex.postValue(newPos)
Expand All @@ -140,19 +173,41 @@ class ResultViewModel : ViewModel() {
}
}

fun streamRead(chapter : ChapterData? = null) = ioSafe {
private var cachedChapters: HashMap<ChapterData, Int> = hashMapOf()

private fun chapterIndex(chapter: ChapterData): Int? {
return cachedChapters[chapter]
}

private fun reCacheChapters() {
val streamResponse = (load as? StreamResponse)
if (streamResponse == null) {
cachedChapters = hashMapOf()
return
}
val out = hashMapOf<ChapterData, Int>()
streamResponse.data.mapIndexed { index, chapterData ->
out[chapterData] = index
}
cachedChapters = out
}

fun streamRead(chapter: ChapterData? = null) = ioSafe {
loadMutex.withLock {
if (!hasLoaded) return@ioSafe
addToHistory()

chapter?.let {
// TODO BETTER STORE
val streamResponse = ((loadResponse.value as? Resource.Success)?.value as? StreamResponse)
val index = streamResponse?.data?.indexOf(chapter)
if (index!= null && index >= 0) {
val streamResponse =
((loadResponse.value as? Resource.Success)?.value as? StreamResponse)
?: return@let
val index = chapterIndex(chapter)
if (index != null && index >= 0) {
setReadChapter(chapter, true)
setKey(EPUB_CURRENT_POSITION, streamResponse.name, index)
setKey(
EPUB_CURRENT_POSITION_SCROLL_CHAR, streamResponse.name,0,
EPUB_CURRENT_POSITION_SCROLL_CHAR, streamResponse.name, 0,
)
}
}
Expand Down Expand Up @@ -416,7 +471,7 @@ class ResultViewModel : ViewModel() {
setKey(
DOWNLOAD_EPUB_LAST_ACCESS, tid.toString(), System.currentTimeMillis()
)

reCacheChapters()
updateBookmarkData()

hasLoaded = true
Expand Down

0 comments on commit 857da5e

Please sign in to comment.