From c201b341a716b90d378dcda4bd9b8ac4a343d4fc Mon Sep 17 00:00:00 2001 From: AntsyLich <59261191+AntsyLich@users.noreply.github.com> Date: Tue, 30 Jul 2024 04:59:16 +0600 Subject: [PATCH] Cleanup backup/restore related code --- .../data/backup/create/BackupCreator.kt | 40 +++++++++---------- .../creators/CategoriesBackupCreator.kt | 2 +- .../create/creators/MangaBackupCreator.kt | 2 +- .../creators/PreferenceBackupCreator.kt | 4 +- .../create/creators/SourcesBackupCreator.kt | 2 +- .../data/backup/restore/BackupRestorer.kt | 8 ++-- .../restore/restorers/CategoriesRestorer.kt | 2 +- .../backup/restore/restorers/MangaRestorer.kt | 2 +- .../restore/restorers/PreferenceRestorer.kt | 4 +- 9 files changed, 32 insertions(+), 34 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/backup/create/BackupCreator.kt b/app/src/main/java/eu/kanade/tachiyomi/data/backup/create/BackupCreator.kt index 406e3bac0..3c12cecfe 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/backup/create/BackupCreator.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/backup/create/BackupCreator.kt @@ -51,24 +51,22 @@ class BackupCreator( suspend fun backup(uri: Uri, options: BackupOptions): String { var file: UniFile? = null try { - file = ( - if (isAutoBackup) { - // Get dir of file and create - val dir = UniFile.fromUri(context, uri) + file = if (isAutoBackup) { + // Get dir of file and create + val dir = UniFile.fromUri(context, uri) - // Delete older backups - dir?.listFiles { _, filename -> FILENAME_REGEX.matches(filename) } - .orEmpty() - .sortedByDescending { it.name } - .drop(MAX_AUTO_BACKUPS - 1) - .forEach { it.delete() } + // Delete older backups + dir?.listFiles { _, filename -> FILENAME_REGEX.matches(filename) } + .orEmpty() + .sortedByDescending { it.name } + .drop(MAX_AUTO_BACKUPS - 1) + .forEach { it.delete() } - // Create new file to place backup - dir?.createFile(getFilename()) - } else { - UniFile.fromUri(context, uri) - } - ) + // Create new file to place backup + dir?.createFile(getFilename()) + } else { + UniFile.fromUri(context, uri) + } if (file == null || !file.isFile) { throw IllegalStateException(context.stringResource(MR.strings.create_backup_file_error)) @@ -116,29 +114,29 @@ class BackupCreator( private suspend fun backupCategories(options: BackupOptions): List { if (!options.categories) return emptyList() - return categoriesBackupCreator.backupCategories() + return categoriesBackupCreator() } private suspend fun backupMangas(mangas: List, options: BackupOptions): List { if (!options.libraryEntries) return emptyList() - return mangaBackupCreator.backupMangas(mangas, options) + return mangaBackupCreator(mangas, options) } private fun backupSources(mangas: List): List { - return sourcesBackupCreator.backupSources(mangas) + return sourcesBackupCreator(mangas) } private fun backupAppPreferences(options: BackupOptions): List { if (!options.appSettings) return emptyList() - return preferenceBackupCreator.backupAppPreferences(includePrivatePreferences = options.privateSettings) + return preferenceBackupCreator.createApp(includePrivatePreferences = options.privateSettings) } private fun backupSourcePreferences(options: BackupOptions): List { if (!options.sourceSettings) return emptyList() - return preferenceBackupCreator.backupSourcePreferences(includePrivatePreferences = options.privateSettings) + return preferenceBackupCreator.createSource(includePrivatePreferences = options.privateSettings) } companion object { diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/backup/create/creators/CategoriesBackupCreator.kt b/app/src/main/java/eu/kanade/tachiyomi/data/backup/create/creators/CategoriesBackupCreator.kt index e1ed56ee1..945f32d86 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/backup/create/creators/CategoriesBackupCreator.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/backup/create/creators/CategoriesBackupCreator.kt @@ -11,7 +11,7 @@ class CategoriesBackupCreator( private val getCategories: GetCategories = Injekt.get(), ) { - suspend fun backupCategories(): List { + suspend operator fun invoke(): List { return getCategories.await() .filterNot(Category::isSystemCategory) .map(backupCategoryMapper) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/backup/create/creators/MangaBackupCreator.kt b/app/src/main/java/eu/kanade/tachiyomi/data/backup/create/creators/MangaBackupCreator.kt index fd10293a9..040de931b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/backup/create/creators/MangaBackupCreator.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/backup/create/creators/MangaBackupCreator.kt @@ -20,7 +20,7 @@ class MangaBackupCreator( private val getHistory: GetHistory = Injekt.get(), ) { - suspend fun backupMangas(mangas: List, options: BackupOptions): List { + suspend operator fun invoke(mangas: List, options: BackupOptions): List { return mangas.map { backupManga(it, options) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/backup/create/creators/PreferenceBackupCreator.kt b/app/src/main/java/eu/kanade/tachiyomi/data/backup/create/creators/PreferenceBackupCreator.kt index d14cef230..5b72ce384 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/backup/create/creators/PreferenceBackupCreator.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/backup/create/creators/PreferenceBackupCreator.kt @@ -22,12 +22,12 @@ class PreferenceBackupCreator( private val preferenceStore: PreferenceStore = Injekt.get(), ) { - fun backupAppPreferences(includePrivatePreferences: Boolean): List { + fun createApp(includePrivatePreferences: Boolean): List { return preferenceStore.getAll().toBackupPreferences() .withPrivatePreferences(includePrivatePreferences) } - fun backupSourcePreferences(includePrivatePreferences: Boolean): List { + fun createSource(includePrivatePreferences: Boolean): List { return sourceManager.getCatalogueSources() .filterIsInstance() .map { diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/backup/create/creators/SourcesBackupCreator.kt b/app/src/main/java/eu/kanade/tachiyomi/data/backup/create/creators/SourcesBackupCreator.kt index 0052c2c93..d3bc573a7 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/backup/create/creators/SourcesBackupCreator.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/backup/create/creators/SourcesBackupCreator.kt @@ -11,7 +11,7 @@ class SourcesBackupCreator( private val sourceManager: SourceManager = Injekt.get(), ) { - fun backupSources(mangas: List): List { + operator fun invoke(mangas: List): List { return mangas .asSequence() .map(BackupManga::source) 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 1c5a3a2d2..4d56e925b 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 @@ -100,7 +100,7 @@ class BackupRestorer( private fun CoroutineScope.restoreCategories(backupCategories: List) = launch { ensureActive() - categoriesRestorer.restoreCategories(backupCategories) + categoriesRestorer(backupCategories) restoreProgress += 1 notifier.showRestoreProgress( @@ -120,7 +120,7 @@ class BackupRestorer( ensureActive() try { - mangaRestorer.restoreManga(it, backupCategories) + mangaRestorer.restore(it, backupCategories) } catch (e: Exception) { val sourceName = sourceMapping[it.source] ?: it.source.toString() errors.add(Date() to "${it.title} [$sourceName]: ${e.message}") @@ -133,7 +133,7 @@ class BackupRestorer( private fun CoroutineScope.restoreAppPreferences(preferences: List) = launch { ensureActive() - preferenceRestorer.restoreAppPreferences(preferences) + preferenceRestorer.restoreApp(preferences) restoreProgress += 1 notifier.showRestoreProgress( @@ -146,7 +146,7 @@ class BackupRestorer( private fun CoroutineScope.restoreSourcePreferences(preferences: List) = launch { ensureActive() - preferenceRestorer.restoreSourcePreferences(preferences) + preferenceRestorer.restoreSource(preferences) restoreProgress += 1 notifier.showRestoreProgress( diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/backup/restore/restorers/CategoriesRestorer.kt b/app/src/main/java/eu/kanade/tachiyomi/data/backup/restore/restorers/CategoriesRestorer.kt index 23a2d47fa..15080f8ee 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/backup/restore/restorers/CategoriesRestorer.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/backup/restore/restorers/CategoriesRestorer.kt @@ -13,7 +13,7 @@ class CategoriesRestorer( private val libraryPreferences: LibraryPreferences = Injekt.get(), ) { - suspend fun restoreCategories(backupCategories: List) { + suspend operator fun invoke(backupCategories: List) { if (backupCategories.isNotEmpty()) { val dbCategories = getCategories.await() val dbCategoriesByName = dbCategories.associateBy { it.name } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/backup/restore/restorers/MangaRestorer.kt b/app/src/main/java/eu/kanade/tachiyomi/data/backup/restore/restorers/MangaRestorer.kt index b0c86e2ab..38d15bad1 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/backup/restore/restorers/MangaRestorer.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/backup/restore/restorers/MangaRestorer.kt @@ -53,7 +53,7 @@ class MangaRestorer( ) } - suspend fun restoreManga( + suspend fun restore( backupManga: BackupManga, backupCategories: List, ) { diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/backup/restore/restorers/PreferenceRestorer.kt b/app/src/main/java/eu/kanade/tachiyomi/data/backup/restore/restorers/PreferenceRestorer.kt index 4871e3949..b70280c55 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/backup/restore/restorers/PreferenceRestorer.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/backup/restore/restorers/PreferenceRestorer.kt @@ -22,14 +22,14 @@ class PreferenceRestorer( private val preferenceStore: PreferenceStore = Injekt.get(), ) { - fun restoreAppPreferences(preferences: List) { + fun restoreApp(preferences: List) { restorePreferences(preferences, preferenceStore) LibraryUpdateJob.setupTask(context) BackupCreateJob.setupTask(context) } - fun restoreSourcePreferences(preferences: List) { + fun restoreSource(preferences: List) { preferences.forEach { val sourcePrefs = AndroidPreferenceStore(context, sourcePreferences(it.sourceKey)) restorePreferences(it.prefs, sourcePrefs)