Skip to content

Android library for working with preferences (key-value pairs), powered by RxJava

License

Notifications You must be signed in to change notification settings

neXenio/RxPreferences

Repository files navigation

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));