From 3d4e56948d86d10ecd227fb59b92da069f37a89f Mon Sep 17 00:00:00 2001 From: nzoba <55888232+nzoba@users.noreply.github.com> Date: Wed, 3 Aug 2022 23:40:13 +0200 Subject: [PATCH] Save read duration to backup (#7672) * Save read duration to backup * Add default value Co-authored-by: Andreas Co-authored-by: Andreas --- .../tachiyomi/data/backup/full/FullBackupManager.kt | 11 +++++++---- .../tachiyomi/data/backup/full/FullBackupRestore.kt | 3 ++- .../data/backup/full/models/BackupHistory.kt | 2 ++ 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/backup/full/FullBackupManager.kt b/app/src/main/java/eu/kanade/tachiyomi/data/backup/full/FullBackupManager.kt index 652f276f20..e5c76789f0 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/backup/full/FullBackupManager.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/backup/full/FullBackupManager.kt @@ -187,7 +187,7 @@ class FullBackupManager(context: Context) : AbstractBackupManager(context) { if (historyByMangaId.isNotEmpty()) { val history = historyByMangaId.map { history -> val chapter = handler.awaitOne { chaptersQueries.getChapterById(history.chapter_id) } - BackupHistory(chapter.url, history.last_read?.time ?: 0L) + BackupHistory(chapter.url, history.last_read?.time ?: 0L, history.time_read) } if (history.isNotEmpty()) { mangaObject.history = history @@ -295,11 +295,14 @@ class FullBackupManager(context: Context) : AbstractBackupManager(context) { internal suspend fun restoreHistoryForManga(history: List) { // List containing history to be updated val toUpdate = mutableListOf() - for ((url, lastRead) in history) { + for ((url, lastRead, readDuration) in history) { var dbHistory = handler.awaitOneOrNull { historyQueries.getHistoryByChapterUrl(url) } // Check if history already in database and update if (dbHistory != null) { - dbHistory = dbHistory.copy(last_read = Date(max(lastRead, dbHistory.last_read?.time ?: 0L))) + dbHistory = dbHistory.copy( + last_read = Date(max(lastRead, dbHistory.last_read?.time ?: 0L)), + time_read = max(readDuration, dbHistory.time_read) - dbHistory.time_read, + ) toUpdate.add( HistoryUpdate( chapterId = dbHistory.chapter_id, @@ -316,7 +319,7 @@ class FullBackupManager(context: Context) : AbstractBackupManager(context) { HistoryUpdate( chapterId = it._id, readAt = Date(lastRead), - sessionReadDuration = 0, + sessionReadDuration = readDuration, ), ) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/backup/full/FullBackupRestore.kt b/app/src/main/java/eu/kanade/tachiyomi/data/backup/full/FullBackupRestore.kt index e32086bb0b..978102f745 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/backup/full/FullBackupRestore.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/backup/full/FullBackupRestore.kt @@ -63,7 +63,8 @@ class FullBackupRestore(context: Context, notifier: BackupNotifier) : AbstractBa val manga = backupManga.getMangaImpl() val chapters = backupManga.getChaptersImpl() val categories = backupManga.categories.map { it.toInt() } - val history = backupManga.brokenHistory.map { BackupHistory(it.url, it.lastRead) } + backupManga.history + val history = + backupManga.brokenHistory.map { BackupHistory(it.url, it.lastRead, it.readDuration) } + backupManga.history val tracks = backupManga.getTrackingImpl() try { diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/backup/full/models/BackupHistory.kt b/app/src/main/java/eu/kanade/tachiyomi/data/backup/full/models/BackupHistory.kt index 790e433c91..a2b66ccedc 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/backup/full/models/BackupHistory.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/backup/full/models/BackupHistory.kt @@ -7,10 +7,12 @@ import kotlinx.serialization.protobuf.ProtoNumber data class BrokenBackupHistory( @ProtoNumber(0) var url: String, @ProtoNumber(1) var lastRead: Long, + @ProtoNumber(2) var readDuration: Long = 0, ) @Serializable data class BackupHistory( @ProtoNumber(1) var url: String, @ProtoNumber(2) var lastRead: Long, + @ProtoNumber(3) var readDuration: Long = 0, )