Skip to content
This repository has been archived by the owner on Sep 7, 2021. It is now read-only.
/ PreferencesFlow Public archive

Android SharedPreferences powered by Kotlin Flow and Kotlin serialization.

Notifications You must be signed in to change notification settings

aallam/PreferencesFlow

Repository files navigation

Preferences Flow

Maven Central

Android SharedPreferences powered by Kotlin flow and Kotlin serialization.

Download

The latest release is available on Maven Central.

implementation 'com.aallam.preferencesflow:preferencesflow:0.1.0'

Usage

Create a PreferencesFlow by passing a SharedPreferences instance:

val preferencesFlow = PreferencesFlow(sharedPreferences)

Or, an Android Context for default SharedPreferences:

val preferencesFlow = PreferencesFlow(context)

Preference

Create a Preference object:

val preference = preferencesFlow.boolean("key", defaultValue = true)

Observe changes to the individual preference:

preference.asFlow()
            .onEach { println(it) }
            .launchIn(scope)

Serializable objects

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.

Credit

This library inspired by rx-preferences

License

PreferenceFlow is distributed under the terms of the Apache License (Version 2.0). See LICENSE for details.

About

Android SharedPreferences powered by Kotlin Flow and Kotlin serialization.

Resources

Stars

Watchers

Forks

Languages