From e5a0a0adb0f0a9c00a50ded34853490996ea7cc1 Mon Sep 17 00:00:00 2001 From: Christian Basler Date: Sun, 25 Aug 2024 21:55:05 +0200 Subject: [PATCH] Improvements try to make it run on Android (finally!) --- commonUI/build.gradle.kts | 42 ++++++++++--------- .../yaep/ui/common/pointer utils.android.kt | 17 ++++++++ .../kotlin/ch/dissem/yaep/ui/common/App.kt | 10 +---- .../ch/dissem/yaep/ui/common/pointer utils.kt | 8 ++++ .../ch/dissem/yaep/ui/common/selector.kt | 15 +++---- .../yaep/ui/common/pointer utils.jvm.kt | 15 +++++++ domain/build.gradle.kts | 2 + gradle/libs.versions.toml | 4 +- 8 files changed, 74 insertions(+), 39 deletions(-) create mode 100644 commonUI/src/androidMain/kotlin/ch/dissem/yaep/ui/common/pointer utils.android.kt create mode 100644 commonUI/src/commonMain/kotlin/ch/dissem/yaep/ui/common/pointer utils.kt create mode 100644 commonUI/src/jvmMain/kotlin/ch/dissem/yaep/ui/common/pointer utils.jvm.kt diff --git a/commonUI/build.gradle.kts b/commonUI/build.gradle.kts index d7d8e47..a6f7421 100644 --- a/commonUI/build.gradle.kts +++ b/commonUI/build.gradle.kts @@ -8,7 +8,7 @@ plugins { kotlin { jvmToolchain(libs.versions.jdk.get().toInt()) - jvm("desktop") + jvm() androidTarget() // @OptIn(ExperimentalWasmDsl::class) @@ -19,29 +19,33 @@ kotlin { // } sourceSets { - val androidMain by getting - val commonTest by getting + commonMain { + dependencies { + api(projects.domain) - commonMain.dependencies { - api(projects.domain) - - implementation(compose.components.resources) - implementation(compose.runtime) - implementation(compose.foundation) - implementation(compose.material3) - implementation(compose.ui) - implementation(compose.components.uiToolingPreview) + implementation(compose.components.resources) + implementation(compose.runtime) + implementation(compose.foundation) + implementation(compose.material3) + implementation(compose.ui) + implementation(compose.components.uiToolingPreview) // implementation(libs.compose.ui.text.googlefonts) - implementation(libs.logging) - } - androidMain.dependencies { - implementation(libs.androidx.compose.foundation) + implementation(libs.logging) + } } - commonTest.dependencies { - implementation(libs.kotlin.test) - implementation(libs.atrium) + androidMain { + dependencies { + implementation(libs.androidx.compose.foundation) + } + } + + commonTest { + dependencies { + implementation(libs.kotlin.test) + implementation(libs.atrium) + } } } } diff --git a/commonUI/src/androidMain/kotlin/ch/dissem/yaep/ui/common/pointer utils.android.kt b/commonUI/src/androidMain/kotlin/ch/dissem/yaep/ui/common/pointer utils.android.kt new file mode 100644 index 0000000..7670210 --- /dev/null +++ b/commonUI/src/androidMain/kotlin/ch/dissem/yaep/ui/common/pointer utils.android.kt @@ -0,0 +1,17 @@ +package ch.dissem.yaep.ui.common + +import androidx.compose.foundation.ExperimentalFoundationApi +import androidx.compose.foundation.combinedClickable +import androidx.compose.ui.Modifier + +@OptIn(ExperimentalFoundationApi::class) +actual fun Modifier.onPointerAction( + primary: () -> Unit, + secondary: () -> Unit +): Modifier { + return this + .combinedClickable( + onClick = primary, + onLongClick = secondary + ) +} \ No newline at end of file diff --git a/commonUI/src/commonMain/kotlin/ch/dissem/yaep/ui/common/App.kt b/commonUI/src/commonMain/kotlin/ch/dissem/yaep/ui/common/App.kt index 66c0d75..10c1159 100644 --- a/commonUI/src/commonMain/kotlin/ch/dissem/yaep/ui/common/App.kt +++ b/commonUI/src/commonMain/kotlin/ch/dissem/yaep/ui/common/App.kt @@ -1,10 +1,7 @@ package ch.dissem.yaep.ui.common import androidx.compose.foundation.BorderStroke -import androidx.compose.foundation.ExperimentalFoundationApi import androidx.compose.foundation.Image -import androidx.compose.foundation.PointerMatcher -import androidx.compose.foundation.PointerMatcher.Companion.mouse import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.aspectRatio @@ -14,7 +11,6 @@ import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.wrapContentHeight import androidx.compose.foundation.lazy.grid.GridCells import androidx.compose.foundation.lazy.grid.LazyVerticalGrid -import androidx.compose.foundation.onClick import androidx.compose.material3.CardDefaults import androidx.compose.material3.HorizontalDivider import androidx.compose.material3.MaterialTheme @@ -29,7 +25,6 @@ import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.draw.alpha import androidx.compose.ui.draw.shadow -import androidx.compose.ui.input.pointer.PointerButton.Companion.Secondary import androidx.compose.ui.unit.TextUnit import androidx.compose.ui.unit.TextUnitType import androidx.compose.ui.unit.dp @@ -186,11 +181,10 @@ fun PuzzleClues( } } -@OptIn(ExperimentalFoundationApi::class) -private fun Modifier.forClue(clue: DisplayClue) = this +private fun Modifier.forClue(clue: DisplayClue): Modifier = this .alpha(if (clue.isActive) 1f else 0.2f) .padding(8.dp) - .onClick(matcher = PointerMatcher.Primary + mouse(Secondary)) { clue.isActive = !clue.isActive } + .onEitherPointerAction { clue.isActive = !clue.isActive } @Composable fun HorizontalClue(modifier: Modifier = Modifier, clue: HorizontalClue, isClueViolated: Boolean) { diff --git a/commonUI/src/commonMain/kotlin/ch/dissem/yaep/ui/common/pointer utils.kt b/commonUI/src/commonMain/kotlin/ch/dissem/yaep/ui/common/pointer utils.kt new file mode 100644 index 0000000..b62afc3 --- /dev/null +++ b/commonUI/src/commonMain/kotlin/ch/dissem/yaep/ui/common/pointer utils.kt @@ -0,0 +1,8 @@ +package ch.dissem.yaep.ui.common + +import androidx.compose.ui.Modifier + +expect fun Modifier.onPointerAction(primary: () -> Unit = {}, secondary: () -> Unit = {}): Modifier + +fun Modifier.onEitherPointerAction(action: () -> Unit): Modifier = + onPointerAction(primary = action, secondary = action) diff --git a/commonUI/src/commonMain/kotlin/ch/dissem/yaep/ui/common/selector.kt b/commonUI/src/commonMain/kotlin/ch/dissem/yaep/ui/common/selector.kt index 54cfcd7..b23967b 100644 --- a/commonUI/src/commonMain/kotlin/ch/dissem/yaep/ui/common/selector.kt +++ b/commonUI/src/commonMain/kotlin/ch/dissem/yaep/ui/common/selector.kt @@ -1,6 +1,7 @@ package ch.dissem.yaep.ui.common -import androidx.compose.foundation.* +import androidx.compose.foundation.Canvas +import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.aspectRatio import androidx.compose.foundation.layout.fillMaxSize @@ -14,7 +15,6 @@ import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.draw.alpha import androidx.compose.ui.geometry.Offset -import androidx.compose.ui.input.pointer.PointerButton import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.drawText import androidx.compose.ui.text.rememberTextMeasurer @@ -23,7 +23,6 @@ import ch.dissem.yaep.domain.ItemClass import ch.dissem.yaep.ui.common.theme.emojiFontFamily import kotlin.math.min -@OptIn(ExperimentalFoundationApi::class) @Composable fun > Selector( modifier: Modifier = Modifier, @@ -48,13 +47,9 @@ fun > Selector( item = option.item, modifier = Modifier .alpha(if (option.enabled) 1f else 0.1f) - .combinedClickable( - onClick = { onSelectItem(option.item) }, - onLongClick = { option.enabled = false } - ) - .onClick( - matcher = PointerMatcher.mouse(PointerButton.Secondary), - onClick = { + .onPointerAction( + primary = { onSelectItem(option.item) }, + secondary = { if (option.enabled) { option.enabled = false onOptionRemoved(option.item) diff --git a/commonUI/src/jvmMain/kotlin/ch/dissem/yaep/ui/common/pointer utils.jvm.kt b/commonUI/src/jvmMain/kotlin/ch/dissem/yaep/ui/common/pointer utils.jvm.kt new file mode 100644 index 0000000..d1c1e02 --- /dev/null +++ b/commonUI/src/jvmMain/kotlin/ch/dissem/yaep/ui/common/pointer utils.jvm.kt @@ -0,0 +1,15 @@ +package ch.dissem.yaep.ui.common + +import androidx.compose.foundation.ExperimentalFoundationApi +import androidx.compose.foundation.PointerMatcher +import androidx.compose.foundation.PointerMatcher.Companion.mouse +import androidx.compose.foundation.onClick +import androidx.compose.ui.Modifier +import androidx.compose.ui.input.pointer.PointerButton.Companion.Secondary + +@OptIn(ExperimentalFoundationApi::class) +actual fun Modifier.onPointerAction(primary: () -> Unit, secondary: () -> Unit): Modifier { + return this + .onClick(matcher = PointerMatcher.Primary, onClick = primary) + .onClick(matcher = mouse(Secondary), onClick = secondary) +} \ No newline at end of file diff --git a/domain/build.gradle.kts b/domain/build.gradle.kts index 8c1b8b0..01b63c9 100644 --- a/domain/build.gradle.kts +++ b/domain/build.gradle.kts @@ -16,6 +16,8 @@ kotlin { // binaries.executable() // } +// linuxX64() + sourceSets { commonMain { dependencies { diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 6e2522c..6880ece 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -5,9 +5,9 @@ android-compileSdk = "34" android-minSdk = "24" android-targetSdk = "34" androidx-activityCompose = "1.9.1" -androidx-compose = "1.6.7" +androidx-compose = "1.6.8" compose-plugin = "1.6.11" -kotlin = "2.0.0" +kotlin = "2.0.20" coreKtx = "1.13.1" atrium = "1.2.0"