From 4014c48c626da812d9e40135941f067ecf08ef03 Mon Sep 17 00:00:00 2001 From: John Leehey Date: Mon, 2 Dec 2019 13:38:33 -0800 Subject: [PATCH] Revert "Remove no-longer-needed FloatListPreference" This reverts commit 19993199db400e0276966dacba5511115e907170. --- .../tachiyomi/ui/setting/PreferenceDSL.kt | 5 ++++ .../ui/setting/SettingsReaderController.kt | 2 +- .../widget/preference/FloatListPreference.kt | 26 +++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/eu/kanade/tachiyomi/widget/preference/FloatListPreference.kt 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 6fc05d1af..138ebf76e 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,6 +3,7 @@ package eu.kanade.tachiyomi.ui.setting import android.support.graphics.drawable.VectorDrawableCompat import android.support.v4.graphics.drawable.DrawableCompat import android.support.v7.preference.* +import eu.kanade.tachiyomi.widget.preference.FloatListPreference import eu.kanade.tachiyomi.widget.preference.IntListPreference @DslMarker @@ -37,6 +38,10 @@ inline fun PreferenceGroup.intListPreference(block: (@DSL IntListPreference).() return initThenAdd(IntListPreference(context), block).also(::initDialog) } +inline fun PreferenceGroup.floatListPreference(block: (@DSL FloatListPreference).() -> Unit): FloatListPreference { + return initThenAdd(FloatListPreference(context), block).also(::initDialog) +} + inline fun PreferenceGroup.multiSelectListPreference(block: (@DSL MultiSelectListPreference).() -> Unit): MultiSelectListPreference { return initThenAdd(MultiSelectListPreference(context), block).also(::initDialog) } 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 901b6c358..e5958fdac 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 @@ -113,7 +113,7 @@ class SettingsReaderController : SettingsController() { defaultValue = false } - intListPreference { + floatListPreference { key = Keys.webtoonMarginRatio titleRes = R.string.pref_reader_theme entriesRes = arrayOf(R.string.webtoon_margin_ratio_0, diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/preference/FloatListPreference.kt b/app/src/main/java/eu/kanade/tachiyomi/widget/preference/FloatListPreference.kt new file mode 100644 index 000000000..fb7d66e63 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/widget/preference/FloatListPreference.kt @@ -0,0 +1,26 @@ +package eu.kanade.tachiyomi.widget.preference + +import android.content.Context +import android.support.v7.preference.ListPreference +import android.util.AttributeSet + +class FloatListPreference @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : + ListPreference(context, attrs) { + + override fun persistString(value: String?): Boolean { + return value != null && persistFloat(value.toFloat()) + } + + override fun getPersistedString(defaultReturnValue: String?): String? { + // When the underlying preference is using a PreferenceDataStore, there's no way (for now) + // to check if a value is in the store, so we use a most likely unused value as workaround + val defaultIntValue = Float.NEGATIVE_INFINITY + + val value = getPersistedFloat(defaultIntValue) + return if (value != defaultIntValue) { + value.toString() + } else { + defaultReturnValue + } + } +} \ No newline at end of file