From d56ff9592edd889fd676f1af3b9d32cf2d6a90f5 Mon Sep 17 00:00:00 2001 From: inorichi Date: Sat, 13 Jan 2018 13:13:03 +0100 Subject: [PATCH] Use a single preference to store migration flags --- .../data/preference/PreferencesHelper.kt | 6 +-- .../tachiyomi/ui/migration/MigrationFlags.kt | 38 +++++++++++++++++++ .../ui/migration/MigrationPresenter.kt | 11 ++++-- .../ui/migration/SearchController.kt | 24 +++--------- 4 files changed, 53 insertions(+), 26 deletions(-) create mode 100644 app/src/main/java/eu/kanade/tachiyomi/ui/migration/MigrationFlags.kt diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt index 9b1605b5d..a367c6c11 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt @@ -165,10 +165,6 @@ class PreferencesHelper(val context: Context) { fun defaultCategory() = prefs.getInt(Keys.defaultCategory, -1) - fun migrateChapters() = rxPrefs.getBoolean("migrate_chapters", true) - - fun migrateTracks() = rxPrefs.getBoolean("migrate_tracks", true) - - fun migrateCategories() = rxPrefs.getBoolean("migrate_categories", true) + fun migrateFlags() = rxPrefs.getInteger("migrate_flags", Int.MAX_VALUE) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/migration/MigrationFlags.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/migration/MigrationFlags.kt new file mode 100644 index 000000000..f47fd63d3 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/migration/MigrationFlags.kt @@ -0,0 +1,38 @@ +package eu.kanade.tachiyomi.ui.migration + +import eu.kanade.tachiyomi.R + +object MigrationFlags { + + private const val CHAPTERS = 0b001 + private const val CATEGORIES = 0b010 + private const val TRACK = 0b100 + + private const val CHAPTERS2 = 0x1 + private const val CATEGORIES2 = 0x2 + private const val TRACK2 = 0x4 + + val titles get() = arrayOf(R.string.chapters, R.string.categories, R.string.track) + + val flags get() = arrayOf(CHAPTERS, CATEGORIES, TRACK) + + fun hasChapters(value: Int): Boolean { + return value and CHAPTERS != 0 + } + + fun hasCategories(value: Int): Boolean { + return value and CATEGORIES != 0 + } + + fun hasTracks(value: Int): Boolean { + return value and TRACK != 0 + } + + fun getEnabledFlagsPositions(value: Int): List { + return flags.mapIndexedNotNull { index, flag -> if (value and flag != 0) index else null } + } + + fun getFlagsFromPositions(positions: Array): Int { + return positions.fold(0, { accumulated, position -> accumulated or (1 shl position) }) + } +} diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/migration/MigrationPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/migration/MigrationPresenter.kt index c17fbee8b..b8b5f7155 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/migration/MigrationPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/migration/MigrationPresenter.kt @@ -97,9 +97,14 @@ class MigrationPresenter( private fun migrateMangaInternal(source: Source, sourceChapters: List, prevManga: Manga, manga: Manga, replace: Boolean) { + val flags = preferences.migrateFlags().getOrDefault() + val migrateChapters = MigrationFlags.hasChapters(flags) + val migrateCategories = MigrationFlags.hasCategories(flags) + val migrateTracks = MigrationFlags.hasTracks(flags) + db.inTransaction { // Update chapters read - if (preferences.migrateChapters().getOrDefault()) { + if (migrateChapters) { try { syncChaptersWithSource(db, sourceChapters, manga, source) } catch (e: Exception) { @@ -120,13 +125,13 @@ class MigrationPresenter( } } // Update categories - if (preferences.migrateCategories().getOrDefault()) { + if (migrateCategories) { val categories = db.getCategoriesForManga(prevManga).executeAsBlocking() val mangaCategories = categories.map { MangaCategory.create(manga, it) } db.setMangaCategories(mangaCategories, listOf(manga)) } // Update track - if (preferences.migrateTracks().getOrDefault()) { + if (migrateTracks) { val tracks = db.getTracks(prevManga).executeAsBlocking() for (track in tracks) { track.id = null diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/migration/SearchController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/migration/SearchController.kt index 5cde04ccd..d8299cada 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/migration/SearchController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/migration/SearchController.kt @@ -64,31 +64,19 @@ class SearchController( private val preferences: PreferencesHelper by injectLazy() override fun onCreateDialog(savedViewState: Bundle?): Dialog { - val optionTitles = arrayOf( - R.string.chapters, - R.string.categories, - R.string.track - ) + val prefValue = preferences.migrateFlags().getOrDefault() - val optionPrefs = arrayOf( - preferences.migrateChapters(), - preferences.migrateCategories(), - preferences.migrateTracks() - ) - - val preselected = optionPrefs.mapIndexedNotNull { index, preference -> - if (preference.getOrDefault()) index else null - } + val preselected = MigrationFlags.getEnabledFlagsPositions(prefValue) return MaterialDialog.Builder(activity!!) .content(R.string.migration_dialog_what_to_include) - .items(optionTitles.map { resources?.getString(it) }) + .items(MigrationFlags.titles.map { resources?.getString(it) }) .alwaysCallMultiChoiceCallback() .itemsCallbackMultiChoice(preselected.toTypedArray(), { _, positions, _ -> // Save current settings for the next time - optionPrefs.forEachIndexed { index, preference -> - preference.set(index in positions) - } + val newValue = MigrationFlags.getFlagsFromPositions(positions) + preferences.migrateFlags().set(newValue) + true }) .positiveText(R.string.migrate)