From bbee093c63563db70f62209c10e1d94cb3bd81ef Mon Sep 17 00:00:00 2001 From: arkon Date: Sat, 6 Feb 2021 12:15:34 -0500 Subject: [PATCH] Remove some logic around old legacy backup versions + minor optimizations --- .../data/backup/legacy/LegacyBackupManager.kt | 55 +++++++------------ .../tachiyomi/data/backup/BackupTest.kt | 38 +++---------- 2 files changed, 27 insertions(+), 66 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/backup/legacy/LegacyBackupManager.kt b/app/src/main/java/eu/kanade/tachiyomi/data/backup/legacy/LegacyBackupManager.kt index ff154eeb4c..e5a50b1ca7 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/backup/legacy/LegacyBackupManager.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/backup/legacy/LegacyBackupManager.kt @@ -53,30 +53,14 @@ import kotlin.math.max class LegacyBackupManager(context: Context, version: Int = CURRENT_VERSION) : AbstractBackupManager(context) { - var parserVersion: Int = version - private set - - var parser: Gson = initParser() - - /** - * Set version of parser - * - * @param version version of parser - */ - internal fun setVersion(version: Int) { - this.parserVersion = version - parser = initParser() - } - - private fun initParser(): Gson = when (parserVersion) { - 2 -> - GsonBuilder() - .registerTypeAdapter(MangaTypeAdapter.build()) - .registerTypeHierarchyAdapter(ChapterTypeAdapter.build()) - .registerTypeAdapter(CategoryTypeAdapter.build()) - .registerTypeAdapter(HistoryTypeAdapter.build()) - .registerTypeHierarchyAdapter(TrackTypeAdapter.build()) - .create() + val parser: Gson = when (version) { + 2 -> GsonBuilder() + .registerTypeAdapter(MangaTypeAdapter.build()) + .registerTypeHierarchyAdapter(ChapterTypeAdapter.build()) + .registerTypeAdapter(CategoryTypeAdapter.build()) + .registerTypeAdapter(HistoryTypeAdapter.build()) + .registerTypeHierarchyAdapter(TrackTypeAdapter.build()) + .create() else -> throw Exception("Unknown backup version") } @@ -308,7 +292,7 @@ class LegacyBackupManager(context: Context, version: Int = CURRENT_VERSION) : Ab */ internal fun restoreCategoriesForManga(manga: Manga, categories: List) { val dbCategories = databaseHelper.getCategories().executeAsBlocking() - val mangaCategoriesToUpdate = mutableListOf() + val mangaCategoriesToUpdate = ArrayList(categories.size) for (backupCategoryStr in categories) { for (dbCategory in dbCategories) { if (backupCategoryStr == dbCategory.name) { @@ -332,7 +316,7 @@ class LegacyBackupManager(context: Context, version: Int = CURRENT_VERSION) : Ab */ internal fun restoreHistoryForManga(history: List) { // List containing history to be updated - val historyToBeUpdated = mutableListOf() + val historyToBeUpdated = ArrayList(history.size) for ((url, lastRead) in history) { val dbHistory = databaseHelper.getHistoryByChapterUrl(url).executeAsBlocking() // Check if history already in database and update @@ -361,14 +345,14 @@ class LegacyBackupManager(context: Context, version: Int = CURRENT_VERSION) : Ab * @param tracks the track list to restore. */ internal fun restoreTrackForManga(manga: Manga, tracks: List) { - // Fix foreign keys with the current manga id - tracks.map { it.manga_id = manga.id!! } - // Get tracks from database val dbTracks = databaseHelper.getTracks(manga).executeAsBlocking() - val trackToUpdate = mutableListOf() + val trackToUpdate = ArrayList(tracks.size) tracks.forEach { track -> + // Fix foreign keys with the current manga id + track.manga_id = manga.id!! + val service = trackManager.getService(track.sync_id) if (service != null && service.isLogged) { var isInDatabase = false @@ -423,12 +407,13 @@ class LegacyBackupManager(context: Context, version: Int = CURRENT_VERSION) : Ab chapter.copyFrom(dbChapter) break } - } - // Filter the chapters that couldn't be found. - chapters.filter { it.id != null } - chapters.map { it.manga_id = manga.id } - updateChapters(chapters) + chapter.manga_id = manga.id + } + + // Filter the chapters that couldn't be found. + updateChapters(chapters.filter { it.id != null }) + return true } } diff --git a/app/src/test/java/eu/kanade/tachiyomi/data/backup/BackupTest.kt b/app/src/test/java/eu/kanade/tachiyomi/data/backup/BackupTest.kt index babbd2406b..12f233c40f 100644 --- a/app/src/test/java/eu/kanade/tachiyomi/data/backup/BackupTest.kt +++ b/app/src/test/java/eu/kanade/tachiyomi/data/backup/BackupTest.kt @@ -68,7 +68,7 @@ class BackupTest { fun setup() { app = RuntimeEnvironment.application context = app.applicationContext - legacyBackupManager = LegacyBackupManager(context) + legacyBackupManager = LegacyBackupManager(context, 2) db = legacyBackupManager.databaseHelper // Mock the source manager @@ -84,6 +84,8 @@ class BackupTest { root.add(Backup.MANGAS, mangaEntries) root.add(Backup.CATEGORIES, categoryEntries) + + clearJson() } /** @@ -91,9 +93,6 @@ class BackupTest { */ @Test fun testRestoreEmptyCategory() { - // Initialize json with version 2 - initializeJsonTest(2) - // Create backup of empty database legacyBackupManager.backupCategories(categoryEntries) @@ -110,9 +109,6 @@ class BackupTest { */ @Test fun testRestoreSingleCategory() { - // Initialize json with version 2 - initializeJsonTest(2) - // Create category and add to json val category = addSingleCategory("category") @@ -130,9 +126,6 @@ class BackupTest { */ @Test fun testRestoreMultipleCategories() { - // Initialize json with version 2 - initializeJsonTest(2) - // Create category and add to json val category = addSingleCategory("category") val category2 = addSingleCategory("category2") @@ -161,9 +154,6 @@ class BackupTest { */ @Test fun testRestoreManga() { - // Initialize json with version 2 - initializeJsonTest(2) - // Add manga to database val manga = getSingleManga("One Piece") manga.viewer = 3 @@ -227,9 +217,6 @@ class BackupTest { */ @Test fun testRestoreChapters() { - // Initialize json with version 2 - initializeJsonTest(2) - // Insert manga val manga = getSingleManga("One Piece") manga.id = legacyBackupManager.databaseHelper.insertManga(manga).executeAsBlocking().insertedId() @@ -266,9 +253,6 @@ class BackupTest { */ @Test fun restoreHistoryForManga() { - // Initialize json with version 2 - initializeJsonTest(2) - val manga = getSingleManga("One Piece") manga.id = legacyBackupManager.databaseHelper.insertManga(manga).executeAsBlocking().insertedId() @@ -300,9 +284,6 @@ class BackupTest { */ @Test fun restoreTrackForManga() { - // Initialize json with version 2 - initializeJsonTest(2) - // Create mangas val manga = getSingleManga("One Piece") val manga2 = getSingleManga("Bleach") @@ -359,31 +340,26 @@ class BackupTest { assertThat(trackDB[0].last_chapter_read).isEqualTo(10) } - fun clearJson() { + private fun clearJson() { root = JsonObject() information = JsonObject() mangaEntries = JsonArray() categoryEntries = JsonArray() } - fun initializeJsonTest(version: Int) { - clearJson() - legacyBackupManager.setVersion(version) - } - - fun addSingleCategory(name: String): Category { + private fun addSingleCategory(name: String): Category { val category = Category.create(name) val catJson = legacyBackupManager.parser.toJsonTree(category) categoryEntries.add(catJson) return category } - fun clearDatabase() { + private fun clearDatabase() { db.deleteMangas().executeAsBlocking() db.deleteHistory().executeAsBlocking() } - fun getSingleHistory(chapter: Chapter): DHistory { + private fun getSingleHistory(chapter: Chapter): DHistory { return DHistory(chapter.url, 1000) }