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