diff --git a/.github/workflows/check-formatting.yml b/.github/workflows/check-formatting.yml new file mode 100644 index 0000000..4b17353 --- /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: 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 . 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..e6d66b0 100644 --- a/.gitignore +++ b/.gitignore @@ -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 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/DataStorePreference.kt b/core/src/main/kotlin/com/patrykandpatrick/opto/core/DataStorePreference.kt new file mode 100644 index 0000000..5e762d3 --- /dev/null +++ b/core/src/main/kotlin/com/patrykandpatrick/opto/core/DataStorePreference.kt @@ -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( + private val key: Preferences.Key, + private val defaultValue: C, + private val serialize: (C) -> S, + private val deserialize: (S) -> C, + private val dataStore: DataStore, +) : Preference { + private fun S?.deserializedOrDefault() = if (this != null) deserialize(this) else defaultValue + + public fun get(preferences: Preferences): C = preferences[key].deserializedOrDefault() + + override fun get(): Flow = 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())) } + } +} diff --git a/core/src/main/kotlin/com/patrykandpatrick/opto/core/PreferenceImpl.kt b/core/src/main/kotlin/com/patrykandpatrick/opto/core/PreferenceImpl.kt deleted file mode 100644 index 0657f4f..0000000 --- a/core/src/main/kotlin/com/patrykandpatrick/opto/core/PreferenceImpl.kt +++ /dev/null @@ -1,29 +0,0 @@ -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.map - -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, -) : Preference { - private fun S?.deserializedOrDefault() = this?.let { deserialize(it) } ?: defaultValue - - fun getFromPreferences(preferences: Preferences) = preferences[key].deserializedOrDefault() - - override fun get() = preferencesDataStore.data.map(::getFromPreferences) - - override suspend fun set(value: C) { - preferencesDataStore.edit { it[key] = serialize(value) } - } - - suspend fun update(function: (C) -> C) { - preferencesDataStore.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..2f2d4c6 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,17 @@ 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) + ): DataStorePreference = + DataStorePreference(key, defaultValue, serialize, deserialize, dataStore) + + public fun preference(key: Preferences.Key, defaultValue: S): DataStorePreference = + 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/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")