Store
🗳 Easier than SharedPreferences with 🚀Jetpack DataStore
Why DataStore?
- Safe to call on UI thread
- Safe from runtime exceptions
- Can signal errors
- Handles data migration(from SharedPreferences)
Dependency Gradle
Add below codes to your root build.gradle
file (not your module build.gradle file).
allprojects {
repositories {
maven { url "https://jitpack.io" }
}
}
And add a dependency code to your module's build.gradle
file
dependencies {
implementation 'com.github.kennethss:store:1.0.4'
}
Dependencies
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.3.1"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1"
Install(application)
class App : Application() {
override fun onCreate() {
super.onCreate()
Store.context(this).preferencesName("preferences_name")
}
}
UseCase
Activity & Fragment & ViewModel
setStore("SOME_KEY", "some primitive type")
getStore<String>("SOME_KEY") { value ->
//do something
}
And add a dependency code to your **module**'s `build.gradle` file
```groovy
dependencies {
implementation 'com.github.kennethss:store:1.0.4'
}
Dependencies
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.3.1"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1"
UseCase
Activity & Fragment & ViewModel
setStore("SOME_KEY", "some primitive type")
getStore<String>("SOME_KEY") { value ->
//do something
}
StoreSwitch
<com.github.kennethss.StoreSwitch
android:theme="@style/Widget.AppCompat.CompoundButton.Switch"
app:key="@string/some_key"
app:default_value="true"/>