Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ String bar = datastore.bar().get();

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

If no `@Preference` annotation is supplied, EasyDatastore will use the name of the method as the shared preferences key

Todo:
multiple changes in a single apply?
throw exception if multiple methods share the same key?
18 changes: 12 additions & 6 deletions app/src/main/java/com/lacronicus/easydatastore/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

public class MainActivity extends ActionBarActivity {

private static final String TAG = "APP";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -20,32 +22,36 @@ protected void onCreate(Bundle savedInstanceState) {

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

datastore.myPrefWithNoExplicitName().put("Hello, no explicit preference name!");
String myPrefWithNoExplicitName = datastore.myPrefWithNoExplicitName().get();
Log.d(TAG, myPrefWithNoExplicitName);

datastore.bar().put("Hello World");
String bar = datastore.bar().get();
Log.d("APP", bar);
Log.d(TAG, bar);
Toast.makeText(this, bar, Toast.LENGTH_SHORT).show();

datastore.myInt().put(2);
int myInt = datastore.myInt().get(-1);
Log.d("APP", "" + myInt);
Log.d(TAG, "" + myInt);

datastore.myFloat().put(2.4f);
float myFloat = datastore.myFloat().get(-1);
Log.d("APP", "" + myFloat);
Log.d(TAG, "" + myFloat);

datastore.myLong().put(Long.MAX_VALUE);
long myLong = datastore.myLong().get(-1);
Log.d("APP", "" + myLong);
Log.d(TAG, "" + myLong);

datastore.myBoolean().put(true);
boolean mybool = datastore.myBoolean().get(false);
Log.d("APP", "" + mybool);
Log.d(TAG, "" + mybool);

MyModel model = new MyModel("derp", 42);
datastore.myModel().put(model);
MyModel pulledModel = datastore.myModel().get();
Log.d("APP", pulledModel.toString());
Log.d(TAG, pulledModel.toString());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
* Created by fdoyle on 7/10/15.
*/
public interface MyDatastore {

StringEntry myPrefWithNoExplicitName();

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

Expand Down
2 changes: 1 addition & 1 deletion easydatastorelib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.library'
apply plugin: 'com.jfrog.bintray'
apply plugin: 'com.github.dcendents.android-maven'

version = "1.0.3"
version = "1.0.4"

android {
compileSdkVersion 21
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ public DatastoreHandler(SharedPreferences preferences, Gson gson) {

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
String key = method.getAnnotation(Preference.class).value();
Preference preference = method.getAnnotation(Preference.class);
boolean hasPreferenceValue = preference != null && preference.value() != null;

String key = hasPreferenceValue ? preference.value() : method.getName();
if (method.getReturnType().equals(StringEntry.class)) {
return new StringEntry(preferences, key);
} else if (method.getReturnType().equals(FloatEntry.class)) {
Expand Down