From 72e35f590b267090fdc48afeeaca4d88a1f1a11a Mon Sep 17 00:00:00 2001 From: Jays2Kings Date: Tue, 14 Feb 2023 20:37:58 -0500 Subject: [PATCH] Reset requested page in reader for read content Also fixed page index not being remembered when switching chapters in reader --- .../tachiyomi/ui/reader/ReaderViewModel.kt | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 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 120d34f8b9..b19c79b4c0 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 @@ -157,13 +157,17 @@ class ReaderViewModel( } init { + var secondRun = false // To save state state.map { it.viewerChapters?.currChapter } .distinctUntilChanged() .filterNotNull() .onEach { currentChapter -> chapterId = currentChapter.chapter.id!! - currentChapter.requestedPage = currentChapter.chapter.last_page_read + if (secondRun || !currentChapter.chapter.read) { + currentChapter.requestedPage = currentChapter.chapter.last_page_read + } + secondRun = true } .launchIn(viewModelScope) } @@ -224,7 +228,9 @@ class ReaderViewModel( val manga = db.getManga(mangaId).executeAsBlocking() if (manga != null) { mutableState.update { it.copy(manga = manga) } - if (chapterId == -1L) chapterId = initialChapterId + if (chapterId == -1L) { + chapterId = initialChapterId + } checkTrackers(manga) @@ -417,21 +423,18 @@ class ReaderViewModel( val loader = loader ?: return -1 Timber.d("Loading adjacent ${chapter.chapter.url}") - var lastPage: Int? = null + var lastPage: Int? = if (chapter.chapter.pages_left <= 1) 0 else chapter.chapter.last_page_read mutableState.update { it.copy(isLoadingAdjacentChapter = true) } try { withIOContext { loadChapter(loader, chapter) - lastPage = - if (chapter.chapter.pages_left <= 1) 0 else chapter.chapter.last_page_read - withUIContext { - } } } catch (e: Throwable) { if (e is CancellationException) { throw e } Timber.e(e) + lastPage = null } finally { mutableState.update { it.copy(isLoadingAdjacentChapter = false) } }