russhwolf / multiplatform-settings

A Kotlin Multiplatform library for saving simple key-value data
Apache License 2.0
1.7k stars 67 forks source link

androidx.datastore integration #58

Closed russhwolf closed 3 years ago

russhwolf commented 4 years ago

The Android team is quietly working on a SharedPreferences replacement called datastore. It'd be nice to have a module that wraps that, once it's ready. An interesting wrinkle is that it has fully coroutine-based APIs so it's going to be non-trivial to wrap with the existing Settings interface.

russhwolf commented 4 years ago

Datastore is now in alpha

JavierSegoviaCordoba commented 4 years ago

@russhwolf, since DataStore is based on Flow, will it break the current API? Or will you add a Flow APIs too?

russhwolf commented 4 years ago

I wouldn't want break the existing API. Probably I'd add an alternate interface based on suspend funs or flows, and a wrapper to convert existing implementations to that interface so you can control both from common. Another option is wrapping everything in runBlocking, but that's much more likely to result in someone accidentally misusing stuff and blocking their main thread.

I'd love to hear what other ideas people have.

JavierSegoviaCordoba commented 4 years ago

Supporting a flow API which emit after each "insertion" is great (I used DataStore for this). This behavior is used by a lot of libraries (room, dataStore, sqldelight, etc).

I think it shouldn't use runblocking, if anyone need this flow so, they can wrap the flow with runblocking (indeed I used this with dataStore too).

russhwolf commented 4 years ago

Flow would be nice (there's a prototype in #37 of wrapping flows around the existing listeners) but I'm leaning toward suspend fun by default so that commonality with non-observable implementations (in particular, JS) is easier. Still need to do more thinking and prototyping though.

JavierSegoviaCordoba commented 4 years ago

Maybe this can be interesting

https://twitter.com/ianhlake/status/1326598994408038400?s=19

russhwolf commented 3 years ago

My current thinking is to have both FlowSettings and SuspendSettings interfaces, and converters between them and the standard Settings and ObservableSettings. Any Settings implementation can be wrapped to SuspendSettings pretty easily, but FlowSettings can only wrap around ObservableSettings because you need to set a listener to be able to get new updates in its Flows. There would be DataStore implementations of both, so you could use the one that fits your use-case better depending on what other implementations you need interop with.

I want to have a version of this ready in the near future as part of 0.7

russhwolf commented 3 years ago

This has been released in 0.7