Skip to content

Commit

Permalink
Add Preference.update, rename PreferenceImpl to `DataStorePrefere…
Browse files Browse the repository at this point in the history
…nce`, and more
  • Loading branch information
patrickmichalik committed Nov 11, 2024
1 parent b0dd67b commit b4927f8
Show file tree
Hide file tree
Showing 17 changed files with 140 additions and 172 deletions.
23 changes: 13 additions & 10 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
*.iml
.DS_Store
.cxx
.externalNativeBuild
.gradle
.cxx/
.externalNativeBuild/
/.gradle/
/.idea/assetWizardSettings.xml
/.idea/caches
/.idea/caches/
/.idea/compiler.xml
/.idea/gradle.xml
/.idea/kotlinc.xml
/.idea/libraries
/.idea/migrations.xml
/.idea/modules.xml
/.idea/navEditor.xml
/.idea/shelf
/.idea/runConfigurations.xml
/.idea/shelf/
/.idea/workspace.xml
/build
/captures
/core/build
/domain/build
local.properties
/build/
/captures/
/local.properties
20 changes: 0 additions & 20 deletions .idea/gradle.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/kotlinc.xml

This file was deleted.

5 changes: 0 additions & 5 deletions build.gradle

This file was deleted.

5 changes: 5 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
plugins {
alias(libs.plugins.androidLibrary) apply false
alias(libs.plugins.kotlinAndroid) apply false
alias(libs.plugins.kotlinJvm) apply false
}
1 change: 1 addition & 0 deletions core/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build/
47 changes: 0 additions & 47 deletions core/build.gradle

This file was deleted.

34 changes: 34 additions & 0 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
plugins {
alias(libs.plugins.androidLibrary)
alias(libs.plugins.kotlinAndroid)
id("maven-publish")
}

android {
namespace = group.toString()
compileSdk = 34
defaultConfig { minSdk = 21 }
buildTypes {
release {
isMinifyEnabled = false
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"))
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
kotlinOptions { jvmTarget = JavaVersion.VERSION_17.toString() }
publishing { singleVariant("release") { withSourcesJar() } }
}

kotlin { explicitApi() }

afterEvaluate {
publishing { publications { create<MavenPublication>("core") { from(components["release"]) } } }
}

dependencies {
api(project(":domain"))
implementation(libs.preferencesDataStore)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.patrykandpatrick.opto.core

import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.edit
import com.patrykandpatrick.opto.domain.Preference
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map

public class DataStorePreference<C, S>(
private val key: Preferences.Key<S>,
private val defaultValue: C,
private val serialize: (C) -> S,
private val deserialize: (S) -> C,
private val dataStore: DataStore<Preferences>,
) : Preference<C> {
private fun S?.deserializedOrDefault() = if (this != null) deserialize(this) else defaultValue

public fun get(preferences: Preferences): C = preferences[key].deserializedOrDefault()

override fun get(): Flow<C> = dataStore.data.map(::get)

override suspend fun set(value: C) {
dataStore.edit { it[key] = serialize(value) }
}

override suspend fun update(function: (C) -> C) {
dataStore.edit { it[key] = serialize(function(it[key].deserializedOrDefault())) }
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@ package com.patrykandpatrick.opto.core
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences

interface PreferenceManager {
val preferencesDataStore: DataStore<Preferences>
public interface PreferenceManager {
public val dataStore: DataStore<Preferences>

fun <S> preference(
key: Preferences.Key<S>,
defaultValue: S,
) = preference(key = key, defaultValue = defaultValue, serialize = { it }, deserialize = { it })

fun <C, S> preference(
public fun <C, S> preference(
key: Preferences.Key<S>,
defaultValue: C,
serialize: (C) -> S,
deserialize: (S) -> C,
) = PreferenceImpl(key, defaultValue, serialize, deserialize, preferencesDataStore)
): DataStorePreference<C, S> =
DataStorePreference(key, defaultValue, serialize, deserialize, dataStore)

public fun <S> preference(key: Preferences.Key<S>, defaultValue: S): DataStorePreference<S, S> =
preference(key = key, defaultValue = defaultValue, serialize = { it }, deserialize = { it })
}
1 change: 1 addition & 0 deletions domain/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build/
23 changes: 0 additions & 23 deletions domain/build.gradle

This file was deleted.

17 changes: 17 additions & 0 deletions domain/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
plugins {
id("java-library")
alias(libs.plugins.kotlinJvm)
id("maven-publish")
}

java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
withSourcesJar()
}

kotlin { explicitApi() }

publishing { publications { create<MavenPublication>("domain") { from(components["java"]) } } }

dependencies { implementation(libs.coroutinesCore) }
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package com.patrykandpatrick.opto.domain

import kotlinx.coroutines.flow.Flow

interface Preference<T> {
fun get(): Flow<T>
public interface Preference<T> {
public fun get(): Flow<T>

suspend fun set(value: T)
public suspend fun set(value: T)

public suspend fun update(function: T.() -> T)
}
20 changes: 0 additions & 20 deletions settings.gradle

This file was deleted.

26 changes: 26 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
pluginManagement {
repositories {
google {
content {
includeGroupByRegex("com\\.android.*")
includeGroupByRegex("com\\.google.*")
includeGroupByRegex("androidx.*")
}
}
gradlePluginPortal()
mavenCentral()
}
}

@Suppress("UnstableApiUsage")
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
}

rootProject.name = "Opto"

include(":core", ":domain")

0 comments on commit b4927f8

Please sign in to comment.