Sfoglia il codice sorgente

Clean up history restoring

arkon 1 anno fa
parent
commit
5fec881387

+ 29 - 36
app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupRestorer.kt

@@ -31,7 +31,6 @@ import tachiyomi.data.UpdateStrategyColumnAdapter
 import tachiyomi.domain.category.interactor.GetCategories
 import tachiyomi.domain.chapter.interactor.GetChaptersByMangaId
 import tachiyomi.domain.chapter.model.Chapter
-import tachiyomi.domain.history.model.HistoryUpdate
 import tachiyomi.domain.library.service.LibraryPreferences
 import tachiyomi.domain.manga.interactor.FetchInterval
 import tachiyomi.domain.manga.interactor.GetMangaByUrlAndSourceId
@@ -291,7 +290,7 @@ class BackupRestorer(
 
         val (existingChapters, newChapters) = backupChapters
             .mapNotNull {
-                val chapter = it.toChapterImpl()
+                val chapter = it.toChapterImpl().copy(mangaId = manga.id)
 
                 val dbChapter = dbChaptersByUrl[chapter.url]
                     ?: // New chapter
@@ -307,7 +306,6 @@ class BackupRestorer(
                     .copyFrom(dbChapter)
                     .copy(
                         id = dbChapter.id,
-                        mangaId = manga.id,
                         bookmark = chapter.bookmark || dbChapter.bookmark,
                     )
                 if (dbChapter.read && !updatedChapter.read) {
@@ -455,44 +453,39 @@ class BackupRestorer(
     }
 
     private suspend fun restoreHistory(backupHistory: List<BackupHistory>) {
-        val toUpdate = mutableListOf<HistoryUpdate>()
-        for ((url, lastRead, readDuration) in backupHistory) {
-            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)),
-                    time_read = max(readDuration, dbHistory.time_read) - dbHistory.time_read,
-                )
-                toUpdate.add(
-                    HistoryUpdate(
-                        chapterId = dbHistory.chapter_id,
-                        readAt = dbHistory.last_read!!,
-                        sessionReadDuration = dbHistory.time_read,
-                    ),
-                )
-            } else {
-                // If not in database, create
-                handler
-                    .awaitOneOrNull { chaptersQueries.getChapterByUrl(url) }
-                    ?.let {
-                        toUpdate.add(
-                            HistoryUpdate(
-                                chapterId = it._id,
-                                readAt = Date(lastRead),
-                                sessionReadDuration = readDuration,
-                            ),
-                        )
-                    }
+        val toUpdate = backupHistory.mapNotNull { history ->
+            val dbHistory = handler.awaitOneOrNull { historyQueries.getHistoryByChapterUrl(history.url) }
+            val item = history.getHistoryImpl()
+
+            if (dbHistory == null) {
+                val chapter = handler.awaitOneOrNull { chaptersQueries.getChapterByUrl(history.url) }
+                return@mapNotNull if (chapter == null) {
+                    // Chapter doesn't exist; skip
+                    null
+                } else {
+                    // New history entry
+                    item.copy(chapterId = chapter._id)
+                }
             }
+
+            // Update history entry
+            item.copy(
+                id = dbHistory._id,
+                chapterId = dbHistory.chapter_id,
+                readAt = max(item.readAt?.time ?: 0L, dbHistory.last_read?.time ?: 0L)
+                    .takeIf { it > 0L }
+                    ?.let { Date(it) },
+                readDuration = max(item.readDuration, dbHistory.time_read),
+            )
         }
+
         if (toUpdate.isNotEmpty()) {
             handler.await(true) {
-                toUpdate.forEach { payload ->
+                toUpdate.forEach {
                     historyQueries.upsert(
-                        payload.chapterId,
-                        payload.readAt,
-                        payload.sessionReadDuration,
+                        it.chapterId,
+                        it.readAt,
+                        it.readDuration,
                     )
                 }
             }

+ 10 - 1
app/src/main/java/eu/kanade/tachiyomi/data/backup/models/BackupHistory.kt

@@ -2,13 +2,22 @@ package eu.kanade.tachiyomi.data.backup.models
 
 import kotlinx.serialization.Serializable
 import kotlinx.serialization.protobuf.ProtoNumber
+import tachiyomi.domain.history.model.History
+import java.util.Date
 
 @Serializable
 data class BackupHistory(
     @ProtoNumber(1) var url: String,
     @ProtoNumber(2) var lastRead: Long,
     @ProtoNumber(3) var readDuration: Long = 0,
-)
+) {
+    fun getHistoryImpl(): History {
+        return History.create().copy(
+            readAt = Date(lastRead),
+            readDuration = readDuration,
+        )
+    }
+}
 
 @Deprecated("Replaced with BackupHistory. This is retained for legacy reasons.")
 @Serializable

+ 10 - 1
domain/src/main/java/tachiyomi/domain/history/model/History.kt

@@ -7,4 +7,13 @@ data class History(
     val chapterId: Long,
     val readAt: Date?,
     val readDuration: Long,
-)
+) {
+    companion object {
+        fun create() = History(
+            id = -1L,
+            chapterId = -1L,
+            readAt = null,
+            readDuration = -1L,
+        )
+    }
+}