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. (cherry picked from commit 6fe5e6e21bfaef2f03ecca3659e03835fc354602) # Conflicts: # app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderViewModel.kt
This commit is contained in:
parent
d67da8fd6f
commit
7911e39fef
@ -296,20 +296,6 @@ class ReaderActivity : BaseActivity() {
|
|||||||
readingModeToast?.cancel()
|
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.
|
* Set menu visibility again on activity resume to apply immersive mode again if needed.
|
||||||
* Helps with rotations.
|
* Helps with rotations.
|
||||||
|
@ -283,7 +283,6 @@ class ReaderViewModel(
|
|||||||
val currentChapters = state.value.viewerChapters
|
val currentChapters = state.value.viewerChapters
|
||||||
if (currentChapters != null) {
|
if (currentChapters != null) {
|
||||||
currentChapters.unref()
|
currentChapters.unref()
|
||||||
saveReadingProgress(currentChapters.currChapter)
|
|
||||||
chapterToDownload?.let {
|
chapterToDownload?.let {
|
||||||
downloadManager.addDownloadsToStartOfQueue(listOf(it))
|
downloadManager.addDownloadsToStartOfQueue(listOf(it))
|
||||||
}
|
}
|
||||||
@ -298,17 +297,6 @@ class ReaderViewModel(
|
|||||||
deletePendingChapters()
|
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.
|
* Whether this presenter is initialized yet.
|
||||||
*/
|
*/
|
||||||
@ -447,7 +435,6 @@ class ReaderViewModel(
|
|||||||
*/
|
*/
|
||||||
private suspend fun loadAdjacent(chapter: ReaderChapter) {
|
private suspend fun loadAdjacent(chapter: ReaderChapter) {
|
||||||
val loader = loader ?: return
|
val loader = loader ?: return
|
||||||
saveCurrentChapterReadingProgress()
|
|
||||||
|
|
||||||
logcat { "Loading adjacent ${chapter.chapter.url}" }
|
logcat { "Loading adjacent ${chapter.chapter.url}" }
|
||||||
|
|
||||||
@ -521,16 +508,17 @@ class ReaderViewModel(
|
|||||||
* [page]'s chapter is different from the currently active.
|
* [page]'s chapter is different from the currently active.
|
||||||
*/
|
*/
|
||||||
fun onPageSelected(page: ReaderPage, hasExtraPage: Boolean, currentPage: String) {
|
fun onPageSelected(page: ReaderPage, hasExtraPage: Boolean, currentPage: String) {
|
||||||
val currentChapters = state.value.viewerChapters ?: return
|
|
||||||
|
|
||||||
val selectedChapter = page.chapter
|
|
||||||
|
|
||||||
// InsertPage and StencilPage doesn't change page progress
|
// InsertPage and StencilPage doesn't change page progress
|
||||||
if (page is InsertPage || page is StencilPage) {
|
if (page is InsertPage || page is StencilPage) {
|
||||||
return
|
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
|
// Save last page read and mark as read if needed
|
||||||
|
saveReadingProgress()
|
||||||
mutableState.update {
|
mutableState.update {
|
||||||
it.copy(
|
it.copy(
|
||||||
currentPage = page.number,
|
currentPage = page.number,
|
||||||
@ -563,11 +551,9 @@ class ReaderViewModel(
|
|||||||
|
|
||||||
if (selectedChapter != currentChapters.currChapter) {
|
if (selectedChapter != currentChapters.currChapter) {
|
||||||
logcat { "Setting ${selectedChapter.chapter.url} as active" }
|
logcat { "Setting ${selectedChapter.chapter.url} as active" }
|
||||||
saveReadingProgress(currentChapters.currChapter)
|
|
||||||
setReadStartTime()
|
setReadStartTime()
|
||||||
viewModelScope.launch { loadNewChapter(selectedChapter) }
|
viewModelScope.launch { loadNewChapter(selectedChapter) }
|
||||||
}
|
}
|
||||||
val pages = page.chapter.pages ?: return
|
|
||||||
val inDownloadRange = page.number.toDouble() / pages.size > 0.25
|
val inDownloadRange = page.number.toDouble() / pages.size > 0.25
|
||||||
if (inDownloadRange) {
|
if (inDownloadRange) {
|
||||||
downloadNextChapters()
|
downloadNextChapters()
|
||||||
@ -639,17 +625,15 @@ class ReaderViewModel(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun saveCurrentChapterReadingProgress() {
|
|
||||||
getCurrentChapter()?.let { saveReadingProgress(it) }
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called when reader chapter is changed in reader or when activity is paused.
|
* Called when reader chapter is changed in reader or when activity is paused.
|
||||||
*/
|
*/
|
||||||
private fun saveReadingProgress(readerChapter: ReaderChapter) {
|
private fun saveReadingProgress() {
|
||||||
viewModelScope.launchNonCancellable {
|
getCurrentChapter()?.let {
|
||||||
saveChapterProgress(readerChapter)
|
viewModelScope.launchNonCancellable {
|
||||||
saveChapterHistory(readerChapter)
|
saveChapterProgress(it)
|
||||||
|
saveChapterHistory(it)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -664,7 +648,7 @@ class ReaderViewModel(
|
|||||||
if (incognitoMode) return
|
if (incognitoMode) return
|
||||||
|
|
||||||
val chapter = readerChapter.chapter
|
val chapter = readerChapter.chapter
|
||||||
getCurrentChapter()?.requestedPage = chapter.last_page_read
|
readerChapter.requestedPage = chapter.last_page_read
|
||||||
updateChapter.await(
|
updateChapter.await(
|
||||||
ChapterUpdate(
|
ChapterUpdate(
|
||||||
id = chapter.id!!,
|
id = chapter.id!!,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user