From a462b40cadb5a60ea8c76d42cf744ad993334e0b Mon Sep 17 00:00:00 2001 From: KaiserBh Date: Wed, 20 Dec 2023 05:04:46 +1100 Subject: [PATCH] refactor: added isSync to MangaRestorer Instead of overwriting it for the backup when restoring chapters we take into account if it's sync then overwrite read status, otherwise keep the old function. Where it only mark it as a read only if the dbchapter is marked as read. --- .../data/backup/restore/BackupRestorer.kt | 2 +- .../data/backup/restore/MangaRestorer.kt | 57 +++++++++---------- 2 files changed, 29 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/backup/restore/BackupRestorer.kt b/app/src/main/java/eu/kanade/tachiyomi/data/backup/restore/BackupRestorer.kt index a1e262844..636811ce5 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/backup/restore/BackupRestorer.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/backup/restore/BackupRestorer.kt @@ -27,7 +27,7 @@ class BackupRestorer( private val categoriesRestorer: CategoriesRestorer = CategoriesRestorer(), private val preferenceRestorer: PreferenceRestorer = PreferenceRestorer(context), - private val mangaRestorer: MangaRestorer = MangaRestorer(), + private val mangaRestorer: MangaRestorer = MangaRestorer(isSync), ) { private var restoreAmount = 0 diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/backup/restore/MangaRestorer.kt b/app/src/main/java/eu/kanade/tachiyomi/data/backup/restore/MangaRestorer.kt index a07f846f2..73b69f409 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/backup/restore/MangaRestorer.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/backup/restore/MangaRestorer.kt @@ -24,6 +24,8 @@ import java.util.Date import kotlin.math.max class MangaRestorer( + private var isSync: Boolean = false, + private val handler: DatabaseHandler = Injekt.get(), private val getCategories: GetCategories = Injekt.get(), private val getMangaByUrlAndSourceId: GetMangaByUrlAndSourceId = Injekt.get(), @@ -33,7 +35,6 @@ class MangaRestorer( private val insertTrack: InsertTrack = Injekt.get(), fetchInterval: FetchInterval = Injekt.get(), ) { - private var now = ZonedDateTime.now() private var currentFetchWindow = fetchInterval.getWindow(now) @@ -100,7 +101,7 @@ class MangaRestorer( ) } - private suspend fun updateManga(manga: Manga): Manga { + suspend fun updateManga(manga: Manga): Manga { handler.await(true) { mangasQueries.update( source = manga.source, @@ -142,36 +143,15 @@ class MangaRestorer( .associateBy { it.url } val (existingChapters, newChapters) = backupChapters - .mapNotNull { - val chapter = it.toChapterImpl().copy(mangaId = manga.id) - + .mapNotNull { backupChapter -> + val chapter = backupChapter.toChapterImpl().copy(mangaId = manga.id) val dbChapter = dbChaptersByUrl[chapter.url] - ?: // New chapter - return@mapNotNull chapter - if (chapter.forComparison() == dbChapter.forComparison()) { - // Same state; skip - return@mapNotNull null + when { + dbChapter == null -> chapter // New chapter + chapter.forComparison() == dbChapter.forComparison() -> null // Same state; skip + else -> updateChapterBasedOnSyncState(chapter, dbChapter) } - - // Update to an existing chapter - var updatedChapter = chapter - .copyFrom(dbChapter) - .copy( - id = dbChapter.id, - bookmark = chapter.bookmark || dbChapter.bookmark, - ) - if (dbChapter.read && !updatedChapter.read) { - updatedChapter = updatedChapter.copy( - read = true, - lastPageRead = dbChapter.lastPageRead, - ) - } else if (updatedChapter.lastPageRead == 0L && dbChapter.lastPageRead != 0L) { - updatedChapter = updatedChapter.copy( - lastPageRead = dbChapter.lastPageRead, - ) - } - updatedChapter } .partition { it.id > 0 } @@ -179,6 +159,25 @@ class MangaRestorer( updateExistingChapters(existingChapters) } + private fun updateChapterBasedOnSyncState(chapter: Chapter, dbChapter: Chapter): Chapter { + return if (isSync) { + chapter.copy( + id = dbChapter.id, + bookmark = chapter.bookmark || dbChapter.bookmark, + read = chapter.read, + lastPageRead = chapter.lastPageRead + ) + } else { + chapter.copyFrom(dbChapter).let { + when { + dbChapter.read && !it.read -> it.copy(read = true, lastPageRead = dbChapter.lastPageRead) + it.lastPageRead == 0L && dbChapter.lastPageRead != 0L -> it.copy(lastPageRead = dbChapter.lastPageRead) + else -> it + } + } + } + } + private fun Chapter.forComparison() = this.copy(id = 0L, mangaId = 0L, dateFetch = 0L, dateUpload = 0L, lastModifiedAt = 0L)