-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
Preference.update
, rename PreferenceImpl
to `DataStorePrefere…
…nce`, and more
- Loading branch information
1 parent
b0dd67b
commit 0861559
Showing
20 changed files
with
158 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
name: Check formatting | ||
on: | ||
push: | ||
pull_request: | ||
jobs: | ||
check-formatting: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: actions/setup-java@v4 | ||
with: | ||
java-version: 21 | ||
distribution: zulu | ||
- run: | | ||
curl -sSL -o ktfmt.jar https://github.com/facebook/ktfmt/releases/download/v0.53/ktfmt-0.53-jar-with-dependencies.jar | ||
java -jar ktfmt.jar --dry-run --kotlinlang-style --set-exit-if-changed . |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/libraries | ||
/.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 |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build/ |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
30 changes: 30 additions & 0 deletions
30
core/src/main/kotlin/com/patrykandpatrick/opto/core/DataStorePreference.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())) } | ||
} | ||
} |
29 changes: 0 additions & 29 deletions
29
core/src/main/kotlin/com/patrykandpatrick/opto/core/PreferenceImpl.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build/ |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |