KotPreferences
4.0.0indexedSimplifies preference management with a fluent API, supporting custom data objects, default values, and type safety for streamlined configuration handling.
Simplifies preference management with a fluent API, supporting custom data objects, default values, and type safety for streamlined configuration handling.
This library provides following main features:
kotlin flowssuspend functionsState, MutableState or StateFlow| Dependency | Version |
|---|---|
| Kotlin | 2.4.0 |
| Jetbrains Compose | 1.11.1 |
:warning: Following experimental annotations are used:
- OptIn
kotlinx.cinterop.ExperimentalForeignApi(3x)I try to use as less experimental features as possible, but in this case the ones above are needed!
Simply add the dependencies inside your build.gradle.kts file.
SettingsModelSettingsModel in plain kotlin (flows + suspending functions)// 1) get a flow
val flow = Preferences.someString.flow
// 2) read/update values by suspend functions
scope.launch(Dispatchers.IO) {
val value = Preferences.someInt.read()
Preferences.someInt.update(value + 1)
}
SettingsModel in views (e.g. with Lifecycle Scope)SettingsModel in composeval name = Preferences.someString.collectAsState()
val name = Preferences.someString.collectAsStateWithLifecycle()
// simply use the state inside your composables, the state will change whenever the setting behind it will change
val name = Preferences.someString.asMutableState()
// simple use and even update the state now
A full demo is included inside the demo module, it shows nearly every usage with working examples.
Check out the API documentation.
You can find more libraries (all multiplatform) of mine that all do work together nicely here.
| Module | android | iOS | windows | macOS | wasm | Notes |
|---|
| core | ✅ | ✅ | ✅ | ✅ | ✅ | This is the core module that holds the android app context |
| storage-datastore | ✅ | ✅ | ✅ | ✅ | ❌ | Provides a storage implementation based on jetpack datastore |
| storage-keyvalue | ✅ | ✅ | ✅ | ✅ | ✅ | Provides a storage implementation based on a simple key-value text file |
| extension-compose | ✅ | ✅ | ✅ | ✅ | ✅ | Provides composable extensions |
| encryption-aes | ✅ | ❌ | ❌ | ❌ | ❌ | Provides AES based encryption |
| Jetbrains Compose Material3 | 1.9.0 |
Define the dependencies inside your libs.versions.toml file.
[versions]
kotpreferences = "<LATEST-VERSION>"
[libraries]
kotpreferences-core = { module = "io.github.mflisar.kotpreferences:core", version.ref = "kotpreferences" }
kotpreferences-storage-datastore = { module = "io.github.mflisar.kotpreferences:storage-datastore", version.ref = "kotpreferences" }
kotpreferences-storage-keyvalue = { module = "io.github.mflisar.kotpreferences:storage-keyvalue", version.ref = "kotpreferences" }
kotpreferences-extension-compose = { module = "io.github.mflisar.kotpreferences:extension-compose", version.ref = "kotpreferences" }
kotpreferences-encryption-aes = { module = "io.github.mflisar.kotpreferences:encryption-aes", version.ref = "kotpreferences" }
And then use the definitions in your projects build.gradle.kts file like following:
implementation(libs.kotpreferences.core)
implementation(libs.kotpreferences.storage.datastore)
implementation(libs.kotpreferences.storage.keyvalue)
implementation(libs.kotpreferences.extension.compose)
implementation(libs.kotpreferences.encryption.aes)
val kotpreferences = "<LATEST-VERSION>"
implementation("io.github.mflisar.kotpreferences:core:${kotpreferences}")
implementation("io.github.mflisar.kotpreferences:storage-datastore:${kotpreferences}")
implementation("io.github.mflisar.kotpreferences:storage-keyvalue:${kotpreferences}")
implementation("io.github.mflisar.kotpreferences:extension-compose:${kotpreferences}")
implementation("io.github.mflisar.kotpreferences:encryption-aes:${kotpreferences}")
// Depending on the platform:
// - common: DataStoreStorage.create(name = "preferences")
// - jvm: DataStoreStorage.create(folder = File(System.getProperty("user.dir")), name = "preferences")
// - android/iOS: DataStoreStorage.create(name = "preferences")
// - ...
object Preferences : SettingsModel(DataStoreStorage.create(name = "preferences")) {
// main data types
val someString by stringPref("value")
val someBool by boolPref(false)
val someInt by intPref(123)
val someLong by intPref(123L)
val someFloat by intPref(123f)
val someDouble by intPref(123.0)
// enum
val someEnum by enumPref(Enum.Value1)
// custom
val someCustomClass1 by anyStringPref(TestClass.CONVERTER, TestClass()) // converts TestClass to a string and saves this string
val someCustomClass2 by anyIntPref(TestClass.CONVERTER, TestClass()) // converts TestClass to an int and saves this int
val someCustomClass3 by anyLongPref(TestClass.CONVERTER, TestClass()) // converts TestClass to a long and saves this long
// sets
val someStringSet by stringSetPref(setOf("a"))
val someIntSet by intSetPref(setOf(1))
val someLongSet by longSetPref(setOf(1L))
val someFloatSet by floatSetPref(setOf(1f))
val someDoubleSet by doubleSetPref(setOf(1.0))
// NULLABLE vs NON NULLABLE
val nonNullableString by stringPref()
val nullableString by nullableStringPref()
val nonNullableInt by intPref()
val nullableInt by nullableIntPref()
val nonNullableFloat by floatPref()
val nullableFloat by nullableFloatPref()
val nonNullableDouble by doublePref()
val nullableDouble by nullableDoublePref()
val nonNullableLong by longPref()
val nullableLong by nullableLongPref()
val nonNullableBool by boolPref()
val nullableBool by nullableBoolPref()
// custom
val someCustomClass4 by nullableAnyStringPref(TestClass.CONVERTER, TestClass())
val someCustomClass5 by nullableAnyIntPref(TestClass.CONVERTER, TestClass())
val someCustomClass6 by nullableAnyLongPref(TestClass.CONVERTER, TestClass())
}
// 1) simply observe a setting
Preferences.someString.observe(lifecycleScope) {
L.d { "someString = $it"}
}
// 2) direct read (not recommended if not necessary but may be useful in many cases)
// => simply returns read() in a blocking way)
val name = Preferences.someString.value
// 3) observe a setting once
Preferences.someString.observeOnce(lifecycleScope) {
L.d { "someString = $it"}
}
// 4) observe ALL settings
Preferences.changes.onEach {
L.d { "[ALL SETTINGS OBSERVER] Setting '${it.setting.key}' changed its value to ${it.value}" }
}.launchIn(lifecycleScope)
// 5) observe SOME settings
Preferences.changes
.filter {
it.setting == Preferences.someString ||
it.setting == Preferences.someBool
}.onEach {
L.d { "[SOME SETTINGS OBSERVER] Setting '${it.setting.key}' changed its value to ${it.value}" }
}.launchIn(lifecycleScope)
// 6) read multiple settings in a suspending way
lifecycleScope.launch(Dispatchers.IO) {
val someString = Preferences.someString.read()
val someBool = Preferences.someBool.read()
}
Surfaced from shared tags and platforms — no rankings paid for.