From b2fee7035ff6ae9523881e1b51292eeb78fe0c36 Mon Sep 17 00:00:00 2001 From: arkon Date: Sat, 3 Apr 2021 16:13:12 -0400 Subject: [PATCH] Use Material Dialogs for auto-download categories preference To allow for negative selections in the future. --- .../ui/setting/SettingsDownloadController.kt | 39 +++++++++++++++++-- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsDownloadController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsDownloadController.kt index ecb74d32e..efec6479f 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsDownloadController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsDownloadController.kt @@ -10,6 +10,7 @@ import androidx.core.content.ContextCompat import androidx.core.net.toUri import androidx.preference.PreferenceScreen import com.afollestad.materialdialogs.MaterialDialog +import com.afollestad.materialdialogs.list.listItemsMultiChoice import com.afollestad.materialdialogs.list.listItemsSingleChoice import com.hippo.unifile.UniFile import eu.kanade.tachiyomi.R @@ -21,7 +22,6 @@ import eu.kanade.tachiyomi.ui.base.controller.DialogController import eu.kanade.tachiyomi.util.preference.defaultValue import eu.kanade.tachiyomi.util.preference.entriesRes import eu.kanade.tachiyomi.util.preference.intListPreference -import eu.kanade.tachiyomi.util.preference.multiSelectListPreference import eu.kanade.tachiyomi.util.preference.onClick import eu.kanade.tachiyomi.util.preference.preference import eu.kanade.tachiyomi.util.preference.preferenceCategory @@ -105,11 +105,12 @@ class SettingsDownloadController : SettingsController() { titleRes = R.string.pref_download_new defaultValue = false } - multiSelectListPreference { + preference { key = Keys.downloadNewCategories titleRes = R.string.pref_download_new_categories - entries = categories.map { it.name }.toTypedArray() - entryValues = categories.map { it.id.toString() }.toTypedArray() + onClick { + DownloadCategoriesDialog().showDialog(router) + } preferences.downloadNew().asImmediateFlow { isVisible = it } .launchIn(viewScope) @@ -198,6 +199,36 @@ class SettingsDownloadController : SettingsController() { } } + class DownloadCategoriesDialog : DialogController() { + + private val preferences: PreferencesHelper = Injekt.get() + private val db: DatabaseHelper = Injekt.get() + + override fun onCreateDialog(savedViewState: Bundle?): Dialog { + val dbCategories = db.getCategories().executeAsBlocking() + val categories = listOf(Category.createDefault()) + dbCategories + + val items = categories.map { it.name } + val preselected = categories + .filter { it.id.toString() in preferences.downloadNewCategories().get() } + .map { categories.indexOf(it) } + .toIntArray() + + return MaterialDialog(activity!!) + .title(R.string.pref_download_new_categories) + .listItemsMultiChoice( + items = items, + initialSelection = preselected, + allowEmptySelection = true + ) { _, selections, _ -> + val newCategories = selections.map { categories[it] } + preferences.downloadNewCategories().set(newCategories.map { it.id.toString() }.toSet()) + } + .positiveButton(android.R.string.ok) + .negativeButton(android.R.string.cancel) + } + } + private companion object { const val DOWNLOAD_DIR = 104 }