EasyDatastore

Additional

Language
Java
Version
N/A
Created
Jul 10, 2015
Updated
Jan 28, 2016 (Retired)
Owner
Frank Doyle (fdoyle)
Contributors
cketti
Frank Doyle (fdoyle)
2
Activity
Badge
Generate
Download
Source code
APK file

Advertisement

EasyDatastore

compile 'com.lacronicus:easydatastorelib:+'
//latest: 
compile 'com.lacronicus:easydatastorelib:1.0.4'

Every app I write has a wrapper object responsible for pushing/pulling data to/from sharedprefs. Usually, it's mostly copied from the last time I wrote one, which often leads to mismatched keys for reading and writing, or two writes having the same key, stupid stuff like that. No more.

With this setup, working with sharedprefs becomes pretty trivial and, because it's all based on an interface, mocking it out is easy too (thanks Retrofit).

Your interface should look like:

public interface MyDatastore {

    @Preference("foo")
    StringEntry foo();

    @Preference("bar")
    StringEntry bar();

    @Preference("baz")
    IntEntry myInt();

    @Preference("boolean")
    BooleanEntry myBoolean();

    @Preference("object")
    ObjectEntry<MyModel> myModel();
    
    @Preference("list")
    ObjectEntry<List<MyModel>> myModelList();
}

Then create your "Datastore" like

MyDatastore datastore = new DatastoreBuilder(PreferenceManager.getDefaultSharedPreferences(context))
                .create(MyDatastore.class);

Then you can do

datastore.bar().put("Hello World");

String bar = datastore.bar().get();

It now also supports all your basic types (String, int, boolean, float, long) as well as Objects via gson.

Todo:

  • multiple changes in a single apply?
  • throw exception if multiple methods share the same key?