Oh no !! Again SharedPreferences

Moinkhan Pathan
3 min readJul 20, 2018

--

Do you feel the same when you start the new project and you or your team members ask you to create shared preference utility, So either you write that boilerplate code again or do copy paste the whole file from another project.

It does not matter which application you are developing, SharedPrefrence is one of the most common requirement of any application for Android developer. this is the one of the boring task that you have to write a code for shared preferences every time you start new project.

If you are lazy developer then you definitely hate it. So instead of reinvent the wheel there is one library https://github.com/moinkhan-in/PreferenceSpider (PreferenceSpider) which does all boilerplate code for you. So let’s get started.

This library work with the native shared preferences so you can integrate it with your existing projects.

dependencies {
implementation 'in.moinkhan:preferencespider:alpha-3.0'
annotationProcessor 'in.moinkhan:preferencespider-compiler:alpha-3.0'
}

Preference Binding

Main feature offered by PreferenSpider is that we can bind shared preferences without writing any boilerplate code. You just have to use Preference annotation.

class ExampleActivity extends Activity {

@Preference
Integer spInt; // you can also use primitive types.

@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_activity);

PreferenceSpider.read(this);
// TODO Use fields...
}

@OnClick(R.id.btnSave)
void onSaveClicked() {

// somehow you update the value.
spInt = 50;

// Update the shared preference.
PreferenceSpider.write(this);
}
}

In above example we have used preference annotation without any attribute. So it will use default shared preference file and `spInt` as key.

Customization

There are 5 attributes are supported.

key

If you want to provide your preference key. If you omit it will take field name as key.

@Preference(key = "sp_string")
String spString;

equivalent to

// for writing
getDefaultPrefsEditor() .edit()
.putString("sp_string", spString) .commit();
// for reading
spString = getDefaultPrefsEditor()  .getString("sp_string", null);

name

If you have custom preference file name.

@Preference(name = "my_file")
String spString;

equivalent to

// for writing
getSharedPrefsEditor("my_file")
.edit()
.putString("spString", spString) .commit();
// for reading
spString = getSharedPrefsEditor("my_file") .getString("spString", null);

Now, let’s say you want to apply preference file name for all preference fields. So it’s not good to write name attribute on each fields, for that you can configure it at a application level.

public class MyApplication extends Application {

@Override
public void onCreate() {
super.onCreate();
PreferenceSpider.newBuilder()
.preferenceName("my_file")
.build();
}
}

It will use `my_file` for all preference field except the field on which name attribute is applied.

defaultValue

If you want to give default value to each preference.

@Preference(defaultValue = "userDefault")
String spString;

equivalent to

// for writing
...
// for reading
spString = getDefaulltPrefsEditor() .getString("spString", "userDefault");

format

Sometimes you want to display your preference value in a formatted string.

@Preference(defaultValue = "Guest", format = "Welcome: %s")
String spUser;

equivalent to

// for reading
spString = String.format("Welcome: %s", getDefaulltPrefsEditor() .getString("spUser", "Guest"));

Keep in a mind applying format will make that preference `readOnly`. That means if you apply the `PreferenceSpider.write(this);` it will ignore all the fields which has format attribute.

readOnly

There are some fields which you want to be act as `readOnly`.
that is if you apply the write function that should be ignored.

@Preference(defaultValue = "true", readOnly = true)
boolean spBoolean;

It will not generate the write function for this field.

Ok, we understood that annotation will remove all boilerplate code for me.
But i don’t want to use annotation.
Don’t worry still you can use PrefrenceUtils class.

String spStr = PreferenceUtils.getInstance(this)
.readString("spStr");
int spInt = PreferenceUtils.getInstance(this)
.readInt("spInt");

There are more methods are available you can see on the official github link.

--

--