From 5fec881387d1d4b5dda19b4a58e103bf60ef4a59 Mon Sep 17 00:00:00 2001 From: arkon Date: Sat, 16 Dec 2023 11:14:53 -0500 Subject: [PATCH] Clean up history restoring --- .../tachiyomi/data/backup/BackupRestorer.kt | 65 +++++++++---------- .../data/backup/models/BackupHistory.kt | 11 +++- .../tachiyomi/domain/history/model/History.kt | 11 +++- 3 files changed, 49 insertions(+), 38 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupRestorer.kt b/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupRestorer.kt index 3ec3208d05..55488ca86b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupRestorer.kt +++ b/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) { - val toUpdate = mutableListOf() - 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, ) } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/backup/models/BackupHistory.kt b/app/src/main/java/eu/kanade/tachiyomi/data/backup/models/BackupHistory.kt index fe693f4d2c..1108a376e7 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/backup/models/BackupHistory.kt +++ b/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 diff --git a/domain/src/main/java/tachiyomi/domain/history/model/History.kt b/domain/src/main/java/tachiyomi/domain/history/model/History.kt index bb2917ad99..41b58b6378 100644 --- a/domain/src/main/java/tachiyomi/domain/history/model/History.kt +++ b/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, + ) + } +}