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()
}
/**
* 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.

View File

@ -223,7 +223,6 @@ class ReaderViewModel(
val currentChapters = state.value.viewerChapters
if (currentChapters != null) {
currentChapters.unref()
saveReadingProgress(currentChapters.currChapter)
chapterToDownload?.let {
downloadManager.addDownloadsToStartOfQueue(listOf(it))
}
@ -238,17 +237,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.
*/
@ -346,7 +334,6 @@ class ReaderViewModel(
*/
private suspend fun loadAdjacent(chapter: ReaderChapter) {
val loader = loader ?: return
saveCurrentChapterReadingProgress()
logcat { "Loading adjacent ${chapter.chapter.url}" }
@ -420,16 +407,17 @@ class ReaderViewModel(
* [page]'s chapter is different from the currently active.
*/
fun onPageSelected(page: ReaderPage) {
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.index + 1,
@ -446,11 +434,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()
@ -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.
*/
private fun saveReadingProgress(readerChapter: ReaderChapter) {
viewModelScope.launchNonCancellable {
saveChapterProgress(readerChapter)
saveChapterHistory(readerChapter)
private fun saveReadingProgress() {
getCurrentChapter()?.let {
viewModelScope.launchNonCancellable {
saveChapterProgress(it)
saveChapterHistory(it)
}
}
}
@ -542,7 +526,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!!,