prefero

Modern preferences layer built on AndroidX DataStore, enabling minimal boilerplate, type-safe autogenerated preference accessors from serializable models, reactive flows, and single-file configuration.

Android JVMJVMKotlin/Native
GitHub stars0
Authorsteogor
Open issues0
LicenseApache License 2.0
Creation date6 months ago

Last activity6 months ago
Latest release1.0.0-alpha01 (6 months ago)

🦉 Prefero

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.


Maven Central Kotlin Build License


Features

  • Kotlin Multiplatform Ready: Use the same preferences code on Android, iOS, Desktop, and more.
  • 🧩 Built on AndroidX DataStore: Reliable, future-proof, and battle-tested storage.
  • 📝 Minimal Boilerplate: Configure and use DataStore with just a few lines of code.
  • 🔐 Type-Safe API: Autogenerate strongly-typed preference accessors from your @Serializable data models.
  • 🦉 Seamless Integration: Works out-of-the-box with KMP projects and multiplatform modules.

Getting Started

1. Add Prefero to Your Project

// 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")
}

2. Easy DataStore Configuration on KMP

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:

  • Single, consistent data file across platforms.
  • Smooth integration with Kotlin Multiplatform.
  • Minimal boilerplate for initialization and file handling.

3. Optional: Generate Type-Safe Preference Accessors

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...
}

Supported Preference Types

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.


Why Prefero?

  • Effortless multiplatform preferences with a single, unified API.
  • Type-safe wrappers generated automatically for your data models.
  • Minimal boilerplate and easy setup for DataStore in any KMP project.
  • Open source and MIT-licensed — ready for your next project!

License

Prefero is released under the MIT License.


Made with ♥ by @teogor

Android JVMJVMKotlin/Native
GitHub stars0
Authorsteogor
Open issues0
LicenseApache License 2.0
Creation date6 months ago

Last activity6 months ago
Latest release1.0.0-alpha01 (6 months ago)

🦉 Prefero

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.


Maven Central Kotlin Build License


Features

  • Kotlin Multiplatform Ready: Use the same preferences code on Android, iOS, Desktop, and more.
  • 🧩 Built on AndroidX DataStore: Reliable, future-proof, and battle-tested storage.
  • 📝 Minimal Boilerplate: Configure and use DataStore with just a few lines of code.
  • 🔐 Type-Safe API: Autogenerate strongly-typed preference accessors from your @Serializable data models.
  • 🦉 Seamless Integration: Works out-of-the-box with KMP projects and multiplatform modules.

Getting Started

1. Add Prefero to Your Project

// 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")
}

2. Easy DataStore Configuration on KMP

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:

  • Single, consistent data file across platforms.
  • Smooth integration with Kotlin Multiplatform.
  • Minimal boilerplate for initialization and file handling.

3. Optional: Generate Type-Safe Preference Accessors

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...
}

Supported Preference Types

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.


Why Prefero?

  • Effortless multiplatform preferences with a single, unified API.
  • Type-safe wrappers generated automatically for your data models.
  • Minimal boilerplate and easy setup for DataStore in any KMP project.
  • Open source and MIT-licensed — ready for your next project!

License

Prefero is released under the MIT License.


Made with ♥ by @teogor