Эх сурвалжийг харах

Save read duration to backup (#7672)

* Save read duration to backup

* Add default value

Co-authored-by: Andreas <[email protected]>

Co-authored-by: Andreas <[email protected]>
nzoba 2 жил өмнө
parent
commit
3d4e56948d

+ 7 - 4
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<BackupHistory>) {
         // List containing history to be updated
         val toUpdate = mutableListOf<HistoryUpdate>()
-        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,
                             ),
                         )
                     }

+ 2 - 1
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 {

+ 2 - 0
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,
 )