Giter VIP home page Giter VIP logo

flow-preferences's Introduction

Release Build Status ktlint

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:flow-preferences:1.3.3'
}

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:

myPref.asFlow().onEach { print(it) }.launchIn(scope)

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. There's also a deleteAndCommit().

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 (so make sure you @Keep them if you're using R8):

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))

Explicit nullability support

By default, strings, objects, enums and sets can never be null, so consumers don't ever have to worry about null checks. If you want to support nullable values, you can explicitly opt in by asking for the nullable-friendly preference types:

val nullableStringPreference = flowSharedPreferences.getNullableString("key", defaultValue = null)

โ•

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.