datastoreutils is a Kotlin Multiplatform utility library designed to simplify interaction with Jetpack DataStore Preferences, offering convenient APIs for common preference operations and seamless integration with Jetpack Compose.
datastoreutils aims to provide a robust and easy-to-use wrapper around Android's Jetpack DataStore Preferences. It abstracts away the complexities of DataStore, offering a clear IPreferencesRepository interface to manage individual preferences (IPreference) across different platforms. Additionally, it provides extensions specifically for Jetpack Compose, enabling reactive observation of preferences directly within your UI.
- Simplified Preferences Management: Provides
IPreferencesRepositoryto create and manageIPreferenceinstances for various data types. - Comprehensive Preference Operations: Each
IPreferencesupports:- Asynchronous saving of values (
save). - Deletion of preferences (
delete). - Reactive observation of preference changes via
Flow(get). - Atomic updates based on current state (
update). - Synchronous fetching of the current value (
currentValue). - Observation of key existence (
exists).
- Asynchronous saving of values (
- Clear All Preferences: Ability to clear all preferences managed by a repository (
clearAllPreferences). - Jetpack Compose Integration: The
composemodule offers@Composableextension functions to collect preference values asStateobjects, integrating smoothly with the Compose lifecycle.collectAsState: CollectsFlowvalues as ComposeState.collectAsStateWithLifecycle: CollectsFlowvalues as ComposeStaterespecting aLifecycleOwnerand minimum active state.
This library is designed for Kotlin Multiplatform projects and supports the following targets:
- Android (minSdk 23)
- iOS (x64, arm64, simulatorArm64)
- JVM
- macOS (x64, arm64)
Add the datastoreutils modules to your build.gradle.kts file.
include("uk.dominikdias.datastoreutils:core:latest")
include("uk.dominikdias.datastoreutils:compose:latest") // If you need Compose integrationsclass MyRepository(dataStore: DataStore<Preferences>): PreferencesRepository(dataStore) {
val foo = createPreference(stringPreferencesKey("foo"), "default_value")
val bar = createPreference(booleanPreferencesKey("bar"), true)
}