Anyone doing kmp development with androidx.datasto...
# random
c
Anyone doing kmp development with androidx.datastore? it works great on ios and android. but looks like its not supported for wasm. No big deal. I'll try to stub it out, but it looks like that's not possible since wasmJs source set doesn't know anything about datastore so I can't even stub things out. Im still a noob when it comes to kmp. Whats the process I should be taking when using it in an app where i have expect actual setup for datastore types?
p
One alternative, create a wrapper persistent module/library, which supports all your platforms. Then this library internally will consume datastore in Android and iOS target but will consume another key-value solution for wasm. From the outside, the API exposed by this library is the same for all targets and can be used in commonMain, could be something like
KeyValueStorage
or
KeyValuePersistanceManager
. Internally the library will have different dependencies and API wrappers on each platform sourceset. Obviously the public API the library exposes, has to live in the library commonMain sourceset. But actual implementations internally for the library will live independently in each target inside the library, with different dependencies each, if required.
☝️ 1
j
+1 for ☝️. In my projects I usually just use datastore for storing user preferences and such, so I'll have an
interface UserPreferences
exposing flows and setters. The implementations in each supported platform use
DataStore
, so in [androidMain] it gets a
Context
injected through Koin, but in [desktopMain] it doesn't need that and directly passes a
File
into
PreferenceDataStoreFactory
c
Hm. I guess I'm not understanding... I'm following this https://developer.android.com/kotlin/multiplatform/datastore but I can't even have a stub in my wasm
fun createDataStore(): DataStore<Preferences> = //TODO
Let me read this again when I'm at my computer and not on mobile. maybe it'll click then. sounds like it should be possible though so thats good.
j
In my
UserPreferences
example I have zero references to DataStore, I keep that an internal implementation detail :)
c
gotcha. okay. so i guess just using their example from the docs wasn't helpful. i thought they would still give the wasm srcSet the same class so I could reference it but do my own no op. so ill wrap preferences entirely. hm. thank you!