From 6fe5e6e21bfaef2f03ecca3659e03835fc354602 Mon Sep 17 00:00:00 2001 From: arkon Date: Wed, 5 Jul 2023 18:57:57 -0400 Subject: [PATCH] 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. --- .../tachiyomi/ui/reader/ReaderActivity.kt | 14 ------- .../tachiyomi/ui/reader/ReaderViewModel.kt | 40 ++++++------------- 2 files changed, 12 insertions(+), 42 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt index 2824bad65f..1710814df1 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt @@ -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. diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderViewModel.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderViewModel.kt index a2b9bf0815..422eacceb6 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderViewModel.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderViewModel.kt @@ -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!!,