Giter Site home page Giter Site logo

codacy-badger / rxkprefs Goto Github PK

View Code? Open in Web Editor NEW

This project forked from afollestad/rxkprefs

0.0 0.0 0.0 66 KB

A small, Rx-powered shared preferences library for Kotlin.

Home Page: https://aidanfollestad.com

License: Apache License 2.0

Kotlin 100.00%

rxkprefs's Introduction

RxkPrefs

This library provides reactive shared preferences interaction with very little code. It is designed specifically to be used with Kotlin.

Inspiration has been taken from other libraries, but it was written from the ground up on its own.

jCenter Build Status Codacy Badge License


Gradle Dependency

Add this to your module's build.gradle file:

dependencies {
    // ... other dependencies
    implementation 'com.afollestad:rxkprefs:1.0.0'
}

Getting Started

The core class of the library is RxkPrefs. It takes 3 parameters, one of which is optional (the shared preferences mode).

// First parameter is your Context, like an Activity, the second is a key.
val myPrefs = RxkPrefs(this, "my_prefs")

// The optional third parameter is a mode.
// This is like using Context.getSharedPreferences("my_prefs", MODE_PRIVATE)
val myPrefs = RxkPrefs(this, "my_prefs", MODE_PRIVATE)

Retrieving a Preference

With a RxkPrefs instance, you can retrieve preferences.

val myPrefs: RxkPrefs = // ...

// Getting a string preference is as simple as this:
val myString = myPrefs.string("my_string", "default_value")

// You could omit the second parameter to use the default, default value (empty string)
val myString = myPrefs.string("my_string")

Interacting with a Preference

Once you have a reference to a preference, there are a few things you can do with them.

val myPref: Pref<String> = // ...

// The key of the preference - first parameter passed in prefs.string(...)
val key: String = myPref.key()

// The default value of the preference - second parameter passed in prefs.string(...)
// Or the primitive default, such as an empty string, 0, or false.
val defaultValue: String = myPref.defaultValue()

// The current value of the preference, or the default value if none.
val currentValue: String = myPref.get()

// Changes the value of the preference.
myPref.set("new value!")

// True if a value has been set, otherwise false.
val isSet: Boolean = myPref.isSet()

// Deletes any existing value for the preference.
myPref.delete()

// See "The Preference Observable" below 
val observable: Observable<String> = myPref.asObservable()

// See "The Preference Consumer" below
val consumer: Consumer<String> = myPref.asConsumer()

The Preference Observable

You can receive changes to a preference in real-time with its Observable.

val myPref: Pref<String> = // ...
val obs = myPref.asObservable()

val sub = obs.subscribe { newValue ->
  // use new value
}
sub.dispose() // when you no longer want to receive values

Further usage of this is more of an RxJava issue and less specific to this library. You should have a basic understanding of what you can do with RxJava and what its use cases are.

The Preference Consumer

You can use the preference consumer to save preference values from the emissions of an Observable.

Say you're using RxBinding to bind Android views to Observables that emit when their value changes, such as a CheckBox:

val myPref: Pref<Boolean> = // ...
val consumer = myPref.asConsumer()

RxCompoundButton.checks(yourCheckboxView)
  .subscribe(consumer)

Whenever the checkbox is checked or unchecked, the underlying boolean shared preference is set to true or false automatically.

rxkprefs's People

Contributors

afollestad avatar

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.