From a7f80b23c48c620a0d12b85204ea18975b667aa5 Mon Sep 17 00:00:00 2001 From: darkokoa Date: Wed, 6 Mar 2024 00:25:55 +0800 Subject: [PATCH 1/6] Fix `calculateAnimatedRotationX` --- .../datetimewheelpicker/core/WheelPicker.kt | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/datetime-wheel-picker/src/commonMain/kotlin/dev/darkokoa/datetimewheelpicker/core/WheelPicker.kt b/datetime-wheel-picker/src/commonMain/kotlin/dev/darkokoa/datetimewheelpicker/core/WheelPicker.kt index b686444..96b093d 100644 --- a/datetime-wheel-picker/src/commonMain/kotlin/dev/darkokoa/datetimewheelpicker/core/WheelPicker.kt +++ b/datetime-wheel-picker/src/commonMain/kotlin/dev/darkokoa/datetimewheelpicker/core/WheelPicker.kt @@ -68,11 +68,6 @@ internal fun WheelPicker( flingBehavior = flingBehavior ) { items(count) { index -> - val rotationX = calculateAnimatedRotationX( - lazyListState = lazyListState, - index = index, - rowCount = rowCount - ) Box( modifier = Modifier .height(size.height / rowCount) @@ -85,7 +80,11 @@ internal fun WheelPicker( ) ) .graphicsLayer { - this.rotationX = rotationX + rotationX = calculateAnimatedRotationX( + lazyListState = lazyListState, + index = index, + rowCount = rowCount + ) }, contentAlignment = Alignment.Center ) { @@ -150,13 +149,9 @@ private fun calculateAnimatedRotationX( val distanceToCenterIndex = index - centerIndex - val distanceToIndexSnap = abs(distanceToCenterIndex) * singleViewPortHeight.toInt() - when { - distanceToCenterIndex > 0 -> centerIndexOffset - distanceToCenterIndex <= 0 -> -centerIndexOffset - else -> 0 - } + val distanceToIndexSnap = distanceToCenterIndex * singleViewPortHeight.toInt() - centerIndexOffset - val animatedRotationX = -20f * (distanceToIndexSnap / singleViewPortHeight) + val animatedRotationX = -22 * (distanceToIndexSnap / singleViewPortHeight) return if (animatedRotationX.isNaN()) { 0f From f596f944d9e1ff85390aa7d2d18c26f4cfd3e9c7 Mon Sep 17 00:00:00 2001 From: darkokoa Date: Wed, 6 Mar 2024 23:52:00 +0800 Subject: [PATCH 2/6] Merge `calculateAnimatedAlpha` and `calculateAnimatedRotationX` Merged `calculateAnimatedAlpha()` and `calculateAnimatedRotationX()` into a single function to return both the new Alpha and RotationX simultaneously. This change simplifies the code and improves performance. --- .../datetimewheelpicker/core/WheelPicker.kt | 62 +++++-------------- 1 file changed, 16 insertions(+), 46 deletions(-) diff --git a/datetime-wheel-picker/src/commonMain/kotlin/dev/darkokoa/datetimewheelpicker/core/WheelPicker.kt b/datetime-wheel-picker/src/commonMain/kotlin/dev/darkokoa/datetimewheelpicker/core/WheelPicker.kt index 96b093d..8d9eb22 100644 --- a/datetime-wheel-picker/src/commonMain/kotlin/dev/darkokoa/datetimewheelpicker/core/WheelPicker.kt +++ b/datetime-wheel-picker/src/commonMain/kotlin/dev/darkokoa/datetimewheelpicker/core/WheelPicker.kt @@ -68,23 +68,19 @@ internal fun WheelPicker( flingBehavior = flingBehavior ) { items(count) { index -> + val (newAlpha, newRotationX) = calculateAnimatedAlphaAndRotationX( + lazyListState = lazyListState, + index = index, + rowCount = rowCount + ) + Box( modifier = Modifier .height(size.height / rowCount) .width(size.width) - .alpha( - calculateAnimatedAlpha( - lazyListState = lazyListState, - index = index, - rowCount = rowCount - ) - ) + .alpha(newAlpha) .graphicsLayer { - rotationX = calculateAnimatedRotationX( - lazyListState = lazyListState, - index = index, - rowCount = rowCount - ) + rotationX = newRotationX }, contentAlignment = Alignment.Center ) { @@ -105,11 +101,11 @@ private fun calculateSnappedItemIndex(lazyListState: LazyListState): Int { } @Composable -private fun calculateAnimatedAlpha( +private fun calculateAnimatedAlphaAndRotationX( lazyListState: LazyListState, index: Int, rowCount: Int -): Float { +): Pair { val layoutInfo = remember { derivedStateOf { lazyListState.layoutInfo } }.value val viewPortHeight = layoutInfo.viewportSize.height.toFloat() @@ -120,44 +116,18 @@ private fun calculateAnimatedAlpha( val distanceToCenterIndex = index - centerIndex - val distanceToIndexSnap = abs(distanceToCenterIndex) * singleViewPortHeight.toInt() - when { - distanceToCenterIndex > 0 -> centerIndexOffset - distanceToCenterIndex <= 0 -> -centerIndexOffset - else -> 0 - } + val distanceToIndexSnap = distanceToCenterIndex * singleViewPortHeight.toInt() - centerIndexOffset + val distanceToIndexSnapAbs = abs(distanceToIndexSnap) - return if (distanceToIndexSnap in 0..singleViewPortHeight.toInt()) { - 1.2f - (distanceToIndexSnap / singleViewPortHeight) + val animatedAlpha = if (abs(distanceToIndexSnap) in 0..singleViewPortHeight.toInt()) { + 1.2f - (distanceToIndexSnapAbs / singleViewPortHeight) } else { 0.2f } -} - -@Composable -private fun calculateAnimatedRotationX( - lazyListState: LazyListState, - index: Int, - rowCount: Int -): Float { - - val layoutInfo = remember { derivedStateOf { lazyListState.layoutInfo } }.value - val viewPortHeight = layoutInfo.viewportSize.height.toFloat() - val singleViewPortHeight = viewPortHeight / rowCount - - val centerIndex = remember { derivedStateOf { lazyListState.firstVisibleItemIndex } }.value - val centerIndexOffset = remember { derivedStateOf { lazyListState.firstVisibleItemScrollOffset } }.value - val distanceToCenterIndex = index - centerIndex + val animatedRotationX = (-22 * (distanceToIndexSnap / singleViewPortHeight)).takeUnless { it.isNaN() } ?: 0f - val distanceToIndexSnap = distanceToCenterIndex * singleViewPortHeight.toInt() - centerIndexOffset - - val animatedRotationX = -22 * (distanceToIndexSnap / singleViewPortHeight) - - return if (animatedRotationX.isNaN()) { - 0f - } else { - animatedRotationX - } + return animatedAlpha to animatedRotationX } object WheelPickerDefaults { From aaad1fa6de7f0b7192da656f84648475667480f4 Mon Sep 17 00:00:00 2001 From: darkokoa Date: Thu, 7 Mar 2024 22:42:02 +0800 Subject: [PATCH 3/6] Scrollback animatedRotationX parameter --- .../kotlin/dev/darkokoa/datetimewheelpicker/core/WheelPicker.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/datetime-wheel-picker/src/commonMain/kotlin/dev/darkokoa/datetimewheelpicker/core/WheelPicker.kt b/datetime-wheel-picker/src/commonMain/kotlin/dev/darkokoa/datetimewheelpicker/core/WheelPicker.kt index 8d9eb22..1c1b4ad 100644 --- a/datetime-wheel-picker/src/commonMain/kotlin/dev/darkokoa/datetimewheelpicker/core/WheelPicker.kt +++ b/datetime-wheel-picker/src/commonMain/kotlin/dev/darkokoa/datetimewheelpicker/core/WheelPicker.kt @@ -125,7 +125,7 @@ private fun calculateAnimatedAlphaAndRotationX( 0.2f } - val animatedRotationX = (-22 * (distanceToIndexSnap / singleViewPortHeight)).takeUnless { it.isNaN() } ?: 0f + val animatedRotationX = (-20 * (distanceToIndexSnap / singleViewPortHeight)).takeUnless { it.isNaN() } ?: 0f return animatedAlpha to animatedRotationX } From 1c9b754213accca2c764829c48d6a797e1b49e71 Mon Sep 17 00:00:00 2001 From: darkokoa Date: Thu, 7 Mar 2024 23:25:57 +0800 Subject: [PATCH 4/6] Upgrade and remove libs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - agp 8.2.2 -> 8.3.0 - compose 1.5.12 -> 1.6.0 - androidx-appcompat 🚮 - compose-uitooling 🚮 --- datetime-wheel-picker/build.gradle.kts | 8 -------- gradle/libs.versions.toml | 8 ++------ gradle/wrapper/gradle-wrapper.properties | 2 +- sample/composeApp/build.gradle.kts | 8 -------- sample/composeApp/src/androidMain/AndroidManifest.xml | 2 +- 5 files changed, 4 insertions(+), 24 deletions(-) diff --git a/datetime-wheel-picker/build.gradle.kts b/datetime-wheel-picker/build.gradle.kts index 3e52958..dae23f1 100644 --- a/datetime-wheel-picker/build.gradle.kts +++ b/datetime-wheel-picker/build.gradle.kts @@ -58,9 +58,7 @@ kotlin { } androidMain.dependencies { - implementation(libs.androidx.appcompat) implementation(libs.androidx.activityCompose) - implementation(libs.compose.uitooling) } jvmMain.dependencies { @@ -94,12 +92,6 @@ android { sourceCompatibility = JavaVersion.VERSION_17 targetCompatibility = JavaVersion.VERSION_17 } - buildFeatures { - compose = true - } - composeOptions { - kotlinCompilerExtensionVersion = "1.5.10" - } } compose.desktop { diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index e37c486..b82c534 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,19 +1,15 @@ [versions] kotlin = "1.9.22" -compose = "1.5.12" -agp = "8.2.2" -androidx-appcompat = "1.6.1" +compose = "1.6.0" +agp = "8.3.0" androidx-activityCompose = "1.8.2" -compose-uitooling = "1.5.4" kotlinx-datetime = "0.5.0" maven-publish = "0.27.0" [libraries] -androidx-appcompat = { module = "androidx.appcompat:appcompat", version.ref = "androidx-appcompat" } androidx-activityCompose = { module = "androidx.activity:activity-compose", version.ref = "androidx-activityCompose" } -compose-uitooling = { module = "androidx.compose.ui:ui-tooling", version.ref = "compose-uitooling" } kotlinx-datetime = { module = "org.jetbrains.kotlinx:kotlinx-datetime", version.ref = "kotlinx-datetime" } [plugins] diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index da55e4b..c5622d6 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.5-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/sample/composeApp/build.gradle.kts b/sample/composeApp/build.gradle.kts index 740b211..3ed1b97 100644 --- a/sample/composeApp/build.gradle.kts +++ b/sample/composeApp/build.gradle.kts @@ -55,9 +55,7 @@ kotlin { } androidMain.dependencies { - implementation(libs.androidx.appcompat) implementation(libs.androidx.activityCompose) - implementation(libs.compose.uitooling) } jvmMain.dependencies { @@ -96,12 +94,6 @@ android { sourceCompatibility = JavaVersion.VERSION_17 targetCompatibility = JavaVersion.VERSION_17 } - buildFeatures { - compose = true - } - composeOptions { - kotlinCompilerExtensionVersion = "1.5.10" - } } compose.desktop { diff --git a/sample/composeApp/src/androidMain/AndroidManifest.xml b/sample/composeApp/src/androidMain/AndroidManifest.xml index d315d8d..1fea4ef 100644 --- a/sample/composeApp/src/androidMain/AndroidManifest.xml +++ b/sample/composeApp/src/androidMain/AndroidManifest.xml @@ -5,7 +5,7 @@ android:name=".AndroidApp" android:icon="@android:drawable/ic_menu_compass" android:label="datetime-wheel-picker" - android:theme="@style/Theme.AppCompat.DayNight.NoActionBar"> + android:theme="@android:style/Theme.Material.NoActionBar"> Date: Thu, 7 Mar 2024 23:49:47 +0800 Subject: [PATCH 5/6] Remove compose.components.resources --- datetime-wheel-picker/build.gradle.kts | 6 +++--- sample/composeApp/build.gradle.kts | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/datetime-wheel-picker/build.gradle.kts b/datetime-wheel-picker/build.gradle.kts index dae23f1..611d5b2 100644 --- a/datetime-wheel-picker/build.gradle.kts +++ b/datetime-wheel-picker/build.gradle.kts @@ -41,15 +41,15 @@ kotlin { sourceSets { all { languageSettings { - optIn("org.jetbrains.compose.resources.ExperimentalResourceApi") +// optIn("org.jetbrains.compose.resources.ExperimentalResourceApi") } } commonMain.dependencies { implementation(compose.runtime) implementation(compose.material3) implementation(compose.materialIconsExtended) - @OptIn(org.jetbrains.compose.ExperimentalComposeLibrary::class) - implementation(compose.components.resources) +// @OptIn(org.jetbrains.compose.ExperimentalComposeLibrary::class) +// implementation(compose.components.resources) implementation(libs.kotlinx.datetime) } diff --git a/sample/composeApp/build.gradle.kts b/sample/composeApp/build.gradle.kts index 3ed1b97..3ff34c6 100644 --- a/sample/composeApp/build.gradle.kts +++ b/sample/composeApp/build.gradle.kts @@ -36,7 +36,7 @@ kotlin { sourceSets { all { languageSettings { - optIn("org.jetbrains.compose.resources.ExperimentalResourceApi") +// optIn("org.jetbrains.compose.resources.ExperimentalResourceApi") } } commonMain.dependencies { @@ -45,8 +45,8 @@ kotlin { implementation(compose.runtime) implementation(compose.material3) implementation(compose.materialIconsExtended) - @OptIn(org.jetbrains.compose.ExperimentalComposeLibrary::class) - implementation(compose.components.resources) +// @OptIn(org.jetbrains.compose.ExperimentalComposeLibrary::class) +// implementation(compose.components.resources) implementation(libs.kotlinx.datetime) } From 5939e9a6d8646bfcf8cc1febd9132bb3ea503cc5 Mon Sep 17 00:00:00 2001 From: darkokoa Date: Thu, 7 Mar 2024 23:50:44 +0800 Subject: [PATCH 6/6] 1.0.0-alpha02 --- gradle.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index 25eea80..4f8b351 100644 --- a/gradle.properties +++ b/gradle.properties @@ -24,7 +24,7 @@ org.jetbrains.compose.experimental.jscanvas.enabled=true # Maven Central GROUP=io.github.darkokoa POM_ARTIFACT_ID=datetime-wheel-picker -VERSION_NAME=1.0.0-alpha01 +VERSION_NAME=1.0.0-alpha02 POM_NAME=DateTime Wheel Picker POM_INCEPTION_YEAR=2024