From 356e95be30f754913300992e460646a0e730c6ea Mon Sep 17 00:00:00 2001 From: Patrick Michalik <120058021+patrickmichalik@users.noreply.github.com> Date: Tue, 10 Dec 2024 21:39:23 +0100 Subject: [PATCH] Add `Preference.update`, convert Gradle files to Kotlin, and more Co-authored-by: Patryk Goworowski --- .github/workflows/check-formatting.yml | 16 +++++++ .github/workflows/run-ktlint.yml | 19 -------- .gitignore | 25 +++++----- .idea/gradle.xml | 20 -------- .idea/kotlinc.xml | 6 --- build.gradle | 5 -- build.gradle.kts | 5 ++ core/.gitignore | 1 + core/build.gradle | 47 ------------------- core/build.gradle.kts | 34 ++++++++++++++ .../opto/core/PreferenceImpl.kt | 17 +++---- .../opto/core/PreferenceManager.kt | 16 +++---- domain/.gitignore | 1 + domain/build.gradle | 23 --------- domain/build.gradle.kts | 17 +++++++ .../opto/domain/Preference.kt | 8 ++-- gradle.properties | 2 +- gradle/libs.versions.toml | 4 +- gradle/wrapper/gradle-wrapper.properties | 2 +- settings.gradle | 20 -------- settings.gradle.kts | 26 ++++++++++ 21 files changed, 139 insertions(+), 175 deletions(-) create mode 100644 .github/workflows/check-formatting.yml delete mode 100644 .github/workflows/run-ktlint.yml delete mode 100644 .idea/gradle.xml delete mode 100644 .idea/kotlinc.xml delete mode 100644 build.gradle create mode 100644 build.gradle.kts create mode 100644 core/.gitignore delete mode 100644 core/build.gradle create mode 100644 core/build.gradle.kts create mode 100644 domain/.gitignore delete mode 100644 domain/build.gradle create mode 100644 domain/build.gradle.kts delete mode 100644 settings.gradle create mode 100644 settings.gradle.kts diff --git a/.github/workflows/check-formatting.yml b/.github/workflows/check-formatting.yml new file mode 100644 index 0000000..f1fbb5c --- /dev/null +++ b/.github/workflows/check-formatting.yml @@ -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: 23 + 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 . diff --git a/.github/workflows/run-ktlint.yml b/.github/workflows/run-ktlint.yml deleted file mode 100644 index f59193f..0000000 --- a/.github/workflows/run-ktlint.yml +++ /dev/null @@ -1,19 +0,0 @@ -name: Run Ktlint -on: - push: - pull_request: -jobs: - run-ktlint: - runs-on: ubuntu-latest - continue-on-error: true - steps: - - uses: actions/setup-java@v4 - with: - java-version: 17 - distribution: zulu - - run: | - curl -sSLO https://github.com/pinterest/ktlint/releases/download/1.1.1/ktlint - chmod +x ktlint - sudo mv ktlint /usr/local/bin - - uses: actions/checkout@v4 - - run: ktlint diff --git a/.gitignore b/.gitignore index aa717fa..c8a3d6c 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/.idea/gradle.xml b/.idea/gradle.xml deleted file mode 100644 index c3861bb..0000000 --- a/.idea/gradle.xml +++ /dev/null @@ -1,20 +0,0 @@ - - - - - - - diff --git a/.idea/kotlinc.xml b/.idea/kotlinc.xml deleted file mode 100644 index 26aebe7..0000000 --- a/.idea/kotlinc.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - diff --git a/build.gradle b/build.gradle deleted file mode 100644 index b3687e7..0000000 --- a/build.gradle +++ /dev/null @@ -1,5 +0,0 @@ -plugins { - alias libs.plugins.androidLibrary apply false - alias libs.plugins.kotlinAndroid apply false - alias libs.plugins.kotlinJvm apply false -} diff --git a/build.gradle.kts b/build.gradle.kts new file mode 100644 index 0000000..c9880fa --- /dev/null +++ b/build.gradle.kts @@ -0,0 +1,5 @@ +plugins { + alias(libs.plugins.androidLibrary) apply false + alias(libs.plugins.kotlinAndroid) apply false + alias(libs.plugins.kotlinJvm) apply false +} diff --git a/core/.gitignore b/core/.gitignore new file mode 100644 index 0000000..84c048a --- /dev/null +++ b/core/.gitignore @@ -0,0 +1 @@ +/build/ diff --git a/core/build.gradle b/core/build.gradle deleted file mode 100644 index e3c6d23..0000000 --- a/core/build.gradle +++ /dev/null @@ -1,47 +0,0 @@ -plugins { - alias libs.plugins.androidLibrary - alias libs.plugins.kotlinAndroid - id "maven-publish" -} - -android { - namespace group - compileSdk 33 - defaultConfig { - minSdk 21 - targetSdk 33 - } - buildTypes { - release { - minifyEnabled 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() - } - } -} - -afterEvaluate { - publishing { - publications { - core(MavenPublication) { - from components.release - } - } - } -} - -dependencies { - api project(":domain") - implementation libs.preferencesDataStore -} diff --git a/core/build.gradle.kts b/core/build.gradle.kts new file mode 100644 index 0000000..f167ca6 --- /dev/null +++ b/core/build.gradle.kts @@ -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("core") { from(components["release"]) } } } +} + +dependencies { + api(project(":domain")) + implementation(libs.preferencesDataStore) +} diff --git a/core/src/main/kotlin/com/patrykandpatrick/opto/core/PreferenceImpl.kt b/core/src/main/kotlin/com/patrykandpatrick/opto/core/PreferenceImpl.kt index 0657f4f..8389aba 100644 --- a/core/src/main/kotlin/com/patrykandpatrick/opto/core/PreferenceImpl.kt +++ b/core/src/main/kotlin/com/patrykandpatrick/opto/core/PreferenceImpl.kt @@ -4,26 +4,27 @@ 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 -class PreferenceImpl( +public class PreferenceImpl( private val key: Preferences.Key, private val defaultValue: C, private val serialize: (C) -> S, private val deserialize: (S) -> C, - private val preferencesDataStore: DataStore, + private val dataStore: DataStore, ) : Preference { - private fun S?.deserializedOrDefault() = this?.let { deserialize(it) } ?: defaultValue + private fun S?.deserializedOrDefault() = if (this != null) deserialize(this) else defaultValue - fun getFromPreferences(preferences: Preferences) = preferences[key].deserializedOrDefault() + public fun get(preferences: Preferences): C = preferences[key].deserializedOrDefault() - override fun get() = preferencesDataStore.data.map(::getFromPreferences) + override fun get(): Flow = dataStore.data.map(::get) override suspend fun set(value: C) { - preferencesDataStore.edit { it[key] = serialize(value) } + dataStore.edit { it[key] = serialize(value) } } - suspend fun update(function: (C) -> C) { - preferencesDataStore.edit { it[key] = serialize(function(it[key].deserializedOrDefault())) } + override suspend fun update(function: (C) -> C) { + dataStore.edit { it[key] = serialize(function(it[key].deserializedOrDefault())) } } } diff --git a/core/src/main/kotlin/com/patrykandpatrick/opto/core/PreferenceManager.kt b/core/src/main/kotlin/com/patrykandpatrick/opto/core/PreferenceManager.kt index f07098a..c4e9610 100644 --- a/core/src/main/kotlin/com/patrykandpatrick/opto/core/PreferenceManager.kt +++ b/core/src/main/kotlin/com/patrykandpatrick/opto/core/PreferenceManager.kt @@ -3,18 +3,16 @@ package com.patrykandpatrick.opto.core import androidx.datastore.core.DataStore import androidx.datastore.preferences.core.Preferences -interface PreferenceManager { - val preferencesDataStore: DataStore +public interface PreferenceManager { + public val dataStore: DataStore - fun preference( - key: Preferences.Key, - defaultValue: S, - ) = preference(key = key, defaultValue = defaultValue, serialize = { it }, deserialize = { it }) - - fun preference( + public fun preference( key: Preferences.Key, defaultValue: C, serialize: (C) -> S, deserialize: (S) -> C, - ) = PreferenceImpl(key, defaultValue, serialize, deserialize, preferencesDataStore) + ): PreferenceImpl = PreferenceImpl(key, defaultValue, serialize, deserialize, dataStore) + + public fun preference(key: Preferences.Key, defaultValue: S): PreferenceImpl = + preference(key = key, defaultValue = defaultValue, serialize = { it }, deserialize = { it }) } diff --git a/domain/.gitignore b/domain/.gitignore new file mode 100644 index 0000000..84c048a --- /dev/null +++ b/domain/.gitignore @@ -0,0 +1 @@ +/build/ diff --git a/domain/build.gradle b/domain/build.gradle deleted file mode 100644 index 17c53fc..0000000 --- a/domain/build.gradle +++ /dev/null @@ -1,23 +0,0 @@ -plugins { - id "java-library" - alias libs.plugins.kotlinJvm - id "maven-publish" -} - -java { - sourceCompatibility = JavaVersion.VERSION_17 - targetCompatibility = JavaVersion.VERSION_17 - withSourcesJar() -} - -publishing { - publications { - domain(MavenPublication) { - from components.java - } - } -} - -dependencies { - implementation libs.coroutinesCore -} diff --git a/domain/build.gradle.kts b/domain/build.gradle.kts new file mode 100644 index 0000000..652e312 --- /dev/null +++ b/domain/build.gradle.kts @@ -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("domain") { from(components["java"]) } } } + +dependencies { implementation(libs.coroutinesCore) } diff --git a/domain/src/main/kotlin/com/patrykandpatrick/opto/domain/Preference.kt b/domain/src/main/kotlin/com/patrykandpatrick/opto/domain/Preference.kt index cc9f137..5014035 100644 --- a/domain/src/main/kotlin/com/patrykandpatrick/opto/domain/Preference.kt +++ b/domain/src/main/kotlin/com/patrykandpatrick/opto/domain/Preference.kt @@ -2,8 +2,10 @@ package com.patrykandpatrick.opto.domain import kotlinx.coroutines.flow.Flow -interface Preference { - fun get(): Flow +public interface Preference { + public fun get(): Flow - suspend fun set(value: T) + public suspend fun set(value: T) + + public suspend fun update(function: T.() -> T) } diff --git a/gradle.properties b/gradle.properties index e771e56..590ff72 100644 --- a/gradle.properties +++ b/gradle.properties @@ -3,4 +3,4 @@ android.useAndroidX=true group=com.patrykandpatrick.opto kotlin.code.style=official org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8 -version=1.1.0 +version=1.2.0 diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 4bc7852..5daa8a4 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,8 +1,8 @@ [versions] -agp = "8.7.2" +agp = "8.7.3" coroutines = "1.9.0" dataStore = "1.1.1" -kotlin = "2.0.21" +kotlin = "2.1.0" [libraries] coroutinesCore = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-core", version.ref = "coroutines" } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index df97d72..e2847c8 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.11.1-bin.zip networkTimeout=10000 validateDistributionUrl=true zipStoreBase=GRADLE_USER_HOME diff --git a/settings.gradle b/settings.gradle deleted file mode 100644 index 77bb58f..0000000 --- a/settings.gradle +++ /dev/null @@ -1,20 +0,0 @@ -pluginManagement { - repositories { - google() - gradlePluginPortal() - mavenCentral() - } -} - -dependencyResolutionManagement { - repositoriesMode.set RepositoriesMode.FAIL_ON_PROJECT_REPOS - repositories { - google() - mavenCentral() - } -} - -rootProject.name = "Opto" - -include ":core" -include ":domain" diff --git a/settings.gradle.kts b/settings.gradle.kts new file mode 100644 index 0000000..e96b46d --- /dev/null +++ b/settings.gradle.kts @@ -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")