From 43e801130e650ac11006cc21e0ce141e77b2d430 Mon Sep 17 00:00:00 2001 From: Jay Date: Tue, 14 Jan 2020 02:33:47 -0800 Subject: [PATCH] Made settings use material dialog box Even the list preference in extensions details ...this was a nightmare --- .../data/preference/PreferencesHelper.kt | 4 + .../extension/ExtensionDetailsController.kt | 15 ++++ .../tachiyomi/ui/setting/PreferenceDSL.kt | 36 +++++---- .../ui/setting/SettingsBackupController.kt | 16 ++-- .../ui/setting/SettingsDownloadController.kt | 14 ++-- .../ui/setting/SettingsGeneralController.kt | 46 ++++++++--- .../ui/setting/SettingsLibraryController.kt | 38 ++++----- .../ui/setting/SettingsReaderController.kt | 34 ++++---- .../widget/preference/IntListMatPreference.kt | 60 ++++++++++++++ .../widget/preference/ListMatPreference.kt | 79 +++++++++++++++++++ .../preference/MultiListMatPreference.kt | 59 ++++++++++++++ app/src/main/res/values-night-v29/themes.xml | 1 + app/src/main/res/values-v29/themes.xml | 1 + 13 files changed, 318 insertions(+), 85 deletions(-) create mode 100644 app/src/main/java/eu/kanade/tachiyomi/widget/preference/IntListMatPreference.kt create mode 100644 app/src/main/java/eu/kanade/tachiyomi/widget/preference/ListMatPreference.kt create mode 100644 app/src/main/java/eu/kanade/tachiyomi/widget/preference/MultiListMatPreference.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 f2d952b6a2..e99c4d182a 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 @@ -31,6 +31,10 @@ class PreferencesHelper(val context: Context) { File(Environment.getExternalStorageDirectory().absolutePath + File.separator + context.getString(R.string.app_name), "backup")) + fun getInt(key: String, default: Int?) = rxPrefs.getInteger(key, default) + fun getStringPref(key: String, default: String?) = rxPrefs.getString(key, default) + fun getStringSet(key: String, default: Set) = rxPrefs.getStringSet(key, default) + fun startScreen() = prefs.getInt(Keys.startScreen, 1) fun clear() = prefs.edit().clear().apply() diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/extension/ExtensionDetailsController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/extension/ExtensionDetailsController.kt index 862f42add2..ecd84260bf 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/extension/ExtensionDetailsController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/extension/ExtensionDetailsController.kt @@ -1,6 +1,7 @@ package eu.kanade.tachiyomi.ui.extension import android.annotation.SuppressLint +import android.app.Dialog import android.content.Context import android.os.Bundle import android.util.TypedValue @@ -31,6 +32,7 @@ import eu.kanade.tachiyomi.ui.base.controller.NucleusController import eu.kanade.tachiyomi.ui.setting.preferenceCategory import eu.kanade.tachiyomi.util.LocaleHelper import eu.kanade.tachiyomi.util.RecyclerWindowInsetsListener +import eu.kanade.tachiyomi.widget.preference.ListMatPreference import eu.kanade.tachiyomi.widget.preference.LoginPreference import eu.kanade.tachiyomi.widget.preference.SourceLoginDialog import kotlinx.android.synthetic.main.extension_detail_controller.* @@ -154,6 +156,7 @@ class ExtensionDetailsController(bundle: Bundle? = null) : while (newScreen.preferenceCount != 0) { val pref = newScreen.getPreference(0) pref.preferenceDataStore = dataStore + pref.fragment = "source_${source.id}" pref.order = Int.MAX_VALUE // reset to default order newScreen.removePreference(pref) @@ -177,6 +180,18 @@ class ExtensionDetailsController(bundle: Bundle? = null) : screen.getPreference(it) === preference } + if (preference is ListPreference) { + ListMatPreference(preference.context).apply { + key = preference.key + sharedPref = preference.fragment + otherPref = preference + preferenceDataStore = preference.preferenceDataStore + entries = preference.entries.mapNotNull { it.toString() } + entryValues = preference.entryValues.mapNotNull { it.toString() } + }.dialog().show() + return + } + val f = when (preference) { is EditTextPreference -> EditTextPreferenceDialogController .newInstance(preference.getKey()) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/PreferenceDSL.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/PreferenceDSL.kt index a92e603d3e..bd5c572503 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/PreferenceDSL.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/PreferenceDSL.kt @@ -3,7 +3,18 @@ package eu.kanade.tachiyomi.ui.setting import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat import androidx.core.graphics.drawable.DrawableCompat import androidx.preference.* +import com.afollestad.materialdialogs.MaterialDialog +import com.afollestad.materialdialogs.list.listItems +import com.afollestad.materialdialogs.list.listItemsSingleChoice +import eu.kanade.tachiyomi.data.preference.PreferencesHelper +import eu.kanade.tachiyomi.data.preference.getOrDefault +import eu.kanade.tachiyomi.widget.preference.IntListMatPreference import eu.kanade.tachiyomi.widget.preference.IntListPreference +import eu.kanade.tachiyomi.widget.preference.ListMatPreference +import eu.kanade.tachiyomi.widget.preference.MultiListMatPreference +import uy.kohesive.injekt.Injekt +import uy.kohesive.injekt.api.get +import uy.kohesive.injekt.injectLazy @DslMarker @Target(AnnotationTarget.TYPE) @@ -29,16 +40,19 @@ inline fun PreferenceGroup.editTextPreference(block: (@DSL EditTextPreference).( return initThenAdd(EditTextPreference(context), block).also(::initDialog) } -inline fun PreferenceGroup.listPreference(block: (@DSL ListPreference).() -> Unit): ListPreference { - return initThenAdd(ListPreference(context), block).also(::initDialog) +inline fun PreferenceGroup.listPreference(block: (@DSL ListMatPreference).() -> Unit): + ListMatPreference { + return initThenAdd(ListMatPreference(context), block) } -inline fun PreferenceGroup.intListPreference(block: (@DSL IntListPreference).() -> Unit): IntListPreference { - return initThenAdd(IntListPreference(context), block).also(::initDialog) +inline fun PreferenceGroup.intListPreference(block: (@DSL IntListMatPreference).() -> Unit): + IntListMatPreference { + return initThenAdd(IntListMatPreference(context), block) } -inline fun PreferenceGroup.multiSelectListPreference(block: (@DSL MultiSelectListPreference).() -> Unit): MultiSelectListPreference { - return initThenAdd(MultiSelectListPreference(context), block).also(::initDialog) +inline fun PreferenceGroup.multiSelectListPreferenceMat(block: (@DSL MultiListMatPreference).() +-> Unit): MultiListMatPreference { + return initThenAdd(MultiListMatPreference(context), block) } inline fun PreferenceScreen.preferenceCategory(block: (@DSL PreferenceCategory).() -> Unit): PreferenceCategory { @@ -62,7 +76,7 @@ fun initDialog(dialogPreference: DialogPreference) { inline fun

PreferenceGroup.initThenAdd(p: P, block: P.() -> Unit): P { return p.apply { block() - this.isIconSpaceReserved = false + this.isIconSpaceReserved = false addPreference(this) } } @@ -102,11 +116,3 @@ var Preference.summaryRes: Int var Preference.iconTint: Int get() = 0 // set only set(value) { DrawableCompat.setTint(icon, value) } - -var ListPreference.entriesRes: Array - get() = emptyArray() // set only - set(value) { entries = value.map { context.getString(it) }.toTypedArray() } - -var MultiSelectListPreference.entriesRes: Array - get() = emptyArray() // set only - set(value) { entries = value.map { context.getString(it) }.toTypedArray() } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBackupController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBackupController.kt index bee201d4fa..1cbaebae17 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBackupController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBackupController.kt @@ -22,8 +22,6 @@ import eu.kanade.tachiyomi.ui.base.controller.DialogController import eu.kanade.tachiyomi.ui.base.controller.popControllerWithTag import eu.kanade.tachiyomi.ui.base.controller.requestPermissionsSafe import eu.kanade.tachiyomi.util.* -import java.io.File -import java.util.concurrent.TimeUnit import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys class SettingsBackupController : SettingsController() { @@ -84,15 +82,14 @@ class SettingsBackupController : SettingsController() { entriesRes = arrayOf(R.string.update_never, R.string.update_6hour, R.string.update_12hour, R.string.update_24hour, R.string.update_48hour, R.string.update_weekly) - entryValues = arrayOf("0", "6", "12", "24", "48", "168") - defaultValue = "0" - summary = "%s" + entryValues = listOf(0, 6, 12, 24, 48, 168) + defaultValue = 0 onChange { newValue -> // Always cancel the previous task, it seems that sometimes they are not updated BackupCreatorJob.cancelTask() - val interval = (newValue as String).toInt() + val interval = newValue as Int if (interval > 0) { BackupCreatorJob.setupTask(interval) } @@ -123,10 +120,9 @@ class SettingsBackupController : SettingsController() { val backupNumber = intListPreference { key = Keys.numberOfBackups titleRes = R.string.pref_backup_slots - entries = arrayOf("1", "2", "3", "4", "5") - entryValues = entries - defaultValue = "1" - summary = "%s" + entries = listOf("1", "2", "3", "4", "5") + entryRange = 1..5 + defaultValue = 1 } preferences.backupInterval().asObservable() 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 41499ef175..fca642e380 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 @@ -65,9 +65,8 @@ class SettingsDownloadController : SettingsController() { entriesRes = arrayOf(R.string.disabled, R.string.last_read_chapter, R.string.second_to_last, R.string.third_to_last, R.string.fourth_to_last, R.string.fifth_to_last) - entryValues = arrayOf("-1", "0", "1", "2", "3", "4") - defaultValue = "-1" - summary = "%s" + entryRange = -1..4 + defaultValue = -1 } } @@ -81,11 +80,12 @@ class SettingsDownloadController : SettingsController() { titleRes = R.string.pref_download_new defaultValue = false } - multiSelectListPreference { + multiSelectListPreferenceMat { key = Keys.downloadNewCategories titleRes = R.string.pref_download_new_categories - entries = dbCategories.map { it.name }.toTypedArray() - entryValues = dbCategories.map { it.id.toString() }.toTypedArray() + entries = dbCategories.map { it.name } + entryValues = dbCategories.map { it.id.toString() } + allSelectionRes = R.string.all preferences.downloadNew().asObservable() .subscribeUntilDestroy { isVisible = it } @@ -96,7 +96,7 @@ class SettingsDownloadController : SettingsController() { .mapNotNull { id -> dbCategories.find { it.id == id.toInt() } } .sortedBy { it.order } - summary = if (selectedCategories.isEmpty()) + customSummary = if (selectedCategories.isEmpty()) resources?.getString(R.string.all) else selectedCategories.joinToString { it.name } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralController.kt index 5a3e2fea58..4337eada69 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralController.kt @@ -7,7 +7,7 @@ import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.preference.getOrDefault import eu.kanade.tachiyomi.data.updater.UpdaterJob import eu.kanade.tachiyomi.util.LocaleHelper -import eu.kanade.tachiyomi.widget.preference.IntListPreference +import eu.kanade.tachiyomi.widget.preference.IntListMatPreference import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys class SettingsGeneralController : SettingsController() { @@ -20,14 +20,14 @@ class SettingsGeneralController : SettingsController() { listPreference { key = Keys.lang titleRes = R.string.pref_language - entryValues = arrayOf("", "ar", "bg", "bn", "ca", "cs", "de", "el", "en-US", "en-GB", + entryValues = listOf("", "ar", "bg", "bn", "ca", "cs", "de", "el", "en-US", "en-GB", "es", "fr", "hi", "hu", "in", "it", "ja", "ko", "lv", "ms", "nb-rNO", "nl", "pl", "pt", "pt-BR", "ro", "ru", "sc", "sr", "sv", "th", "tl", "tr", "uk", "vi", "zh-rCN") entries = entryValues.map { value -> val locale = LocaleHelper.getLocaleFromString(value.toString()) locale?.getDisplayName(locale)?.capitalize() ?: context.getString(R.string.system_default) - }.toTypedArray() + } defaultValue = "" summary = "%s" @@ -41,6 +41,29 @@ class SettingsGeneralController : SettingsController() { } } intListPreference { + key = Keys.theme + titleRes = R.string.pref_theme + entriesRes = arrayOf(R.string.light_theme, R.string.dark_theme, + R.string.amoled_theme, R.string.darkblue_theme, + R.string.system_theme, R.string.system_amoled_theme, R.string.system_darkblue_theme) + entryRange = 1..7 + defaultValue = 5 + + onChange { + activity?.recreate() + true + } + } + intListPreference { + key = Keys.startScreen + titleRes = R.string.pref_start_screen + entriesRes = arrayOf(R.string.label_library, R.string.label_recent_manga, + R.string.label_recent_updates) + entryRange = 1..3 + defaultValue = 1 + } + + /*intListPreference { key = Keys.theme titleRes = R.string.pref_theme entriesRes = arrayOf(R.string.light_theme, R.string.dark_theme, @@ -63,7 +86,7 @@ class SettingsGeneralController : SettingsController() { entryValues = arrayOf("1", "2", "3") defaultValue = "1" summary = "%s" - } + }*/ switchPreference { key = Keys.automaticUpdates titleRes = R.string.pref_enable_automatic_updates @@ -87,7 +110,7 @@ class SettingsGeneralController : SettingsController() { val biometricManager = BiometricManager.from(context) if (biometricManager.canAuthenticate() == BiometricManager.BIOMETRIC_SUCCESS) { - var preference:IntListPreference? = null + var preference:IntListMatPreference? = null switchPreference { key = Keys.useBiometrics titleRes = R.string.lock_with_biometrics @@ -102,18 +125,17 @@ class SettingsGeneralController : SettingsController() { key = Keys.lockAfter titleRes = R.string.lock_when_idle isVisible = preferences.useBiometrics().getOrDefault() - val values = arrayOf("0", "2", "5", "10", "20", "30", "60", "90", "120", "-1") - entries = values.map { + val values = listOf(0, 2, 5, 10, 20, 30, 60, 90, 120, -1) + entries = values.mapNotNull { when (it) { - "0" -> context.getString(R.string.lock_always) - "-1" -> context.getString(R.string.lock_never) + 0 -> context.getString(R.string.lock_always) + -1 -> context.getString(R.string.lock_never) else -> resources?.getQuantityString(R.plurals.lock_after_mins, it.toInt(), it) } - }.toTypedArray() + } entryValues = values - defaultValue = "0" - summary = "%s" + defaultValue = 0 } } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsLibraryController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsLibraryController.kt index 451fb3518b..402e8f48c2 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsLibraryController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsLibraryController.kt @@ -4,7 +4,6 @@ import android.app.Dialog import android.os.Bundle import android.os.Handler import android.view.View -import androidx.biometric.BiometricManager import androidx.preference.PreferenceScreen import com.afollestad.materialdialogs.MaterialDialog import com.afollestad.materialdialogs.customview.customView @@ -15,8 +14,6 @@ import eu.kanade.tachiyomi.data.library.LibraryUpdateJob import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.preference.getOrDefault import eu.kanade.tachiyomi.ui.base.controller.DialogController -import eu.kanade.tachiyomi.util.LocaleHelper -import eu.kanade.tachiyomi.widget.preference.IntListPreference import kotlinx.android.synthetic.main.pref_library_columns.view.* import rx.Observable import uy.kohesive.injekt.Injekt @@ -60,27 +57,26 @@ class SettingsLibraryController : SettingsController() { entriesRes = arrayOf(R.string.update_never, R.string.update_1hour, R.string.update_2hour, R.string.update_3hour, R.string.update_6hour, R.string.update_12hour, R.string.update_24hour, R.string.update_48hour) - entryValues = arrayOf("0", "1", "2", "3", "6", "12", "24", "48") - defaultValue = "0" - summary = "%s" + entryValues = listOf(0, 1, 2, 3, 6, 12, 24, 48) + defaultValue = 0 onChange { newValue -> // Always cancel the previous task, it seems that sometimes they are not updated. LibraryUpdateJob.cancelTask() - val interval = (newValue as String).toInt() + val interval = newValue as Int if (interval > 0) { LibraryUpdateJob.setupTask(interval) } true } } - multiSelectListPreference { + multiSelectListPreferenceMat { key = Keys.libraryUpdateRestriction titleRes = R.string.pref_library_update_restriction entriesRes = arrayOf(R.string.wifi, R.string.charging) - entryValues = arrayOf("wifi", "ac") - summaryRes = R.string.pref_library_update_restriction_summary + entryValues = listOf("wifi", "ac") + customSummartRes = R.string.pref_library_update_restriction_summary preferences.libraryUpdateInterval().asObservable() .subscribeUntilDestroy { isVisible = it > 0 } @@ -99,11 +95,12 @@ class SettingsLibraryController : SettingsController() { val dbCategories = db.getCategories().executeAsBlocking() - multiSelectListPreference { + multiSelectListPreferenceMat { key = Keys.libraryUpdateCategories titleRes = R.string.pref_library_update_categories - entries = dbCategories.map { it.name }.toTypedArray() - entryValues = dbCategories.map { it.id.toString() }.toTypedArray() + entries = dbCategories.map { it.name } + entryValues = dbCategories.map { it.id.toString() } + allSelectionRes = R.string.all preferences.libraryUpdateCategories().asObservable() .subscribeUntilDestroy { @@ -111,7 +108,7 @@ class SettingsLibraryController : SettingsController() { .mapNotNull { id -> dbCategories.find { it.id == id.toInt() } } .sortedBy { it.order } - summary = if (selectedCategories.isEmpty()) + customSummary = if (selectedCategories.isEmpty()) context.getString(R.string.all) else selectedCategories.joinToString { it.name } @@ -127,11 +124,8 @@ class SettingsLibraryController : SettingsController() { R.string.action_sort_alpha, R.string.action_sort_last_updated ) - entryValues = arrayOf( - "0", - "1" - ) - defaultValue = "0" + entryRange = 0..1 + defaultValue = 0 summaryRes = R.string.pref_library_update_prioritization_summary } intListPreference { @@ -139,16 +133,16 @@ class SettingsLibraryController : SettingsController() { titleRes = R.string.default_category val categories = listOf(Category.createDefault()) + dbCategories - entries = arrayOf(context.getString(R.string.default_category_summary)) + + entries = listOf(context.getString(R.string.default_category_summary)) + categories.map { it.name }.toTypedArray() - entryValues = arrayOf("-1") + categories.map { it.id.toString() }.toTypedArray() + entryValues = listOf(-1) + categories.mapNotNull { it.id }.toList() defaultValue = "-1" val selectedCategory = categories.find { it.id == preferences.defaultCategory() } summary = selectedCategory?.name ?: context.getString(R.string.default_category_summary) onChange { newValue -> summary = categories.find { - it.id == (newValue as String).toInt() + it.id == newValue as Int }?.name ?: context.getString(R.string.default_category_summary) true } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsReaderController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsReaderController.kt index 93c2a1e674..2e603c6b78 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsReaderController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsReaderController.kt @@ -15,9 +15,8 @@ class SettingsReaderController : SettingsController() { titleRes = R.string.pref_viewer_type entriesRes = arrayOf(R.string.left_to_right_viewer, R.string.right_to_left_viewer, R.string.vertical_viewer, R.string.webtoon_viewer) - entryValues = arrayOf("1", "2", "3", "4") - defaultValue = "1" - summary = "%s" + entryRange = 1..4 + defaultValue = 1 } intListPreference { key = Keys.imageScaleType @@ -25,44 +24,41 @@ class SettingsReaderController : SettingsController() { entriesRes = arrayOf(R.string.scale_type_fit_screen, R.string.scale_type_stretch, R.string.scale_type_fit_width, R.string.scale_type_fit_height, R.string.scale_type_original_size, R.string.scale_type_smart_fit) - entryValues = arrayOf("1", "2", "3", "4", "5", "6") - defaultValue = "1" - summary = "%s" + entryRange = 1..6 + defaultValue = 1 } intListPreference { key = Keys.zoomStart titleRes = R.string.pref_zoom_start entriesRes = arrayOf(R.string.zoom_start_automatic, R.string.zoom_start_left, R.string.zoom_start_right, R.string.zoom_start_center) - entryValues = arrayOf("1", "2", "3", "4") - defaultValue = "1" - summary = "%s" + entryRange = 1..4 + defaultValue = 1 } intListPreference { key = Keys.rotation titleRes = R.string.pref_rotation_type entriesRes = arrayOf(R.string.rotation_free, R.string.rotation_lock, R.string.rotation_force_portrait, R.string.rotation_force_landscape) - entryValues = arrayOf("1", "2", "3", "4") - defaultValue = "1" - summary = "%s" + entryRange = 1..4 + defaultValue = 1 } intListPreference { key = Keys.readerTheme titleRes = R.string.pref_reader_theme entriesRes = arrayOf(R.string.white_background, R.string.black_background, R.string .reader_theme_smart, R.string.reader_theme_smart_theme) - entryValues = arrayOf("0", "1", "2", "3") - defaultValue = "0" - summary = "%s" + entryRange = 0..3 + defaultValue = 2 } intListPreference { key = Keys.doubleTapAnimationSpeed titleRes = R.string.pref_double_tap_anim_speed - entries = arrayOf(context.getString(R.string.double_tap_anim_speed_0), context.getString(R.string.double_tap_anim_speed_fast), context.getString(R.string.double_tap_anim_speed_normal)) - entryValues = arrayOf("1", "250", "500") // using a value of 0 breaks the image viewer, so min is 1 - defaultValue = "500" - summary = "%s" + entries = listOf(context.getString(R.string.double_tap_anim_speed_0), context.getString(R + .string.double_tap_anim_speed_fast), context.getString(R.string.double_tap_anim_speed_normal)) + entryValues = listOf(1, 250, 500) // using a value of 0 breaks the image viewer, so + // min is 1 + defaultValue = 500 } switchPreference { key = Keys.skipRead diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/preference/IntListMatPreference.kt b/app/src/main/java/eu/kanade/tachiyomi/widget/preference/IntListMatPreference.kt new file mode 100644 index 0000000000..a372c3a479 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/widget/preference/IntListMatPreference.kt @@ -0,0 +1,60 @@ +package eu.kanade.tachiyomi.widget.preference + +import android.content.Context +import android.util.AttributeSet +import androidx.preference.Preference +import com.afollestad.materialdialogs.MaterialDialog +import com.afollestad.materialdialogs.list.listItemsSingleChoice +import eu.kanade.tachiyomi.data.preference.PreferencesHelper +import eu.kanade.tachiyomi.data.preference.getOrDefault +import eu.kanade.tachiyomi.ui.setting.defaultValue +import uy.kohesive.injekt.Injekt +import uy.kohesive.injekt.api.get + +class IntListMatPreference @JvmOverloads constructor(context: Context, attrs: AttributeSet? = + null) : + Preference(context, attrs) { + private val prefs: PreferencesHelper = Injekt.get() + + var entryValues:List = emptyList() + var entryRange:IntRange + get() = 0..0 + set(value) { entryValues = value.toList() } + var entriesRes:Array + get() = emptyArray() + set(value) { entries = value.map { context.getString(it) } } + private var defValue:Int = 0 + var entries:List = emptyList() + + override fun onSetInitialValue(defaultValue: Any?) { + super.onSetInitialValue(defaultValue) + defValue = defaultValue as? Int ?: defValue + } + override fun getSummary(): CharSequence { + val index = entryValues.indexOf(prefs.getInt(key, defValue).getOrDefault()) + return if (entries.isEmpty() || index == -1) "" + else entries[index] + } + + override fun onClick() { + dialog().show() + } + + fun dialog(): MaterialDialog { + return MaterialDialog(context).apply { + title(text = title.toString()) + negativeButton(android.R.string.cancel) + val default = entryValues.indexOf(prefs.getInt(key, defValue).getOrDefault()) + listItemsSingleChoice(items = entries, + waitForPositiveButton = false, + initialSelection = default) { + _, pos, _ -> + val value = entryValues[pos] + prefs.getInt(key, defValue).set(value) + callChangeListener(value) + this@IntListMatPreference.summary = this@IntListMatPreference.summary + dismiss() + } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/preference/ListMatPreference.kt b/app/src/main/java/eu/kanade/tachiyomi/widget/preference/ListMatPreference.kt new file mode 100644 index 0000000000..7b742d6a25 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/widget/preference/ListMatPreference.kt @@ -0,0 +1,79 @@ +package eu.kanade.tachiyomi.widget.preference + +import android.content.Context +import android.content.SharedPreferences +import android.util.AttributeSet +import androidx.preference.Preference +import androidx.preference.PreferenceManager +import com.afollestad.materialdialogs.MaterialDialog +import com.afollestad.materialdialogs.list.listItemsSingleChoice +import eu.kanade.tachiyomi.data.preference.PreferencesHelper +import eu.kanade.tachiyomi.data.preference.getOrDefault +import eu.kanade.tachiyomi.ui.setting.defaultValue +import uy.kohesive.injekt.Injekt +import uy.kohesive.injekt.api.get + +open class ListMatPreference @JvmOverloads constructor(context: Context, attrs: AttributeSet? = + null) : + Preference(context, attrs) { + protected val prefs: PreferencesHelper = Injekt.get() + + var sharedPref:String? = null + var otherPref:Preference? = null + var entryValues:List = emptyList() + var entriesRes:Array + get() = emptyArray() + set(value) { entries = value.map { context.getString(it) } } + protected var defValue:String = "" + var entries:List = emptyList() + + override fun onSetInitialValue(defaultValue: Any?) { + super.onSetInitialValue(defaultValue) + defValue = defaultValue as? String ?: defValue + } + override fun getSummary(): CharSequence { + val index = entryValues.indexOf(prefs.getStringPref(key, defValue).getOrDefault()) + return if (entries.isEmpty() || index == -1) "" + else entries[index] + } + + override fun onClick() { + dialog().show() + } + + open fun dialog(): MaterialDialog { + return MaterialDialog(context).apply { + if (this@ListMatPreference.title != null) + title(text = this@ListMatPreference.title.toString()) + negativeButton(android.R.string.cancel) + val default = entryValues.indexOf(if (sharedPref != null) { + val settings = context.getSharedPreferences(sharedPref, Context.MODE_PRIVATE) + settings.getString(key, "") + } + else prefs.getStringPref(key, defValue).getOrDefault()) + listItemsSingleChoice(items = entries, + waitForPositiveButton = false, + initialSelection = default) { _, pos, _ -> + val value = entryValues[pos] + if (sharedPref != null) { + val oldDef = if (default > -1) entries[default] else "" + val settings = context.getSharedPreferences(sharedPref, Context.MODE_PRIVATE) + val edit = settings.edit() + edit.putString(key, value) + edit.apply() + otherPref?.callChangeListener(value) + if (oldDef == otherPref?.summary || otherPref?.summary.isNullOrEmpty()) otherPref?.summary = + entries[pos] + else otherPref?.summary = otherPref?.summary?.toString()?.replace(oldDef, + entries[pos] + ) ?: entries[pos] + } + else { + prefs.getStringPref(key, defValue).set(value) + this@ListMatPreference.summary = this@ListMatPreference.summary + } + dismiss() + } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/preference/MultiListMatPreference.kt b/app/src/main/java/eu/kanade/tachiyomi/widget/preference/MultiListMatPreference.kt new file mode 100644 index 0000000000..a02307113d --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/widget/preference/MultiListMatPreference.kt @@ -0,0 +1,59 @@ +package eu.kanade.tachiyomi.widget.preference + +import android.content.Context +import android.util.AttributeSet +import com.afollestad.materialdialogs.MaterialDialog +import com.afollestad.materialdialogs.list.checkItem +import com.afollestad.materialdialogs.list.isItemChecked +import com.afollestad.materialdialogs.list.listItemsMultiChoice +import com.afollestad.materialdialogs.list.uncheckItem +import eu.kanade.tachiyomi.data.preference.getOrDefault + +class MultiListMatPreference @JvmOverloads constructor(context: Context, attrs: AttributeSet? = + null) : + ListMatPreference(context, attrs) { + + var allSelectionRes:Int? = null + var customSummartRes:Int + get() = 0 + set(value) { customSummary = context.getString(value) } + var customSummary:String? = null + var positions:IntArray? = null + + override fun getSummary(): CharSequence { + return customSummary ?: super.getSummary() + } + + override fun dialog():MaterialDialog { + return MaterialDialog(context).apply { + title(text = title.toString()) + negativeButton(android.R.string.cancel) + positiveButton(android.R.string.ok) { + var value = positions?.map { + entryValues[it - if (allSelectionRes != null) 1 else 0] }?.toSet() ?: emptySet() + if (allSelectionRes != null && isItemChecked(0)) value = emptySet() + prefs.getStringSet(key, emptySet()).set(value) + callChangeListener(value) + this@MultiListMatPreference.summary = this@MultiListMatPreference.summary + } + val set = prefs.getStringSet(key, emptySet()).getOrDefault() + var default = set.map { + entryValues.indexOf(it) + if (allSelectionRes != null) 1 else 0 } + .toIntArray() + if (allSelectionRes != null && default.isEmpty()) default = intArrayOf(0) + val items = if (allSelectionRes != null) + (listOf(context.getString(allSelectionRes!!)) + entries) else entries + listItemsMultiChoice(items = items, + allowEmptySelection = true, + disabledIndices = if (allSelectionRes != null) intArrayOf(0) else null, + waitForPositiveButton = false, + initialSelection = default) { _, pos, _ -> + positions = pos + if (allSelectionRes != null) { + if (pos.isEmpty()) checkItem(0) + else uncheckItem(0) + } + } + } + } +} \ No newline at end of file diff --git a/app/src/main/res/values-night-v29/themes.xml b/app/src/main/res/values-night-v29/themes.xml index 71bbc144ab..dd09264394 100644 --- a/app/src/main/res/values-night-v29/themes.xml +++ b/app/src/main/res/values-night-v29/themes.xml @@ -3,5 +3,6 @@ \ No newline at end of file diff --git a/app/src/main/res/values-v29/themes.xml b/app/src/main/res/values-v29/themes.xml index 056509289b..21dc9274b4 100644 --- a/app/src/main/res/values-v29/themes.xml +++ b/app/src/main/res/values-v29/themes.xml @@ -5,5 +5,6 @@ \ No newline at end of file