Reset requested page in reader for read content

Also fixed page index not being remembered when switching chapters in reader
This commit is contained in:
Jays2Kings 2023-02-14 20:37:58 -05:00
parent d75a2318a9
commit 72e35f590b

View file

@ -157,13 +157,17 @@ class ReaderViewModel(
} }
init { init {
var secondRun = false
// To save state // To save state
state.map { it.viewerChapters?.currChapter } state.map { it.viewerChapters?.currChapter }
.distinctUntilChanged() .distinctUntilChanged()
.filterNotNull() .filterNotNull()
.onEach { currentChapter -> .onEach { currentChapter ->
chapterId = currentChapter.chapter.id!! 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) .launchIn(viewModelScope)
} }
@ -224,7 +228,9 @@ class ReaderViewModel(
val manga = db.getManga(mangaId).executeAsBlocking() val manga = db.getManga(mangaId).executeAsBlocking()
if (manga != null) { if (manga != null) {
mutableState.update { it.copy(manga = manga) } mutableState.update { it.copy(manga = manga) }
if (chapterId == -1L) chapterId = initialChapterId if (chapterId == -1L) {
chapterId = initialChapterId
}
checkTrackers(manga) checkTrackers(manga)
@ -417,21 +423,18 @@ class ReaderViewModel(
val loader = loader ?: return -1 val loader = loader ?: return -1
Timber.d("Loading adjacent ${chapter.chapter.url}") 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) } mutableState.update { it.copy(isLoadingAdjacentChapter = true) }
try { try {
withIOContext { withIOContext {
loadChapter(loader, chapter) loadChapter(loader, chapter)
lastPage =
if (chapter.chapter.pages_left <= 1) 0 else chapter.chapter.last_page_read
withUIContext {
}
} }
} catch (e: Throwable) { } catch (e: Throwable) {
if (e is CancellationException) { if (e is CancellationException) {
throw e throw e
} }
Timber.e(e) Timber.e(e)
lastPage = null
} finally { } finally {
mutableState.update { it.copy(isLoadingAdjacentChapter = false) } mutableState.update { it.copy(isLoadingAdjacentChapter = false) }
} }