Skip to content
Kotlin Flow version of rx-preferences -- Coroutines support for Android SharedPreferences
Kotlin
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.idea First commit with the initial version Oct 26, 2019
flow-preferences-sample First commit with the initial version Oct 26, 2019
flow-preferences-tests Simplify BaseTest setup Nov 21, 2019
flow-preferences Add key to ValueNotPersistedException message Nov 21, 2019
gradle/wrapper First commit with the initial version Oct 26, 2019
.gitignore
LICENSE Initial commit Oct 20, 2019
README.md Add README Oct 26, 2019
build.gradle First commit with the initial version Oct 26, 2019
dependencies.gradle Update Coroutines to 1.3.2 Nov 14, 2019
gradle.properties First commit with the initial version Oct 26, 2019
gradlew First commit with the initial version Oct 26, 2019
gradlew.bat First commit with the initial version Oct 26, 2019
settings.gradle

README.md

Build Status Release

Flow Preferences

This is the Kotlin Flow version of rx-preferences. It follows pretty much the same API and should feel familiar to anyone with experience there. But instead of RxJava, we have Coroutines -- mainly Flows.

Download

repositories {
  maven { url "https://jitpack.io" }
}

dependencies {
  implementation 'com.github.tfcporciuncula:flow-preferences:1.0.0'
}

How it looks like

Start with the regular SharedPreferences:

val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)

Create an instance of FlowSharedPreferences from that:

val flowSharedPreferences = FlowSharedPreferences(sharedPreferences)

Get a preference:

val myPref = flowSharedPreferences.getInt("key", defaultValue = 10)

Go with the Flow:

scope.launch {
  myPref.asFlow().collect { print(it) }
}

Extras

Set and commit support

Preferences expose the regular get() and put() (named as set()) functions from SharedPreferences. But in addition to that, they also expose the suspend setAndCommit() function that puts the value and performs a commit in case you must ensure the preference is persisted right away.

Collector support

You can call asCollector() to ask a FlowCollector from a preference. You can then persist values from a Flow directly to the preference:

val flow = flow { emit(1) }

scope.launch {
  myPref.asCollector().emitAll(flow)
}

You can use asSyncCollector() if you want to put and commit the value (like setAndCommit()) on each emission.

Enum support

Enum classes work out of the box and are persisted as strings based on their name value:

enum class MyEnum { A, B, C }

val myPref = flowSharedPreferences.getEnum("key", defaultValue = MyEnum.A)

Object support

Arbitrary objects are also supported as long as an instance of ObjectPreference.Serializer is provided:

class TestObject(val id: Int)

val serializer =
  object : ObjectPreference.Serializer<TestObject> {
    override fun deserialize(serialized: String) = TestObject(serialized.toInt())
    override fun serialize(value: TestObject) = value.id.toString()
  }

val myPref = flowSharedPreferences.getObject("key", serializer, defaultValue = TestObject(0))

You can’t perform that action at this time.