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

View file

@ -233,7 +233,7 @@ fun createAdvancedSettings(context: Context) = listOf(
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) {