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.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,
)
}
}

View File

@ -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

View File

@ -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,
)
}
}