GithubHelp home page GithubHelp logo

desiderantes / rx-preferences Goto Github PK

View Code? Open in Web Editor NEW

This project forked from f2prateek/rx-preferences

0.0 0.0 2.0 361 KB

Reactive SharedPreferences for Android

License: Apache License 2.0

Java 89.34% Shell 1.43% Kotlin 9.23%
hacktoberfest android rxjava rxandroid3

rx-preferences's Introduction

Rx Preferences

Reactive SharedPreferences for Android.

Usage

Create an RxSharedPreferences instance which wraps a SharedPreferences:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
RxSharedPreferences rxPreferences = RxSharedPreferences.create(preferences);
val preferences = PreferenceManager.getDefaultSharedPreferences(context)
val rxpreferences = RxSharedPreferences.create(preferences)

Hint: Keep a strong reference on your RxSharedPreferences instance for as long as you want to observe them to prevent listeners from being GCed.

Create individual Preference objects:

Preference<String> username = rxPreferences.getString("username");
Preference<Boolean> showWhatsNew = rxPreferences.getBoolean("show-whats-new", true);
val username = rxPreferences.getString("username")
val shoWhatsNew = rxPreferences.getBoolean("show-whats-new", true)

Observe changes to individual preferences:

username.asObservable().subscribe(new Action1<String>() {
  @Override public void call(String username) {
    Log.d(TAG, "Username: " + username);
  }
}
username.asObservable.subscribe { usernameStr ->
    Log.d(TAG, "Username: $usernameStr")
}

Subscribe preferences to streams to store values:

RxCompoundButton.checks(showWhatsNewView)
    .subscribe(showWhatsNew.asConsumer());

(Note: RxCompoundButton is from RxBinding)

Test your preferences with Memory Storage:

Preference<String> stringPref = InMemoryPreference.getStringPreference("string-pref");
stringPref.setSync("New Value");

assertThat(stringPref.isSet()).isTrue();

Download

implementation 'com.desiderantes.rx.preferences3:rx-preferences:3.0.0-SNAPSHOT'
implementation("com.desiderantes.rx.preferences3:rx-preferences:3.0.0-SNAPSHOT")

The package is available in the Package registry from this Github repository

License

Copyright 2014 Prateek Srivastava
          2021 - Mario Daniel Ruiz Saavedra

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.

rx-preferences's People

Contributors

andrewreitz avatar banasiak avatar bemusementpark avatar braisgabin avatar desiderantes avatar egor-n avatar f2prateek avatar freszu avatar jakewharton avatar m4xp1 avatar michaelevans avatar nekdenis avatar nightlynexus avatar paulwoitaschek avatar r-gurzkowski avatar rjrjr avatar skao93 avatar ubuntudroid 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.