I am releasing my first Kotlin Multiplatform library. Support for android, ios, desktop, native and ...
x
I am releasing my first Kotlin Multiplatform library. Support for android, ios, desktop, native and js. It's still under construction, ๐Ÿšง๐Ÿ‘ท๐Ÿพโ€โ™‚๏ธ๐Ÿ—๏ธ but SNAPSHOT builds are now out for feedback. API in the thread ๐Ÿงต https://github.com/xxfast/KStore
K 1
K 5
๐ŸŽ‰ 11
๐Ÿ’ฏ 2
Given that you have a
@Serializable
model
Copy code
@Serializable data class Pet(val name: String, val age: Int) // Any serializable
val mylo = Pet(name = "Mylo", age = 1)
Crate a store
Copy code
val store: KStore<Pet> = storeOf("path/to/file")
Get a value once
Copy code
val mylo: Pet? = store.get()
Observe for changes
Copy code
val pets: Flow<Pet?> = store.updates
Set value
Copy code
store.set(mylo)
Update a value
Copy code
store.update { pet: Pet? ->
  pet?.copy(age = pet.age + 1)
}
Delete/Reset value
Copy code
store.delete() // sets to null
store.reset() // sets to default
Rest of the configurations
Copy code
private val store: KStore<Pet> = storeOf(
  path = filePathTo("file.json"), // required
  default = null, // optional
  enableCache = true, // optional
  serializer = Json, // optional
)