All Projects â†’ afollestad â†’ Rxkprefs

afollestad / Rxkprefs

Licence: apache-2.0
🛠 A small Kotlin library to make shared preferences easy + RxJava and Coroutines support

Programming Languages

kotlin
9241 projects

Projects that are alternatives of or similar to Rxkprefs

rxkotlin-jdbc
Fluent RxJava JDBC extension functions for Kotlin
Stars: ✭ 27 (-89.77%)
Mutual labels:  rxjava, rx, rxkotlin
Reaktive
Kotlin multi-platform implementation of Reactive Extensions
Stars: ✭ 760 (+187.88%)
Mutual labels:  reactive, rx, rxkotlin
Prefser
Wrapper for Android SharedPreferences with object serialization and RxJava Observables
Stars: ✭ 228 (-13.64%)
Mutual labels:  rxjava, storage, sharedpreferences
Rxgps
Finding current location cannot be easier on Android !
Stars: ✭ 307 (+16.29%)
Mutual labels:  rxjava, reactive, rx
Rxfirebase
Rxjava 2.0 wrapper on Google's Android Firebase library.
Stars: ✭ 509 (+92.8%)
Mutual labels:  rxjava, reactive, storage
Android Okgraphql
Reactive GraphQl client for Android
Stars: ✭ 64 (-75.76%)
Mutual labels:  rxjava, reactive, rx
Binder
An Annotation processor that allows binding two classes with each other, where the first class can listen to the updates of the second class ... ideal for MVVM and similar patterns
Stars: ✭ 21 (-92.05%)
Mutual labels:  reactive, rxjava, rx
iMoney
iMoney 金融项目
Stars: ✭ 55 (-79.17%)
Mutual labels:  sharedpreferences, rxjava
Rx.Http
A reactive way to make HTTP Request in .NET Core 🚀
Stars: ✭ 62 (-76.52%)
Mutual labels:  reactive, rx
modern-android
Modern Android Project Skeleton
Stars: ✭ 17 (-93.56%)
Mutual labels:  rxjava, rxkotlin
fpEs
Functional Programming for EcmaScript(Javascript)
Stars: ✭ 40 (-84.85%)
Mutual labels:  reactive, rx
KVStorage
Android 结构化KV存傚框架基于 yaml 生成 java 结构化存傚类
Stars: ✭ 228 (-13.64%)
Mutual labels:  sharedpreferences, storage
kesho
store cache any data type string, boolean, jsonObject, jsonArray, .....
Stars: ✭ 19 (-92.8%)
Mutual labels:  sharedpreferences, storage
neurosky-android-sdk
Android SDK for the NeuroSky MindWave Mobile Brainwave Sensing Headset
Stars: ✭ 39 (-85.23%)
Mutual labels:  rxjava, rxkotlin
RxPagination
Implement pagination in just few lines with RxPagination
Stars: ✭ 20 (-92.42%)
Mutual labels:  rx, rxkotlin
RxAnimator
An RxJava2 binding for android Animator
Stars: ✭ 80 (-69.7%)
Mutual labels:  rxjava, rx
InterReact
Interactive Brokers reactive C# API.
Stars: ✭ 28 (-89.39%)
Mutual labels:  reactive, rx
reactify
The first and only true Functional Reactive Programming framework for Scala.
Stars: ✭ 77 (-70.83%)
Mutual labels:  reactive, rx
rxjava2-http
Transmit RxJava2 Flowable over http with non-blocking backpressure
Stars: ✭ 19 (-92.8%)
Mutual labels:  reactive, rxjava
mono-reactive
open source Reactive Extensions (Rx) implementation for Mono
Stars: ✭ 65 (-75.38%)
Mutual labels:  reactive, rx

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.

Android CI Codacy Badge License


Gradle Dependency

Core

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

dependencies {

  implementation "com.afollestad.rxkprefs:core:2.0.3"
}

Getting Started

The core of the library is the RxkPrefs interface. You can retrieve an instance of this interface with the rxkPrefs method, which takes 3 parameters. One of these parameters is optional (the shared preferences mode).

// Parameter is your Context, like an Activity, uses PreferenceManager#getDefaultSharedPreferences
val myPrefs = rxkPrefs(this)

// 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, it defaults to MODE_PRIVATE above.
// 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. By that, I do not mean the raw value of the preference, but an instance of the Pref interface which provides more functionality.

val myPrefs: RxkPrefs = // ...

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

// You could omit the second parameter to use the default, default value (empty string)
val myString: Pref<String> = 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<Int> = // ...

// The key of the preference - first parameter passed in prefs.integer(...) or any other pref getter
// This is always a String.
val key: String = myPref.key()

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

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

// Changes the value of the preference.
myPref.set(1024)

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

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

// These are used by the RxJava and coroutines extensions, but you may find them useful.
myPref.addOnChangedListener { }
myPref.addOnDestroyedListener { }

// Destroys the instance, clearing listeners and anything that could leak memory.
myPref.destroy()

Coroutines Extension

Gradle Dependency

Coroutines

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

dependencies {
    
  implementation "com.afollestad.rxkprefs:coroutines:2.0.3"
}

As a Flow

You can receive changes to a preference in real-time using a coroutines Flow, specifically a hot flow.

val myPref: Pref<Boolean> = // ...
val flow: Flow<Boolean> = myPref.asFlow()

// One way...
scope.launch {
  flow.collect { println(it) }
}

// Another way...
flow
  .onEach { println(it) }
  .launchIn(scope)


RxJava Extension

Gradle Dependency

RxJava

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

dependencies {
    
  implementation "com.afollestad.rxkprefs:rxjava:2.0.3"
}

As an Observable

You can receive changes to a preference in real-time using an RxJava Observable.

val myPref: Pref<Long> = // ...
val obs: Observable<Long> = myPref.observe()

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

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.

As a Consumer

Pref can act as an RxJava Consumer. You can use this 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> = // ...

RxCompoundButton.checks(yourCheckboxView)
  .subscribe(myPref.asConsumer())

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

Basically, it works like this:

val myObs: Observable<String> = // ...
val myConsumer: Consumer<String> = // ...which can be from an instance of Pref

myObs.subscribe(myConsumer)
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].