
Modern preferences layer built on AndroidX DataStore, enabling minimal boilerplate, type-safe autogenerated preference accessors from serializable models, reactive flows, and single-file configuration.
Prefero is a modern preferences library built on AndroidX DataStore, designed for Kotlin Multiplatform projects. Effortlessly configure cross-platform preferences with minimal boilerplate and maximum type-safety.
@Serializable data models.// build.gradle.kts (Kotlin DSL example)
dependencies {
implementation("dev.teogor.prefero:prefero-core:1.0.0-alpha01")
// If using annotation processing for typed preferences:
ksp("dev.teogor.prefero:prefero-processor:1.0.0-alpha01")
}Configure your DataStore with just a few lines in your Kotlin Multiplatform project:
prefero {
fileName = "demo.prefero_pb"
}This configuration automatically sets up a DataStore Preferences instance with the given file name, compatible with Android, iOS, and other supported targets.
You get:
Prefero can autogenerate fully type-safe wrappers for your preferences using KSP.
Just annotate your @Serializable data classes with @GeneratePreference:
import dev.teogor.prefero.core.GeneratePreference
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@GeneratePreference
@Serializable
data class UserSettings(
@SerialName("dark_mode")
val darkMode: Boolean = false,
@SerialName("username")
val username: String = ""
)After building your project, Prefero will generate a class like:
class UserSettings_Prefero(
private val dataStore: DataStore<Preferences>,
) {
private val defaults = UserSettings() // defaults instance
private val darkModeKey: Preferences.Key<Boolean> =
booleanPreferencesKey("dark_mode")
val darkModeFlow: Flow<Boolean>
get() = dataStore.data.map { it[darkModeKey] ?: defaults.darkMode }
suspend fun getDarkMode(): Boolean =
dataStore.data.map { it[darkModeKey] ?: defaults.darkMode }.first()
suspend fun setDarkMode(value: Boolean) =
dataStore.edit { it[darkModeKey] = value }
// Same for username...
}Prefero supports all primitive and core types from AndroidX Preferences DataStore:
| Kotlin Type | Corresponding Key Function |
|---|---|
Boolean |
booleanPreferencesKey |
Int |
intPreferencesKey |
Long |
longPreferencesKey |
Float |
floatPreferencesKey |
Double |
doublePreferencesKey |
String |
stringPreferencesKey |
Set<String> |
stringSetPreferencesKey |
ByteArray |
byteArrayPreferencesKey |
These types directly align with official AndroidX DataStore APIs and behaviors.
Prefero is released under the MIT License.
Made with β₯ by @teogor
Prefero is a modern preferences library built on AndroidX DataStore, designed for Kotlin Multiplatform projects. Effortlessly configure cross-platform preferences with minimal boilerplate and maximum type-safety.
@Serializable data models.// build.gradle.kts (Kotlin DSL example)
dependencies {
implementation("dev.teogor.prefero:prefero-core:1.0.0-alpha01")
// If using annotation processing for typed preferences:
ksp("dev.teogor.prefero:prefero-processor:1.0.0-alpha01")
}Configure your DataStore with just a few lines in your Kotlin Multiplatform project:
prefero {
fileName = "demo.prefero_pb"
}This configuration automatically sets up a DataStore Preferences instance with the given file name, compatible with Android, iOS, and other supported targets.
You get:
Prefero can autogenerate fully type-safe wrappers for your preferences using KSP.
Just annotate your @Serializable data classes with @GeneratePreference:
import dev.teogor.prefero.core.GeneratePreference
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@GeneratePreference
@Serializable
data class UserSettings(
@SerialName("dark_mode")
val darkMode: Boolean = false,
@SerialName("username")
val username: String = ""
)After building your project, Prefero will generate a class like:
class UserSettings_Prefero(
private val dataStore: DataStore<Preferences>,
) {
private val defaults = UserSettings() // defaults instance
private val darkModeKey: Preferences.Key<Boolean> =
booleanPreferencesKey("dark_mode")
val darkModeFlow: Flow<Boolean>
get() = dataStore.data.map { it[darkModeKey] ?: defaults.darkMode }
suspend fun getDarkMode(): Boolean =
dataStore.data.map { it[darkModeKey] ?: defaults.darkMode }.first()
suspend fun setDarkMode(value: Boolean) =
dataStore.edit { it[darkModeKey] = value }
// Same for username...
}Prefero supports all primitive and core types from AndroidX Preferences DataStore:
| Kotlin Type | Corresponding Key Function |
|---|---|
Boolean |
booleanPreferencesKey |
Int |
intPreferencesKey |
Long |
longPreferencesKey |
Float |
floatPreferencesKey |
Double |
doublePreferencesKey |
String |
stringPreferencesKey |
Set<String> |
stringSetPreferencesKey |
ByteArray |
byteArrayPreferencesKey |
These types directly align with official AndroidX DataStore APIs and behaviors.
Prefero is released under the MIT License.
Made with β₯ by @teogor