From a94d455e79d4a5143fad8d860107044192581b69 Mon Sep 17 00:00:00 2001 From: Jay Date: Sun, 19 Jan 2020 17:42:11 -0800 Subject: [PATCH] Fixed scrollbar in preference popup --- .../extension/ExtensionDetailsController.kt | 2 +- .../tachiyomi/ui/setting/PreferenceDSL.kt | 16 +++-- .../ui/setting/SettingsBackupController.kt | 4 +- .../ui/setting/SettingsDownloadController.kt | 4 +- .../ui/setting/SettingsGeneralController.kt | 8 +-- .../ui/setting/SettingsLibraryController.kt | 10 +-- .../ui/setting/SettingsReaderController.kt | 12 ++-- .../widget/preference/IntListMatPreference.kt | 20 +++--- .../widget/preference/ListMatPreference.kt | 72 +++++++++---------- .../widget/preference/MatPreference.kt | 39 ++++++++++ .../preference/MultiListMatPreference.kt | 64 ++++++++--------- 11 files changed, 145 insertions(+), 106 deletions(-) create mode 100644 app/src/main/java/eu/kanade/tachiyomi/widget/preference/MatPreference.kt 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 ecd84260bf..5a0e65c2d0 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 @@ -181,7 +181,7 @@ class ExtensionDetailsController(bundle: Bundle? = null) : } if (preference is ListPreference) { - ListMatPreference(preference.context).apply { + ListMatPreference(activity, preference.context).apply { key = preference.key sharedPref = preference.fragment otherPref = preference 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 bd5c572503..58037acb7f 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 @@ -1,5 +1,6 @@ package eu.kanade.tachiyomi.ui.setting +import android.app.Activity import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat import androidx.core.graphics.drawable.DrawableCompat import androidx.preference.* @@ -40,19 +41,22 @@ inline fun PreferenceGroup.editTextPreference(block: (@DSL EditTextPreference).( return initThenAdd(EditTextPreference(context), block).also(::initDialog) } -inline fun PreferenceGroup.listPreference(block: (@DSL ListMatPreference).() -> Unit): +inline fun PreferenceGroup.listPreference(activity: Activity?, block: (@DSL ListMatPreference).() +-> Unit): ListMatPreference { - return initThenAdd(ListMatPreference(context), block) + return initThenAdd(ListMatPreference(activity, context), block) } -inline fun PreferenceGroup.intListPreference(block: (@DSL IntListMatPreference).() -> Unit): +inline fun PreferenceGroup.intListPreference(activity: Activity?, block: (@DSL +IntListMatPreference).() -> Unit): IntListMatPreference { - return initThenAdd(IntListMatPreference(context), block) + return initThenAdd(IntListMatPreference(activity, context), block) } -inline fun PreferenceGroup.multiSelectListPreferenceMat(block: (@DSL MultiListMatPreference).() +inline fun PreferenceGroup.multiSelectListPreferenceMat(activity: Activity?, block: (@DSL +MultiListMatPreference).() -> Unit): MultiListMatPreference { - return initThenAdd(MultiListMatPreference(context), block) + return initThenAdd(MultiListMatPreference(activity, context), block) } inline fun PreferenceScreen.preferenceCategory(block: (@DSL PreferenceCategory).() -> Unit): PreferenceCategory { 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 3b68bf8b19..4086681b9e 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 @@ -76,7 +76,7 @@ class SettingsBackupController : SettingsController() { preferenceCategory { titleRes = R.string.pref_backup_service_category - intListPreference { + intListPreference(activity) { key = Keys.backupInterval titleRes = R.string.pref_backup_interval entriesRes = arrayOf(R.string.update_never, R.string.update_6hour, @@ -117,7 +117,7 @@ class SettingsBackupController : SettingsController() { summary = dir.filePath + "/automatic" } } - val backupNumber = intListPreference { + val backupNumber = intListPreference(activity) { key = Keys.numberOfBackups titleRes = R.string.pref_backup_slots entries = listOf("1", "2", "3", "4", "5") 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 fca642e380..7e28998694 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 @@ -59,7 +59,7 @@ class SettingsDownloadController : SettingsController() { titleRes = R.string.pref_remove_after_marked_as_read defaultValue = false } - intListPreference { + intListPreference(activity) { key = Keys.removeAfterReadSlots titleRes = R.string.pref_remove_after_read entriesRes = arrayOf(R.string.disabled, R.string.last_read_chapter, @@ -80,7 +80,7 @@ class SettingsDownloadController : SettingsController() { titleRes = R.string.pref_download_new defaultValue = false } - multiSelectListPreferenceMat { + multiSelectListPreferenceMat(activity) { key = Keys.downloadNewCategories titleRes = R.string.pref_download_new_categories entries = dbCategories.map { 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 0c4b96b92e..f32c632162 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 @@ -17,7 +17,7 @@ class SettingsGeneralController : SettingsController() { override fun setupPreferenceScreen(screen: PreferenceScreen) = with(screen) { titleRes = R.string.pref_category_general - listPreference { + listPreference(activity) { key = Keys.lang titleRes = R.string.pref_language entryValues = listOf("", "ar", "bg", "bn", "ca", "cs", "de", "el", "en-US", "en-GB", @@ -40,7 +40,7 @@ class SettingsGeneralController : SettingsController() { true } } - intListPreference { + intListPreference(activity) { key = Keys.theme titleRes = R.string.pref_theme entriesRes = arrayOf(R.string.light_theme, R.string.dark_theme, @@ -54,7 +54,7 @@ class SettingsGeneralController : SettingsController() { true } } - intListPreference { + intListPreference(activity) { key = Keys.startScreen titleRes = R.string.pref_start_screen entriesRes = arrayOf(R.string.label_library, R.string.label_recent_manga, @@ -96,7 +96,7 @@ class SettingsGeneralController : SettingsController() { true } } - preference = intListPreference { + preference = intListPreference(activity) { key = Keys.lockAfter titleRes = R.string.lock_when_idle isVisible = preferences.useBiometrics().getOrDefault() 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 d9f7a16b17..7de68a9c43 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 @@ -51,7 +51,7 @@ class SettingsLibraryController : SettingsController() { "${context.getString(R.string.landscape)}: $landscape" } } - intListPreference { + intListPreference(activity) { key = Keys.libraryUpdateInterval titleRes = R.string.pref_library_update_interval entriesRes = arrayOf(R.string.update_never, R.string.update_1hour, @@ -71,7 +71,7 @@ class SettingsLibraryController : SettingsController() { true } } - multiSelectListPreferenceMat { + multiSelectListPreferenceMat(activity) { key = Keys.libraryUpdateRestriction titleRes = R.string.pref_library_update_restriction entriesRes = arrayOf(R.string.wifi, R.string.charging) @@ -95,7 +95,7 @@ class SettingsLibraryController : SettingsController() { val dbCategories = db.getCategories().executeAsBlocking() - multiSelectListPreferenceMat { + multiSelectListPreferenceMat(activity) { key = Keys.libraryUpdateCategories titleRes = R.string.pref_library_update_categories entries = dbCategories.map { it.name } @@ -114,7 +114,7 @@ class SettingsLibraryController : SettingsController() { selectedCategories.joinToString { it.name } } } - intListPreference { + intListPreference(activity) { key = Keys.libraryUpdatePrioritization titleRes = R.string.pref_library_update_prioritization @@ -128,7 +128,7 @@ class SettingsLibraryController : SettingsController() { defaultValue = 0 summaryRes = R.string.pref_library_update_prioritization_summary } - intListPreference { + intListPreference(activity) { key = Keys.defaultCategory titleRes = R.string.default_category 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 2e603c6b78..21429c7fc2 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 @@ -10,7 +10,7 @@ class SettingsReaderController : SettingsController() { override fun setupPreferenceScreen(screen: PreferenceScreen) = with(screen) { titleRes = R.string.pref_category_reader - intListPreference { + intListPreference(activity) { key = Keys.defaultViewer titleRes = R.string.pref_viewer_type entriesRes = arrayOf(R.string.left_to_right_viewer, R.string.right_to_left_viewer, @@ -18,7 +18,7 @@ class SettingsReaderController : SettingsController() { entryRange = 1..4 defaultValue = 1 } - intListPreference { + intListPreference(activity) { key = Keys.imageScaleType titleRes = R.string.pref_image_scale_type entriesRes = arrayOf(R.string.scale_type_fit_screen, R.string.scale_type_stretch, @@ -27,7 +27,7 @@ class SettingsReaderController : SettingsController() { entryRange = 1..6 defaultValue = 1 } - intListPreference { + intListPreference(activity) { key = Keys.zoomStart titleRes = R.string.pref_zoom_start entriesRes = arrayOf(R.string.zoom_start_automatic, R.string.zoom_start_left, @@ -35,7 +35,7 @@ class SettingsReaderController : SettingsController() { entryRange = 1..4 defaultValue = 1 } - intListPreference { + intListPreference(activity) { key = Keys.rotation titleRes = R.string.pref_rotation_type entriesRes = arrayOf(R.string.rotation_free, R.string.rotation_lock, @@ -43,7 +43,7 @@ class SettingsReaderController : SettingsController() { entryRange = 1..4 defaultValue = 1 } - intListPreference { + intListPreference(activity) { key = Keys.readerTheme titleRes = R.string.pref_reader_theme entriesRes = arrayOf(R.string.white_background, R.string.black_background, R.string @@ -51,7 +51,7 @@ class SettingsReaderController : SettingsController() { entryRange = 0..3 defaultValue = 2 } - intListPreference { + intListPreference(activity) { key = Keys.doubleTapAnimationSpeed titleRes = R.string.pref_double_tap_anim_speed entries = listOf(context.getString(R.string.double_tap_anim_speed_0), context.getString(R 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 index a372c3a479..b423304bb8 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/widget/preference/IntListMatPreference.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/widget/preference/IntListMatPreference.kt @@ -1,9 +1,11 @@ package eu.kanade.tachiyomi.widget.preference +import android.app.Activity import android.content.Context import android.util.AttributeSet import androidx.preference.Preference import com.afollestad.materialdialogs.MaterialDialog +import com.afollestad.materialdialogs.callbacks.onDismiss import com.afollestad.materialdialogs.list.listItemsSingleChoice import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.preference.getOrDefault @@ -11,11 +13,11 @@ 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? = +class IntListMatPreference @JvmOverloads constructor(activity: Activity?, context: Context, + attrs: +AttributeSet? = null) : - Preference(context, attrs) { - private val prefs: PreferencesHelper = Injekt.get() - + MatPreference(activity, context, attrs) { var entryValues:List = emptyList() var entryRange:IntRange get() = 0..0 @@ -36,14 +38,8 @@ class IntListMatPreference @JvmOverloads constructor(context: Context, attrs: At else entries[index] } - override fun onClick() { - dialog().show() - } - - fun dialog(): MaterialDialog { - return MaterialDialog(context).apply { - title(text = title.toString()) - negativeButton(android.R.string.cancel) + override fun dialog(): MaterialDialog { + return super.dialog().apply { val default = entryValues.indexOf(prefs.getInt(key, defValue).getOrDefault()) listItemsSingleChoice(items = entries, waitForPositiveButton = false, 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 index 6c78716f62..8fad529a36 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/widget/preference/ListMatPreference.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/widget/preference/ListMatPreference.kt @@ -1,5 +1,7 @@ package eu.kanade.tachiyomi.widget.preference +import android.annotation.SuppressLint +import android.app.Activity import android.content.Context import android.content.SharedPreferences import android.util.AttributeSet @@ -13,10 +15,10 @@ 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? = +open class ListMatPreference @JvmOverloads constructor(activity: Activity?, context: Context, + attrs: AttributeSet? = null) : - Preference(context, attrs) { - protected val prefs: PreferencesHelper = Injekt.get() + MatPreference(activity, context, attrs) { var sharedPref:String? = null var otherPref:Preference? = null @@ -37,44 +39,42 @@ open class ListMatPreference @JvmOverloads constructor(context: Context, attrs: else entries[index] } - override fun onClick() { - dialog().show() + override fun dialog(): MaterialDialog { + return super.dialog().apply { + setItems() + } } - 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) { + @SuppressLint("CheckResult") + open fun MaterialDialog.setItems() { + 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) - settings.getString(key, "") + 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).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 - callChangeListener(value) - } - dismiss() + else { + prefs.getStringPref(key, defValue).set(value) + this@ListMatPreference.summary = this@ListMatPreference.summary + callChangeListener(value) } + dismiss() } } } \ No newline at end of file diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/preference/MatPreference.kt b/app/src/main/java/eu/kanade/tachiyomi/widget/preference/MatPreference.kt new file mode 100644 index 0000000000..491c895165 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/widget/preference/MatPreference.kt @@ -0,0 +1,39 @@ +package eu.kanade.tachiyomi.widget.preference + +import android.app.Activity +import android.content.Context +import android.util.AttributeSet +import androidx.preference.Preference +import com.afollestad.materialdialogs.MaterialDialog +import com.afollestad.materialdialogs.callbacks.onDismiss +import com.afollestad.materialdialogs.list.listItemsSingleChoice +import eu.kanade.tachiyomi.data.preference.PreferencesHelper +import eu.kanade.tachiyomi.data.preference.getOrDefault +import uy.kohesive.injekt.Injekt +import uy.kohesive.injekt.api.get + +open class MatPreference @JvmOverloads constructor(val activity: Activity?, context: Context, + attrs: + AttributeSet? = + null) : + Preference(context, attrs) { + + protected val prefs: PreferencesHelper = Injekt.get() + private var isShowing = false + + override fun onClick() { + if (!isShowing) + dialog().apply { + onDismiss { this@MatPreference.isShowing = false } + }.show() + isShowing = true + } + + open fun dialog(): MaterialDialog { + return MaterialDialog(activity ?: context).apply { + if (title != null) + title(text = title.toString()) + negativeButton(android.R.string.cancel) + } + } +} \ 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 index e8db43651e..18f5301806 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/widget/preference/MultiListMatPreference.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/widget/preference/MultiListMatPreference.kt @@ -1,5 +1,7 @@ package eu.kanade.tachiyomi.widget.preference +import android.annotation.SuppressLint +import android.app.Activity import android.content.Context import android.util.AttributeSet import com.afollestad.materialdialogs.MaterialDialog @@ -9,9 +11,10 @@ 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? = +class MultiListMatPreference @JvmOverloads constructor(activity: Activity?, context: Context, + attrs: AttributeSet? = null) : - ListMatPreference(context, attrs) { + ListMatPreference(activity, context, attrs) { var allSelectionRes:Int? = null var customSummaryRes:Int @@ -24,37 +27,34 @@ class MultiListMatPreference @JvmOverloads constructor(context: Context, attrs: 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) - } - callChangeListener(positions) + @SuppressLint("CheckResult") + override fun MaterialDialog.setItems() { + 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) } + callChangeListener(positions) } } } \ No newline at end of file