Compose-Settings
Compose-Settings copied to clipboard
Generic `rememberPreference*SettingState`
Hi there, Sorry for creating back-to-back Issues & PRs. I liked the solution provided by the library & just think that there can be improvements :)
Enhancement / Suggestion :
I think we can use generic api like below instead of explicitly typed rememberPreference*SettingState
-
-
rememberSettingsDataStoreState<T>
-
rememberSettingsPreferenceState<T>
Sample usage:
// Using SharedPreference
val switchState = rememberSettingsPreferenceState(key = "themeSwitch", defaultValue = false)
// Using DataStore
val switchState = rememberSettingsDataStoreState(key = "themeSwitch", defaultValue = false)
Let me know what you think.
So, the API will be:
val switchState = rememberSettingsPreferenceState<Boolean>(key = "themeSwitch", defaultValue = false)
val rangeState = rememberSettingsPreferenceState<Float>(key = "themeRange", defaultValue = 0.05f)
val itemState = rememberSettingsPreferenceState<String>(key = "themeRange", defaultValue = "Forlayo")
I like the idea, and I think I started thinking that way.
But... it may cause problems when trying to save complex data:
val smthngState = rememberSettingsPreferenceState<MyCustomDataClass>(key = "themeRange", defaultValue = ...)
Would you like to create a proposal and we can d discuss it further?
Does the current implementation allow saving custom objects to local storage?
If we require that sort of api, we can try to add transformation parameters to allow developers to convert the custom data to & from String
similar to TypeConverter
in RoomDB but an implementation like that seems out of scope here.
Well, I agree.
It allows that when using proto. But it was made by another user and I don't know much about that.
Btw, SettingsValueState
already support generics.
The thing is... On switch / checkbox you only remember boolean, for example.
But, adding a generic one sounds good for me, with error on not allowed types
I haven't seen an example of Proto in the sample app but looking at rememberProtoDataStoreTransformSettingState
, I see that its already generic.
Agreed that SettingsValueState
already supports generics which also makes creating a new rememberSettingsPreferenceState
& rememberSettingsDataStoreState
easy.
I'll push a new PR & you can check if the new api is a fit for the project.
I've deleted the demo as plugin was updated and I couldn't make it work 😅
If you know about proto and feel like to add an example... I would love that!
I haven't tried Proto yet but I can try & update the sample apps later once we finalise the new api.