onValueChanged cancelable ?

#6 · closed · 3 comments

View on GitHub ↗

jscti

Hi there It would be nice to be able to cancel a value change with `onValueChanged`. Use Case / example : - A checkbox to activate geolocating / notifications : anything related to Android System Permission - When activating the switch, we show a permission popup (async) - if user refuses : we want the switch to not change its states. so we would invalidate the change - if user accepts: let the switch state be changed 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

Comments

MFlisar

Are you talking about the `KotPreference` implemention? Otherwise, it's really simple: * create a state * pass `state.value` as `value` to the preference * update or not update the `state.value` in the `onValueChange` callback I will add the cancel option to the `KotPreference` implementation though

MFlisar

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

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