ReaderViewModel: Fix saved state (#9807)

Also save page index
This commit is contained in:
Ivan Iskandar 2023-08-05 20:47:02 +07:00 committed by GitHub
parent d874f20362
commit af0fdfa3b7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -79,8 +79,8 @@ import java.util.Date
/** /**
* Presenter used by the activity to perform background operations. * Presenter used by the activity to perform background operations.
*/ */
class ReaderViewModel( class ReaderViewModel @JvmOverloads constructor(
private val savedState: SavedStateHandle = SavedStateHandle(), private val savedState: SavedStateHandle,
private val sourceManager: SourceManager = Injekt.get(), private val sourceManager: SourceManager = Injekt.get(),
private val downloadManager: DownloadManager = Injekt.get(), private val downloadManager: DownloadManager = Injekt.get(),
private val downloadProvider: DownloadProvider = Injekt.get(), private val downloadProvider: DownloadProvider = Injekt.get(),
@ -120,6 +120,15 @@ class ReaderViewModel(
field = value field = value
} }
/**
* The visible page index of the currently loaded chapter. Used to restore from process kill.
*/
private var chapterPageIndex = savedState.get<Int>("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. * The chapter loader for the loaded manga. It'll be null until [manga] is set.
*/ */
@ -198,7 +207,10 @@ class ReaderViewModel(
.distinctUntilChanged() .distinctUntilChanged()
.filterNotNull() .filterNotNull()
.onEach { currentChapter -> .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 currentChapter.requestedPage = currentChapter.chapter.last_page_read
} }
chapterId = currentChapter.chapter.id!! chapterId = currentChapter.chapter.id!!
@ -490,6 +502,7 @@ class ReaderViewModel(
it.copy(currentPage = pageIndex + 1) it.copy(currentPage = pageIndex + 1)
} }
readerChapter.requestedPage = pageIndex readerChapter.requestedPage = pageIndex
chapterPageIndex = pageIndex
if (!incognitoMode && page.status != Page.State.ERROR) { if (!incognitoMode && page.status != Page.State.ERROR) {
readerChapter.chapter.last_page_read = pageIndex readerChapter.chapter.last_page_read = pageIndex