MFlisar / ComposePreferences

Compose Preference Screen for Android - Material3 Design (including customisation, nesting and more)
Apache License 2.0
6 stars 0 forks source link

onValueChanged cancelable ? #6

Closed jscti closed 5 months ago

jscti commented 5 months ago

Hi there

It would be nice to be able to cancel a value change with onValueChanged.

Use Case / example :

or any other case needing a custom check

I implemented it in a custom Preference and the changes are really small but maybe that could/should be a core feature of your lib :

fun PreferenceScope.PreferenceBool(
    ...
    onValueChange: (selected: Boolean) -> Boolean,
    ...
) {
   ...
   if (onValueChange(it)) {
        checked = it
   }
   ...
}

Without this, it's painfull to revert the switch back on the "disabled" state. I needed to play with delay() to update the preference storage AFTER the onValueChanged finished its job and actually enabled the switch

MFlisar commented 5 months ago

Are you talking about the KotPreference implemention?

Otherwise, it's really simple:

I will add the cancel option to the KotPreference implementation though

MFlisar commented 5 months ago

I just saw, that the bool preference did hold an unnecessary internal state that prevented it from being cancelable. Should be possible now and I also added this feature for the KotPreference version

jscti commented 5 months ago

Great thank you, it works now No I wasn't using the KOT implementation