Skip to content

ProxyPref is an Android library, which represents SharedPreferences as an interface

License

Notifications You must be signed in to change notification settings

konmik/proxypref

Repository files navigation

ProxyPref

ProxyPref is a simple Android library, which allows to easily access SharedPreferences.

Definition

Define an interface with set/get method for types: String, Integer, Long, Float, Boolean, Set<String>

Keys are method names from the interface, but without set/get prefixes. You can omit set/get part if you like.

Annotate fields with Preference annotation to set a preference key name if you're going to use ProGuard.

RxJava

There is also rx.Observable and rx.functions.Action1 interface support to chain preferences into RxJava operators.

null

null is does not exist value. If you don't want to get nulls - define default values with DefaultString, DefaultInteger, DefaultLong, DefaultFloat, DefaultBoolean, DefaultSet.

You can also remove a key-value pair from preferences by passing null into the set method.

interface MyPreferences {

    // access with get/set prefix
    String getTestString();         // key = testString
    void setTestString(String x);   // key = testString

    // without get/set prefix
    Integer testInteger();          // key = testInteger
    void testInteger(Integer x);    // key = testInteger

    // observe with rx.Observable
    Observable<Integer> lastSelectedItem(); // key = lastSelectedItem

    // set with rx.functions.Action1
    Action1<Integer> setLastSelectedItem(); // key = lastSelectedItem

    // ProGuard ready
    @Preference("username")
    String a12();               // key = username

    // Default value
    @DefaultString("user256")
    String username();          // key = username

    // Default set
    @DefaultSet({"1", "2", "3"})
    Set<String> getSomeSet();   // key = someSet
}

Usage

Is easy!

MyPreferences pref = ProxyPreferences
    .build(MyPreferences.class, getSharedPreferences("preferences", 0));

Log.v("test", pref.username());
OR
MyPreferences pref = ProxyPreferences
    .buildWithRx(MyPreferences.class, getSharedPreferences("preferences", 0));

If you want to have RxJava features. Don't forget to include RxJava itself. ;)

RxJava warning

Keep hard references to RxJava subscriptions otherwise they can be GC'ed due to the SharedPreferences bug.

Dependency

compile 'info.android15.proxypref:proxypref:0.2.0'

About

ProxyPref is an Android library, which represents SharedPreferences as an interface

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages