Yet one more library leveraging kotlin magic for elegant shared prefs handling. With some extra rx sugar on top.
- Handles all primitive types that Shared Preferences do.
- ...but adds hassle-free
enum
serialization. - ...and any arbitrary
Serializable
structure viamoshi
json serializer. - Takes care of proper handling of nullable and default values.
- Provides
RxJava
observability.
You can use the delegates directly like you would use a regular variable:
var someFlag by prefs.boolean("some_flag", false)
textView.text = "The flag is currently: $someFlag" //loaded from shared preferences
someFlag = true //saved to shared preferences
The magic part is that every time you read or write the variable, it's loaded from shared prefs or saved to them.
var booleanImplicit by prefs.boolean("key1", false)
var booleanExplicit: Boolean by prefs.boolean("key2", false)
var booleanNullableImplicit by prefs.boolean("key3")
var booleanNullableExplicit: Boolean? by prefs.boolean("key4")
var booleanNullableExplicitDefault: Boolean? by prefs.boolean("key5", false)
val someObservableInt = prefs.observableInt("some_observable_int")
someObservableInt.observable().subscribe {
//will return the current value from shared prefs upon subscription
//and then while it's subscribed, every value subsequently written to it.
}
//for the observable to work you need to save via the same instance
someObservableInt.save(21)
Boolean
Int
Long
Float
Double
String
StringSet
Enum
(saved as a string name of the enum)- and other classes that can be serialized to json via
moshi
NOTE Prefer moshi-codegen for json serialization. Reflection is much slower and deserializing large structures upon app startup may slow it down significantly.
Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}
Add the dependency with current JitPack version:
dependencies {
implementation 'com.github.FutureMind:prefs-delegates:0.10.0'
// or if you need only one of the library modules:
implementation 'com.github.FutureMind.prefs-delegates:preferencesdelegates:0.10.0'
implementation 'com.github.FutureMind.prefs-delegates:rxpreferencesdelegates:0.10.0'
}