Android SharedPreferences powered by Kotlin flow and Kotlin serialization.
The latest release is available on Maven Central.
implementation 'com.aallam.preferencesflow:preferencesflow:0.1.0'
Create a PreferencesFlow
by passing a SharedPreferences
instance:
val preferencesFlow = PreferencesFlow(sharedPreferences)
Or, an Android Context for default SharedPreferences
:
val preferencesFlow = PreferencesFlow(context)
Create a Preference
object:
val preference = preferencesFlow.boolean("key", defaultValue = true)
Observe changes to the individual preference:
preference.asFlow()
.onEach { println(it) }
.launchIn(scope)
It's possible use Serialization to store, operate and
observe objects in the SharedPreference
.
First, create an Serializable
class:
@Serializable
internal data class Point(val x: Int, val y: Int)
Then create a Preference
like usual:
preferencesFlow.serializable("point", Point.serializer())
Annotating a Kotlin class with @Serializable
instructs the serialization plugin to automatically generate
implementation of KSerializer
that can be accessed with T.serializer()
extension function on the class companion.
This library inspired by rx-preferences
PreferenceFlow is distributed under the terms of the Apache License (Version 2.0). See LICENSE for details.