Fix reader not saving read duration when changing chapter (mihonapp/mihon#2784)

(cherry picked from commit 2e0786f699cc6d4863eb20331739c8325a451e63)
This commit is contained in:
AntsyLich 2025-12-13 00:49:57 +06:00 committed by Cuong-Tran
parent a5d8f2efac
commit 2274e901a1
2 changed files with 13 additions and 17 deletions

View file

@ -323,7 +323,9 @@ class ReaderActivity : BaseActivity() {
}
override fun onPause() {
viewModel.flushReadTimer()
lifecycleScope.launchNonCancellable {
viewModel.updateHistory()
}
// AM (DISCORD) -->
updateDiscordRPC(exitingReader = true)

View file

@ -571,7 +571,7 @@ class ReaderViewModel @JvmOverloads constructor(
viewModelScope.launchIO {
logcat { "Loading ${chapter.chapter.url}" }
flushReadTimer()
updateHistory()
restartReadTimer()
try {
@ -879,26 +879,20 @@ class ReaderViewModel @JvmOverloads constructor(
chapterReadStartTime = Instant.now().toEpochMilli()
}
fun flushReadTimer() {
getCurrentChapter()?.let {
viewModelScope.launchNonCancellable {
updateHistory(it)
}
}
}
/**
* Saves the chapter last read history if incognito mode isn't on.
*/
private suspend fun updateHistory(readerChapter: ReaderChapter) {
if (incognitoMode) return
suspend fun updateHistory() {
getCurrentChapter()?.let { readerChapter ->
if (incognitoMode) return@let
val chapterId = readerChapter.chapter.id!!
val endTime = Date()
val sessionReadDuration = chapterReadStartTime?.let { endTime.time - it } ?: 0
val chapterId = readerChapter.chapter.id!!
val endTime = Date()
val sessionReadDuration = chapterReadStartTime?.let { endTime.time - it } ?: 0
upsertHistory.await(HistoryUpdate(chapterId, endTime, sessionReadDuration))
chapterReadStartTime = null
upsertHistory.await(HistoryUpdate(chapterId, endTime, sessionReadDuration))
chapterReadStartTime = null
}
}
/**