Compose-Settings icon indicating copy to clipboard operation
Compose-Settings copied to clipboard

Generic `rememberPreference*SettingState`

Open ItzNotABug opened this issue 2 years ago • 6 comments

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 -

  1. rememberSettingsDataStoreState<T>
  2. 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.

ItzNotABug avatar Feb 20 '23 14:02 ItzNotABug

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?

alorma avatar Feb 20 '23 15:02 alorma

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.

ItzNotABug avatar Feb 20 '23 15:02 ItzNotABug

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

alorma avatar Feb 20 '23 15:02 alorma

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.

ItzNotABug avatar Feb 20 '23 15:02 ItzNotABug

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!

alorma avatar Feb 20 '23 15:02 alorma

I haven't tried Proto yet but I can try & update the sample apps later once we finalise the new api.

ItzNotABug avatar Feb 20 '23 16:02 ItzNotABug