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.
This commit is contained in:
arkon 2023-07-05 18:57:57 -04:00
parent 8c5496b53f
commit 6fe5e6e21b
2 changed files with 12 additions and 42 deletions

View File

@ -235,20 +235,6 @@ class ReaderActivity : BaseActivity() {
readingModeToast?.cancel() 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. * Set menu visibility again on activity resume to apply immersive mode again if needed.
* Helps with rotations. * Helps with rotations.

View File

@ -223,7 +223,6 @@ class ReaderViewModel(
val currentChapters = state.value.viewerChapters val currentChapters = state.value.viewerChapters
if (currentChapters != null) { if (currentChapters != null) {
currentChapters.unref() currentChapters.unref()
saveReadingProgress(currentChapters.currChapter)
chapterToDownload?.let { chapterToDownload?.let {
downloadManager.addDownloadsToStartOfQueue(listOf(it)) downloadManager.addDownloadsToStartOfQueue(listOf(it))
} }
@ -238,17 +237,6 @@ class ReaderViewModel(
deletePendingChapters() 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. * Whether this presenter is initialized yet.
*/ */
@ -346,7 +334,6 @@ class ReaderViewModel(
*/ */
private suspend fun loadAdjacent(chapter: ReaderChapter) { private suspend fun loadAdjacent(chapter: ReaderChapter) {
val loader = loader ?: return val loader = loader ?: return
saveCurrentChapterReadingProgress()
logcat { "Loading adjacent ${chapter.chapter.url}" } logcat { "Loading adjacent ${chapter.chapter.url}" }
@ -420,16 +407,17 @@ class ReaderViewModel(
* [page]'s chapter is different from the currently active. * [page]'s chapter is different from the currently active.
*/ */
fun onPageSelected(page: ReaderPage) { fun onPageSelected(page: ReaderPage) {
val currentChapters = state.value.viewerChapters ?: return
val selectedChapter = page.chapter
// InsertPage and StencilPage doesn't change page progress // InsertPage and StencilPage doesn't change page progress
if (page is InsertPage || page is StencilPage) { if (page is InsertPage || page is StencilPage) {
return 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 // Save last page read and mark as read if needed
saveReadingProgress()
mutableState.update { mutableState.update {
it.copy( it.copy(
currentPage = page.index + 1, currentPage = page.index + 1,
@ -446,11 +434,9 @@ class ReaderViewModel(
if (selectedChapter != currentChapters.currChapter) { if (selectedChapter != currentChapters.currChapter) {
logcat { "Setting ${selectedChapter.chapter.url} as active" } logcat { "Setting ${selectedChapter.chapter.url} as active" }
saveReadingProgress(currentChapters.currChapter)
setReadStartTime() setReadStartTime()
viewModelScope.launch { loadNewChapter(selectedChapter) } viewModelScope.launch { loadNewChapter(selectedChapter) }
} }
val pages = page.chapter.pages ?: return
val inDownloadRange = page.number.toDouble() / pages.size > 0.25 val inDownloadRange = page.number.toDouble() / pages.size > 0.25
if (inDownloadRange) { if (inDownloadRange) {
downloadNextChapters() downloadNextChapters()
@ -520,17 +506,15 @@ class ReaderViewModel(
} }
} }
fun saveCurrentChapterReadingProgress() {
getCurrentChapter()?.let { saveReadingProgress(it) }
}
/** /**
* Called when reader chapter is changed in reader or when activity is paused. * Called when reader chapter is changed in reader or when activity is paused.
*/ */
private fun saveReadingProgress(readerChapter: ReaderChapter) { private fun saveReadingProgress() {
viewModelScope.launchNonCancellable { getCurrentChapter()?.let {
saveChapterProgress(readerChapter) viewModelScope.launchNonCancellable {
saveChapterHistory(readerChapter) saveChapterProgress(it)
saveChapterHistory(it)
}
} }
} }
@ -542,7 +526,7 @@ class ReaderViewModel(
if (incognitoMode) return if (incognitoMode) return
val chapter = readerChapter.chapter val chapter = readerChapter.chapter
getCurrentChapter()?.requestedPage = chapter.last_page_read readerChapter.requestedPage = chapter.last_page_read
updateChapter.await( updateChapter.await(
ChapterUpdate( ChapterUpdate(
id = chapter.id!!, id = chapter.id!!,