Save reader progress on every page change
Fixes #9668 Could probably refactor this a bit more, but the reader view model stuff is a mess in general anyway. (cherry picked from commit 6fe5e6e21bfaef2f03ecca3659e03835fc354602) # Conflicts: # app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderViewModel.kt
This commit is contained in:
parent
d67da8fd6f
commit
7911e39fef
@ -296,20 +296,6 @@ class ReaderActivity : BaseActivity() {
|
||||
readingModeToast?.cancel()
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when the activity is saving instance state. Current progress is persisted if this
|
||||
* activity isn't changing configurations.
|
||||
*/
|
||||
override fun onSaveInstanceState(outState: Bundle) {
|
||||
viewModel.onSaveInstanceState()
|
||||
super.onSaveInstanceState(outState)
|
||||
}
|
||||
|
||||
override fun onPause() {
|
||||
viewModel.saveCurrentChapterReadingProgress()
|
||||
super.onPause()
|
||||
}
|
||||
|
||||
/**
|
||||
* Set menu visibility again on activity resume to apply immersive mode again if needed.
|
||||
* Helps with rotations.
|
||||
|
@ -283,7 +283,6 @@ class ReaderViewModel(
|
||||
val currentChapters = state.value.viewerChapters
|
||||
if (currentChapters != null) {
|
||||
currentChapters.unref()
|
||||
saveReadingProgress(currentChapters.currChapter)
|
||||
chapterToDownload?.let {
|
||||
downloadManager.addDownloadsToStartOfQueue(listOf(it))
|
||||
}
|
||||
@ -298,17 +297,6 @@ class ReaderViewModel(
|
||||
deletePendingChapters()
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when the activity is saved. It updates the database
|
||||
* to persist the current progress of the active chapter.
|
||||
*/
|
||||
fun onSaveInstanceState() {
|
||||
val currentChapter = getCurrentChapter() ?: return
|
||||
viewModelScope.launchNonCancellable {
|
||||
saveChapterProgress(currentChapter)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether this presenter is initialized yet.
|
||||
*/
|
||||
@ -447,7 +435,6 @@ class ReaderViewModel(
|
||||
*/
|
||||
private suspend fun loadAdjacent(chapter: ReaderChapter) {
|
||||
val loader = loader ?: return
|
||||
saveCurrentChapterReadingProgress()
|
||||
|
||||
logcat { "Loading adjacent ${chapter.chapter.url}" }
|
||||
|
||||
@ -521,16 +508,17 @@ class ReaderViewModel(
|
||||
* [page]'s chapter is different from the currently active.
|
||||
*/
|
||||
fun onPageSelected(page: ReaderPage, hasExtraPage: Boolean, currentPage: String) {
|
||||
val currentChapters = state.value.viewerChapters ?: return
|
||||
|
||||
val selectedChapter = page.chapter
|
||||
|
||||
// InsertPage and StencilPage doesn't change page progress
|
||||
if (page is InsertPage || page is StencilPage) {
|
||||
return
|
||||
}
|
||||
|
||||
val currentChapters = state.value.viewerChapters ?: return
|
||||
val pages = page.chapter.pages ?: return
|
||||
val selectedChapter = page.chapter
|
||||
|
||||
// Save last page read and mark as read if needed
|
||||
saveReadingProgress()
|
||||
mutableState.update {
|
||||
it.copy(
|
||||
currentPage = page.number,
|
||||
@ -563,11 +551,9 @@ class ReaderViewModel(
|
||||
|
||||
if (selectedChapter != currentChapters.currChapter) {
|
||||
logcat { "Setting ${selectedChapter.chapter.url} as active" }
|
||||
saveReadingProgress(currentChapters.currChapter)
|
||||
setReadStartTime()
|
||||
viewModelScope.launch { loadNewChapter(selectedChapter) }
|
||||
}
|
||||
val pages = page.chapter.pages ?: return
|
||||
val inDownloadRange = page.number.toDouble() / pages.size > 0.25
|
||||
if (inDownloadRange) {
|
||||
downloadNextChapters()
|
||||
@ -639,17 +625,15 @@ class ReaderViewModel(
|
||||
}
|
||||
}
|
||||
|
||||
fun saveCurrentChapterReadingProgress() {
|
||||
getCurrentChapter()?.let { saveReadingProgress(it) }
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when reader chapter is changed in reader or when activity is paused.
|
||||
*/
|
||||
private fun saveReadingProgress(readerChapter: ReaderChapter) {
|
||||
private fun saveReadingProgress() {
|
||||
getCurrentChapter()?.let {
|
||||
viewModelScope.launchNonCancellable {
|
||||
saveChapterProgress(readerChapter)
|
||||
saveChapterHistory(readerChapter)
|
||||
saveChapterProgress(it)
|
||||
saveChapterHistory(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -664,7 +648,7 @@ class ReaderViewModel(
|
||||
if (incognitoMode) return
|
||||
|
||||
val chapter = readerChapter.chapter
|
||||
getCurrentChapter()?.requestedPage = chapter.last_page_read
|
||||
readerChapter.requestedPage = chapter.last_page_read
|
||||
updateChapter.await(
|
||||
ChapterUpdate(
|
||||
id = chapter.id!!,
|
||||
|
Loading…
x
Reference in New Issue
Block a user