MG Preference - mrkcsc/android-mg-bootstrap GitHub Wiki
Initialize the preferences utility in your Application class:
class App extends Application {
@Override
public void onCreate() {
super.onCreate();
MGPreference.getConfig().init(this);
}
}
Create an MGPreference
object typed to an object of your choosing - provide a unique String
identifier for look-ups.
Note: You can make the variable final
and static
if you choose because the look-up is done by key.
// You can back primitive class wrappers.
MGPreference<String> pref = MGPreference.create("KEY");
MGPreference<String> pref = MGPreference.create("KEY", "defaultValue");
// You can back classes that use generics.
MGPreference<Map<String, List<String>>> pref = MGPreference.create("KEY");
// You can back complex objects.
MGPreference<Object> pref = MGPreference.create("KEY");
Once you variable is declared
, get and/or set its value as needed. Get will return null if no value has been previously set.
// Get
int value = prefSimple.get();
// Set
prefSimple.set(100);
// Clear
preference0.clear();
Create an RxJava enabled version of a preference as follows:
// Create like a normal preference object.
MGPreferenceRx<Object> test = MGPreferenceRx.create("KEY");
// Optionally provide a default value, otherwise default is null.
MGPreferenceRx<Object> test = MGPreferenceRx.create("KEY", new Object());
// Final overload specifies if this preference should be cached to disk.
MGPreferenceRx<Object> test = MGPreferenceRx.create("KEY", new Object(), false);
Usage of MGPreferenceRx
as follows:
// Subscribe to updates.
test.get().subscribe(object -> { });
// Subscribe overload to not emit null values.
test.get(false).subscribe(object -> {});
// Fetch the latest value synchronously.
Object object = test.getBlocking();
// Set value.
test.set(new Object());
Todo:
Todo: