Fix reader not saving read duration when changing chapter (#2784)
(cherry picked from commit 2e0786f699cc6d4863eb20331739c8325a451e63) # Conflicts: # CHANGELOG.md
This commit is contained in:
@@ -467,7 +467,9 @@ class ReaderActivity : BaseActivity() {
|
||||
}
|
||||
|
||||
override fun onPause() {
|
||||
viewModel.flushReadTimer()
|
||||
lifecycleScope.launchNonCancellable {
|
||||
viewModel.updateHistory()
|
||||
}
|
||||
super.onPause()
|
||||
}
|
||||
|
||||
|
||||
@@ -487,7 +487,7 @@ class ReaderViewModel @JvmOverloads constructor(
|
||||
viewModelScope.launchIO {
|
||||
logcat { "Loading ${chapter.chapter.url}" }
|
||||
|
||||
flushReadTimer()
|
||||
updateHistory()
|
||||
restartReadTimer()
|
||||
|
||||
try {
|
||||
@@ -784,26 +784,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
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user