Skip to content

Latest commit

 

History

History
66 lines (48 loc) · 3.49 KB

README.md

File metadata and controls

66 lines (48 loc) · 3.49 KB

Travis GitHub release JitPack Codecov license

RxPreferences

This library provides an RxJava interface for working with preferences (key-value pairs), as well as some commonly used implementations.

Usage

Integration

You can get the latest artifacts from JitPack:

allprojects {
    repositories {
        maven { url 'https://jitpack.io' }
    }
}

dependencies {
    implementation 'com.github.neXenio:RxPreferences:1.2.0'
}

PreferencesProvider

The PreferencesProvider interface allows you to persist, restore or delete key-value pairs of any type. There are different implementations available:

  • SharedPreferencesProvider uses SharedPreferences. It's what you'd normally use in simple apps.
  • EncryptedSharedPreferencesProvider uses EncryptedSharedPreferences. Wraps the SharedPreferences class and automatically encrypts keys and values
  • InMemoryPreferencesProvider uses a simple HashMap. It's very fast but data is not actually persisted to disk. Useful for testing purposes.

The most important methods are:

  • Completable persist(String key, Type value)
  • Single<Type> restore(String key, Class<Type> typeClass)
  • Observable<Type> getChanges(String key, Class<Type> typeClass)
  • Completable delete(String key)

There are also some convenience methods available, they are documented here.

Serializer

The Serializer interface is used by a PreferencesProvider to serialize the values that you want to persist, and to deserialize the values that you want to restore. All PreferencesProvider implementations use a simple GsonSerializer by default. Setting a custom serializer is not required, but might be useful if you want use custom type adapters:

// create a Gson instance with some custom stuff
Gson gson = new GsonBuilder()
        .excludeFieldsWithoutExposeAnnotation()
        .registerTypeAdapter(Bitmap.class, new BitmapTypeAdapter())
        .create();

// create a preferences provider instance and set the Gson serializer
TrayPreferencesProvider trayPreferencesProvider = new TrayPreferencesProvider(context);
trayPreferencesProvider.setSerializer(new GsonSerializer(gson));