Clean up history restoring

This commit is contained in:
arkon 2023-12-16 11:14:53 -05:00
parent 3ac68e810d
commit 5fec881387
3 changed files with 49 additions and 38 deletions

View File

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

View File

@ -2,13 +2,22 @@ package eu.kanade.tachiyomi.data.backup.models
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import kotlinx.serialization.protobuf.ProtoNumber import kotlinx.serialization.protobuf.ProtoNumber
import tachiyomi.domain.history.model.History
import java.util.Date
@Serializable @Serializable
data class BackupHistory( data class BackupHistory(
@ProtoNumber(1) var url: String, @ProtoNumber(1) var url: String,
@ProtoNumber(2) var lastRead: Long, @ProtoNumber(2) var lastRead: Long,
@ProtoNumber(3) var readDuration: Long = 0, @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.") @Deprecated("Replaced with BackupHistory. This is retained for legacy reasons.")
@Serializable @Serializable

View File

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