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 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)
}

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>,
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

View File

@ -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)