rxpreferences

An RxJava wrapper for Android's SharedPreferences.

License

License

GroupId

GroupId

dev.drewhamilton.rxpreferences
ArtifactId

ArtifactId

rxpreferences
Last Version

Last Version

3.0.0
Release Date

Release Date

Type

Type

aar
Description

Description

rxpreferences
An RxJava wrapper for Android's SharedPreferences.
Project URL

Project URL

https://github.com/drewhamilton/RxPreferences
Source Code Management

Source Code Management

https://github.com/drewhamilton/RxPreferences

Download rxpreferences

How to add to project

<!-- https://jarcasting.com/artifacts/dev.drewhamilton.rxpreferences/rxpreferences/ -->
<dependency>
    <groupId>dev.drewhamilton.rxpreferences</groupId>
    <artifactId>rxpreferences</artifactId>
    <version>3.0.0</version>
    <type>aar</type>
</dependency>
// https://jarcasting.com/artifacts/dev.drewhamilton.rxpreferences/rxpreferences/
implementation 'dev.drewhamilton.rxpreferences:rxpreferences:3.0.0'
// https://jarcasting.com/artifacts/dev.drewhamilton.rxpreferences/rxpreferences/
implementation ("dev.drewhamilton.rxpreferences:rxpreferences:3.0.0")
'dev.drewhamilton.rxpreferences:rxpreferences:aar:3.0.0'
<dependency org="dev.drewhamilton.rxpreferences" name="rxpreferences" rev="3.0.0">
  <artifact name="rxpreferences" type="aar" />
</dependency>
@Grapes(
@Grab(group='dev.drewhamilton.rxpreferences', module='rxpreferences', version='3.0.0')
)
libraryDependencies += "dev.drewhamilton.rxpreferences" % "rxpreferences" % "3.0.0"
[dev.drewhamilton.rxpreferences/rxpreferences "3.0.0"]

Dependencies

compile (1)

Group / Artifact Type Version
io.reactivex.rxjava3 : rxjava jar 3.0.1

Project Modules

There are no modules declared in this project.

RxPreferences

An RxJava wrapper for Android's SharedPreferences.

Download

Maven Central

RxPreferences is available in Maven Central.

// RxPreferences:
implementation "dev.drewhamilton.rxpreferences:rxpreferences:$version"
// With Kotlin extensions:
implementation "dev.drewhamilton.rxpreferences:rxpreferences-ktx:$version"
// Dagger component:
implementation "dev.drewhamilton.rxpreferences:rxpreferences-dagger:$version"

Usage

RxPreferences

Get the current value of any preference:

rxPreferences.getIntOnce("Language count", 0)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(count -> displayCount(count));

Observe the initial value plus any changes to a preference:

rxPreferences.getStringStream("Favorite language", "None")
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(name -> displayBestFriend(name));

Edit preferences and monitor the completion of committing those changes:

rxPreferences.edit()
        .putInt("Language count", 1)
        .putString("Favorite language", "Java")
        .commit()
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(() -> showSavedIndicator(), error -> displayError(error));

Kotlin extensions

With Kotlin extensions, use the Editor as a receiver:

rxPreferences.edit {
    putInt("Language count", 2)
    putString("Favorite language", "Kotlin")
}.subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe({ showSavedIndicator() }, { error -> displayError(error) })

Dagger component

Easily provide and access RxPreferences instances using RxPreferencesComponent:

@Module
object PersistenceModule {

    private const val sharedPreferencesName = "example.SharedPreferences"

    private val ExampleApplication.sharedPreferences
        get() = getSharedPreferences(sharedPreferencesName, Context.MODE_PRIVATE)

    @JvmStatic
    @Provides
    fun preferencesComponent(application: ExampleApplication) =
        RxPreferencesComponent.create(application.sharedPreferences)

    @JvmStatic
    @Provides
    @Reusable
    fun preferences(preferencesComponent: RxPreferencesComponent) =
        preferencesComponent.rxPreferences()
}

License

Copyright 2018 Drew Hamilton

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Versions

Version
3.0.0
3.0.0-beta01