Extension for Android SharePreferences
, provide easier usage and LiveData style of change listener.
Add Jitpack repository to your root build.grable
:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Then add dependency in your module build.gradle
:
dependencies {
// Base module and LiveData usage
implementation 'com.github.Jintin.PreferencesExtension:preferences:1.3.0'
// RxJava3 usage
implementation 'com.github.Jintin.PreferencesExtension:rxjava:1.3.0'
// Flow usage
implementation 'com.github.Jintin.PreferencesExtension:flow:1.3.0'
}
- Provide reified type usage:
val value = preference.get<String>(MY_KEY)
// or
val value: String = preference.get(MY_KEY)
LiveData
style to get preference modify event:
val liveData = preference.liveData<String>(MY_KEY)
liveData.observe(this) {
// get update here
}
RxJava3
style to get preference modify event:
val observable = preference.observable<String>(MY_KEY)
observable.subscribe {
// get update here
}
Flow
style to get preference modify event:
val flow = preference.flow<String>(MY_KEY)
flow.collect {
// get update here
}
You can go to ./app
module to see the full example and get more information.
Bug reports and pull requests are welcome on GitHub at https://github.com/Jintin/PreferencesExtension.
The package is available as open source under the terms of the MIT License.