From 9f9f9872eb32b12e9d2e31fe427cddd5542642b2 Mon Sep 17 00:00:00 2001 From: Jobobby04 Date: Sun, 7 Feb 2021 21:54:26 -0500 Subject: [PATCH] Fix legacy backups (cherry picked from commit ded58541f5903c109b70799683829e26018d2af6) --- .../data/backup/AbstractBackupManager.kt | 7 ++++ .../data/backup/full/FullBackupManager.kt | 2 +- .../data/database/queries/ChapterQueries.kt | 6 ++++ .../resolvers/ChapterBackupPutResolver.kt | 4 +-- .../ChapterKnownBackupPutResolver.kt | 34 +++++++++++++++++++ 5 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 app/src/main/java/eu/kanade/tachiyomi/data/database/resolvers/ChapterKnownBackupPutResolver.kt diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/backup/AbstractBackupManager.kt b/app/src/main/java/eu/kanade/tachiyomi/data/backup/AbstractBackupManager.kt index 8afc1eaea7..723fb259be 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/backup/AbstractBackupManager.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/backup/AbstractBackupManager.kt @@ -80,6 +80,13 @@ abstract class AbstractBackupManager(protected val context: Context) { databaseHelper.updateChaptersBackup(chapters).executeAsBlocking() } + /** + * Updates a list of chapters with known database ids + */ + protected fun updateKnownChapters(chapters: List) { + databaseHelper.updateKnownChaptersBackup(chapters).executeAsBlocking() + } + /** * Return number of backups. * diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/backup/full/FullBackupManager.kt b/app/src/main/java/eu/kanade/tachiyomi/data/backup/full/FullBackupManager.kt index 86b7644644..a11bb8967d 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/backup/full/FullBackupManager.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/backup/full/FullBackupManager.kt @@ -405,7 +405,7 @@ class FullBackupManager(context: Context) : AbstractBackupManager(context) { } val newChapters = chapters.groupBy { it.id != null } - newChapters[true]?.let { updateChapters(it) } + newChapters[true]?.let { updateKnownChapters(it) } newChapters[false]?.let { insertChapters(it) } } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/ChapterQueries.kt b/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/ChapterQueries.kt index 77f7b3de29..948ab9654f 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/ChapterQueries.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/ChapterQueries.kt @@ -7,6 +7,7 @@ import eu.kanade.tachiyomi.data.database.models.Chapter import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.database.models.MangaChapter import eu.kanade.tachiyomi.data.database.resolvers.ChapterBackupPutResolver +import eu.kanade.tachiyomi.data.database.resolvers.ChapterKnownBackupPutResolver import eu.kanade.tachiyomi.data.database.resolvers.ChapterProgressPutResolver import eu.kanade.tachiyomi.data.database.resolvers.ChapterSourceOrderPutResolver import eu.kanade.tachiyomi.data.database.resolvers.MangaChapterGetResolver @@ -84,6 +85,11 @@ interface ChapterQueries : DbProvider { .withPutResolver(ChapterBackupPutResolver()) .prepare() + fun updateKnownChaptersBackup(chapters: List) = db.put() + .objects(chapters) + .withPutResolver(ChapterKnownBackupPutResolver()) + .prepare() + fun updateChapterProgress(chapter: Chapter) = db.put() .`object`(chapter) .withPutResolver(ChapterProgressPutResolver()) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/database/resolvers/ChapterBackupPutResolver.kt b/app/src/main/java/eu/kanade/tachiyomi/data/database/resolvers/ChapterBackupPutResolver.kt index dcf3912c2e..76516139fb 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/database/resolvers/ChapterBackupPutResolver.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/database/resolvers/ChapterBackupPutResolver.kt @@ -21,8 +21,8 @@ class ChapterBackupPutResolver : PutResolver() { fun mapToUpdateQuery(chapter: Chapter) = UpdateQuery.builder() .table(ChapterTable.TABLE) - .where("${ChapterTable.COL_ID} = ?") - .whereArgs(chapter.id) + .where("${ChapterTable.COL_URL} = ?") + .whereArgs(chapter.url) .build() fun mapToContentValues(chapter: Chapter) = diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/database/resolvers/ChapterKnownBackupPutResolver.kt b/app/src/main/java/eu/kanade/tachiyomi/data/database/resolvers/ChapterKnownBackupPutResolver.kt new file mode 100644 index 0000000000..5b8882e115 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/data/database/resolvers/ChapterKnownBackupPutResolver.kt @@ -0,0 +1,34 @@ +package eu.kanade.tachiyomi.data.database.resolvers + +import androidx.core.content.contentValuesOf +import com.pushtorefresh.storio.sqlite.StorIOSQLite +import com.pushtorefresh.storio.sqlite.operations.put.PutResolver +import com.pushtorefresh.storio.sqlite.operations.put.PutResult +import com.pushtorefresh.storio.sqlite.queries.UpdateQuery +import eu.kanade.tachiyomi.data.database.inTransactionReturn +import eu.kanade.tachiyomi.data.database.models.Chapter +import eu.kanade.tachiyomi.data.database.tables.ChapterTable + +class ChapterKnownBackupPutResolver : PutResolver() { + + override fun performPut(db: StorIOSQLite, chapter: Chapter) = db.inTransactionReturn { + val updateQuery = mapToUpdateQuery(chapter) + val contentValues = mapToContentValues(chapter) + + val numberOfRowsUpdated = db.lowLevel().update(updateQuery, contentValues) + PutResult.newUpdateResult(numberOfRowsUpdated, updateQuery.table()) + } + + fun mapToUpdateQuery(chapter: Chapter) = UpdateQuery.builder() + .table(ChapterTable.TABLE) + .where("${ChapterTable.COL_ID} = ?") + .whereArgs(chapter.id) + .build() + + fun mapToContentValues(chapter: Chapter) = + contentValuesOf( + ChapterTable.COL_READ to chapter.read, + ChapterTable.COL_BOOKMARK to chapter.bookmark, + ChapterTable.COL_LAST_PAGE_READ to chapter.last_page_read + ) +}