Hey everyone! :wave: I’ve been working on a new li...
# feed
m
Hey everyone! 👋 I’ve been working on a new library to simplify state persistence in Jetpack Compose, and I’m excited to share it with you all—*PreferenceState*! 🚀 If you've ever dealt with DataStore in Compose, you know the struggle—ViewModel, repository, Flow collection... all for a simple stored value! PreferenceState eliminates that boilerplate, letting you manage persistent state effortlessly. You can check it out here: PreferenceState Library Would love to hear your thoughts and feedback! Let me know if you find it useful or have any suggestions.
s
Where does it save these informations? https://github.com/russhwolf/multiplatform-settings for example has a documentation where this goes.
m
It saves all info in Datastore
s
What is DataStore?
Oh, ok... your library is Android only.
I have a plan to extend it to multiplatform in future
s
Yeah, ok. Understood. I somehow assumed all new libs would be KMP nowadays. 😅
2
https://github.com/russhwolf/multiplatform-settings can use Datastore on Android, too... So I would suggest to build your lib on top of that to make using it easier.
m
Yes the issue is not of datastore. I tried building it for Multiplatform, but the issue was with MutableState. Multiplaform has MutableStateFlow only and it was giving some trouble. So I will be expanding it in future when I find a workaround
s
Ah, I see! I wish you every success.
m
Thanks a lot Stefan
j
Androidx datastore is KMP btw. So not an issue :) I however think bad practice expose datastore directly into ui layer. It makes the code almost impossible to test to start with. I suggest read all values through data manager in between for single ui state UDF pattern.
👀 1
m
The problem is not with datastore. I am using mutablestate to read/write data on UI. I agree with the test part but the main aim for this library was to skip boilerplate for basic persistence.
s
So why isn’t it KMP? 🤔
m
If you see the code of PreferenceState Class
Copy code
abstract class PreferenceState<T>(
    private val key: Preferences.Key<T>,
    private val defaultValue: T,
    private val dataStore: DataStore<Preferences> = _dataStore
) : MutableState<T> {

    private val _state = mutableStateOf(defaultValue)
}
It is using mutableState. This is required so that I can directly use it like this in UI
Copy code
var randomString by rememberStringPreferenceState("randomString", "Hello World")

to edit
randomString = UUID.randomUUID().toString()
Basically you can directly assign the value to var and it will be written in datastore. But the problem with KMP is that "mutableState" is not available there and instead I will have to use mutableStateFlow which works in a slightly different way. So using that would not make writing the var so easy. It would be great if you guys can also contribute to make it compatible with KMP
k
MutableState isn't available in KMP?! That feels like it would essentially make CMP unusable, no?
👀 1
m
KMP have MutableStateFlow, but PreferenceState extends MutableState because of how it works internally. Checkout the code and I am sure you will understand why. This might help you to understand the diff between two. https://proandroiddev.com/mutablestate-or-mutablestateflow-a-perspective-on-what-to-use-in-jetpack-compose-ccec0af7abbf
m
CMP already has MutableState, it can't work without it. I'm pretty sure you can migrate this library to KMP. https://github.com/JetBrains/compose-multiplatform-core/blob/824d6dadfb69934f2a3ff[…]src/commonMain/kotlin/androidx/compose/runtime/SnapshotState.kt
m
Strange. Thanks @mohamed rejeb for the reference. I will look into this and will migrate to KMP if it works.