Use a single preference to store migration flags

This commit is contained in:
inorichi 2018-01-13 13:13:03 +01:00
parent 08f6317beb
commit d56ff9592e
4 changed files with 53 additions and 26 deletions

View file

@ -165,10 +165,6 @@ class PreferencesHelper(val context: Context) {
fun defaultCategory() = prefs.getInt(Keys.defaultCategory, -1) fun defaultCategory() = prefs.getInt(Keys.defaultCategory, -1)
fun migrateChapters() = rxPrefs.getBoolean("migrate_chapters", true) fun migrateFlags() = rxPrefs.getInteger("migrate_flags", Int.MAX_VALUE)
fun migrateTracks() = rxPrefs.getBoolean("migrate_tracks", true)
fun migrateCategories() = rxPrefs.getBoolean("migrate_categories", true)
} }

View file

@ -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<Int> {
return flags.mapIndexedNotNull { index, flag -> if (value and flag != 0) index else null }
}
fun getFlagsFromPositions(positions: Array<Int>): Int {
return positions.fold(0, { accumulated, position -> accumulated or (1 shl position) })
}
}

View file

@ -97,9 +97,14 @@ class MigrationPresenter(
private fun migrateMangaInternal(source: Source, sourceChapters: List<SChapter>, private fun migrateMangaInternal(source: Source, sourceChapters: List<SChapter>,
prevManga: Manga, manga: Manga, replace: Boolean) { 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 { db.inTransaction {
// Update chapters read // Update chapters read
if (preferences.migrateChapters().getOrDefault()) { if (migrateChapters) {
try { try {
syncChaptersWithSource(db, sourceChapters, manga, source) syncChaptersWithSource(db, sourceChapters, manga, source)
} catch (e: Exception) { } catch (e: Exception) {
@ -120,13 +125,13 @@ class MigrationPresenter(
} }
} }
// Update categories // Update categories
if (preferences.migrateCategories().getOrDefault()) { if (migrateCategories) {
val categories = db.getCategoriesForManga(prevManga).executeAsBlocking() val categories = db.getCategoriesForManga(prevManga).executeAsBlocking()
val mangaCategories = categories.map { MangaCategory.create(manga, it) } val mangaCategories = categories.map { MangaCategory.create(manga, it) }
db.setMangaCategories(mangaCategories, listOf(manga)) db.setMangaCategories(mangaCategories, listOf(manga))
} }
// Update track // Update track
if (preferences.migrateTracks().getOrDefault()) { if (migrateTracks) {
val tracks = db.getTracks(prevManga).executeAsBlocking() val tracks = db.getTracks(prevManga).executeAsBlocking()
for (track in tracks) { for (track in tracks) {
track.id = null track.id = null

View file

@ -64,31 +64,19 @@ class SearchController(
private val preferences: PreferencesHelper by injectLazy() private val preferences: PreferencesHelper by injectLazy()
override fun onCreateDialog(savedViewState: Bundle?): Dialog { override fun onCreateDialog(savedViewState: Bundle?): Dialog {
val optionTitles = arrayOf( val prefValue = preferences.migrateFlags().getOrDefault()
R.string.chapters,
R.string.categories,
R.string.track
)
val optionPrefs = arrayOf( val preselected = MigrationFlags.getEnabledFlagsPositions(prefValue)
preferences.migrateChapters(),
preferences.migrateCategories(),
preferences.migrateTracks()
)
val preselected = optionPrefs.mapIndexedNotNull { index, preference ->
if (preference.getOrDefault()) index else null
}
return MaterialDialog.Builder(activity!!) return MaterialDialog.Builder(activity!!)
.content(R.string.migration_dialog_what_to_include) .content(R.string.migration_dialog_what_to_include)
.items(optionTitles.map { resources?.getString(it) }) .items(MigrationFlags.titles.map { resources?.getString(it) })
.alwaysCallMultiChoiceCallback() .alwaysCallMultiChoiceCallback()
.itemsCallbackMultiChoice(preselected.toTypedArray(), { _, positions, _ -> .itemsCallbackMultiChoice(preselected.toTypedArray(), { _, positions, _ ->
// Save current settings for the next time // Save current settings for the next time
optionPrefs.forEachIndexed { index, preference -> val newValue = MigrationFlags.getFlagsFromPositions(positions)
preference.set(index in positions) preferences.migrateFlags().set(newValue)
}
true true
}) })
.positiveText(R.string.migrate) .positiveText(R.string.migrate)