Screenshots • Description • Features • Libraries Used • Project Setup
Since Jetpack Compose
does not provide any libraries for Preferences
and it also uses SharedPreferences
which have been
deprecated, this implementation solves both these issues by using Preferences Datastore
- Uses Preferences Datastore
- SettingsCheckbox: Checkbox Preference
- SettingsSwitch: Switch Preference
- SettingsGroup: Group of Preferences
- SettingsList: Single select List Preference
- SettingsMultiList: Multi select List Preference
- SettingsScreen: Parent screen for all preferences
- Jetpack Compose - UI
- Preferences Datastore- Data Persistence
To add Settings-Compose to your project: Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Add the dependency:
dependencies {
implementation 'com.github.laksh-21:SettingsCompose:0.1.0'
}
Now to use the library, one first needs to create a Preferences Datastore
instance and a DatastoreManager
instance using that:
val Context.datastore: DataStore<Preferences> by preferencesDataStore(name = "settings")
val datastoreManager = DatastoreManager(datastore = this.datastore)
Then, you can provide the instance to the SettingsScreen
:
SettingsScreen(datastoreManager = datastoreManager) {
...
}
Now you're ready to use all the components of the library inside the scope of SettingsScreen
.
For every single component, you need to provide a SettingReference
like:
CheckboxReference
: uses aBoolean
valueSwitchReference
: uses aBoolean
valueListReference
: uses aString
valueMultiListReference
: uses aSet<String>
value
To create such reference, the key is a Preferences.Key<T>
type value and default value is a <T>
type value:
CheckboxReference(key = booleanPreferencesKey(name = "checkBoxName"), defaultValue = false)
The same reference can be used to query the current value of the Setting