From af0fdfa3b7e859db54154473a8cd4c7b50121ca7 Mon Sep 17 00:00:00 2001 From: Ivan Iskandar <12537387+ivaniskandar@users.noreply.github.com> Date: Sat, 5 Aug 2023 20:47:02 +0700 Subject: [PATCH] ReaderViewModel: Fix saved state (#9807) Also save page index --- .../tachiyomi/ui/reader/ReaderViewModel.kt | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) 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 bf6ee7c5f..8d16c7b80 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 @@ -79,8 +79,8 @@ import java.util.Date /** * Presenter used by the activity to perform background operations. */ -class ReaderViewModel( - private val savedState: SavedStateHandle = SavedStateHandle(), +class ReaderViewModel @JvmOverloads constructor( + private val savedState: SavedStateHandle, private val sourceManager: SourceManager = Injekt.get(), private val downloadManager: DownloadManager = Injekt.get(), private val downloadProvider: DownloadProvider = Injekt.get(), @@ -120,6 +120,15 @@ class ReaderViewModel( field = value } + /** + * The visible page index of the currently loaded chapter. Used to restore from process kill. + */ + private var chapterPageIndex = savedState.get("page_index") ?: -1 + set(value) { + savedState["page_index"] = value + field = value + } + /** * The chapter loader for the loaded manga. It'll be null until [manga] is set. */ @@ -198,7 +207,10 @@ class ReaderViewModel( .distinctUntilChanged() .filterNotNull() .onEach { currentChapter -> - if (!currentChapter.chapter.read) { + if (chapterPageIndex >= 0) { + // Restore from SavedState + currentChapter.requestedPage = chapterPageIndex + } else if (!currentChapter.chapter.read) { currentChapter.requestedPage = currentChapter.chapter.last_page_read } chapterId = currentChapter.chapter.id!! @@ -490,6 +502,7 @@ class ReaderViewModel( it.copy(currentPage = pageIndex + 1) } readerChapter.requestedPage = pageIndex + chapterPageIndex = pageIndex if (!incognitoMode && page.status != Page.State.ERROR) { readerChapter.chapter.last_page_read = pageIndex