improve behvavior of slider preference

using onConfirm instead of always onValueChange (which is called when sliding, but we're mostly interested in changed settings)
This commit is contained in:
Helium314 2025-04-28 13:56:36 +02:00
parent 1d441a8ca6
commit b5dece2ff4
2 changed files with 10 additions and 4 deletions

View file

@ -27,6 +27,7 @@ fun <T: Number> SliderPreference(
range: ClosedFloatingPointRange<Float>, range: ClosedFloatingPointRange<Float>,
stepSize: Int? = null, stepSize: Int? = null,
onValueChanged: (Float?) -> Unit = { }, onValueChanged: (Float?) -> Unit = { },
onConfirmed: (T) -> Unit = { },
) { ) {
val ctx = LocalContext.current val ctx = LocalContext.current
val prefs = ctx.prefs() val prefs = ctx.prefs()
@ -48,8 +49,13 @@ fun <T: Number> SliderPreference(
SliderDialog( SliderDialog(
onDismissRequest = { showDialog = false }, onDismissRequest = { showDialog = false },
onDone = { onDone = {
if (default is Int) prefs.edit().putInt(key, it.toInt()).apply() if (default is Int) {
else prefs.edit().putFloat(key, it).apply() prefs.edit().putInt(key, it.toInt()).apply()
onConfirmed(it.toInt() as T)
} else {
prefs.edit().putFloat(key, it).apply()
onConfirmed(it as T)
}
}, },
initialValue = initialValue.toFloat(), initialValue = initialValue.toFloat(),
range = range, range = range,
@ -59,7 +65,7 @@ fun <T: Number> SliderPreference(
}, },
onValueChanged = onValueChanged, onValueChanged = onValueChanged,
showDefault = true, showDefault = true,
onDefault = { prefs.edit().remove(key).apply(); onValueChanged(null) }, onDefault = { prefs.edit().remove(key).apply(); onConfirmed(default) },
intermediateSteps = stepSize?.let { intermediateSteps = stepSize?.let {
// this is not nice, but slider wants it like this... // this is not nice, but slider wants it like this...
((range.endInclusive - range.start) / it - 1).toInt() ((range.endInclusive - range.start) / it - 1).toInt()

View file

@ -233,7 +233,7 @@ fun createAdvancedSettings(context: Context) = listOf(
else -> "version unknown" else -> "version unknown"
} }
}, },
onValueChanged = { KeyboardSwitcher.getInstance().setThemeNeedsReload() } onConfirmed = { KeyboardSwitcher.getInstance().setThemeNeedsReload() }
) )
}, },
Setting(context, Settings.PREF_URL_DETECTION, R.string.url_detection_title, R.string.url_detection_summary) { Setting(context, Settings.PREF_URL_DETECTION, R.string.url_detection_title, R.string.url_detection_summary) {