Skip to content

Commit 70fe254

Browse files
committed
.
1 parent bec145d commit 70fe254

7 files changed

Lines changed: 81 additions & 71 deletions

File tree

app/build.gradle

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ plugins {
44
}
55

66
android {
7-
compileSdk 32
7+
compileSdk 33
88

99
defaultConfig {
1010
applicationId "com.commandiron.wheelpickercompose"
1111
minSdk 23
12-
targetSdk 32
12+
targetSdk 33
1313
versionCode 1
1414
versionName "1.0"
1515

@@ -36,7 +36,7 @@ android {
3636
compose true
3737
}
3838
composeOptions {
39-
kotlinCompilerExtensionVersion compose_version
39+
kotlinCompilerExtensionVersion compose_compiler_version
4040
}
4141
packagingOptions {
4242
resources {
@@ -48,17 +48,17 @@ android {
4848
dependencies {
4949

5050
implementation 'androidx.core:core-ktx:1.7.0'
51-
implementation "androidx.compose.ui:ui:$compose_version"
51+
implementation "androidx.compose.ui:ui:$compose_ui_version"
5252
implementation "androidx.compose.material3:material3:1.0.0-alpha15"
53-
implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
53+
implementation "androidx.compose.ui:ui-tooling-preview:$compose_ui_version"
5454
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
5555
implementation 'androidx.activity:activity-compose:1.3.1'
5656
testImplementation 'junit:junit:4.13.2'
5757
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
5858
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
59-
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
60-
debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
61-
debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_version"
59+
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_ui_version"
60+
debugImplementation "androidx.compose.ui:ui-tooling:$compose_ui_version"
61+
debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_ui_version"
6262

6363
implementation project(':wheel-picker-compose')
6464
// implementation 'com.github.commandiron:WheelPickerCompose:1.0.9'

app/src/main/java/com/commandiron/wheelpickercompose/MainActivity.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ class MainActivity : ComponentActivity() {
3333
WheelPicker(
3434
count = 6,
3535
onScrollFinished = { snappedIndex ->
36+
println(snappedIndex)
3637
snappedIndex
3738
},
3839
selectorEnabled = true
39-
) { index, snappedIndex ->
40-
println(snappedIndex)
40+
) { index ->
4141
Text(text = "Test $index")
4242
}
4343
}

build.gradle

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
buildscript {
22
ext {
3-
compose_version = '1.1.1'
3+
compose_ui_version = '1.3.1'
4+
compose_compiler_version = '1.3.2'
45
}
56
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
67
plugins {
7-
id 'com.android.application' version '7.2.1' apply false
8-
id 'com.android.library' version '7.2.1' apply false
9-
id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
8+
id 'com.android.application' version '7.3.0' apply false
9+
id 'com.android.library' version '7.3.0' apply false
10+
id 'org.jetbrains.kotlin.android' version '1.7.20' apply false
1011
}
1112

1213
task clean(type: Delete) {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Wed Sep 07 10:30:27 TRT 2022
1+
#Mon Nov 14 07:26:13 TRT 2022
22
distributionBase=GRADLE_USER_HOME
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip
44
distributionPath=wrapper/dists
55
zipStorePath=wrapper/dists
66
zipStoreBase=GRADLE_USER_HOME

wheel-picker-compose/build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ plugins {
55
}
66

77
android {
8-
compileSdk 32
8+
compileSdk 33
99

1010
defaultConfig {
1111
minSdk 23
12-
targetSdk 32
12+
targetSdk 33
1313

1414
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1515
consumerProguardFiles "consumer-rules.pro"
@@ -36,12 +36,12 @@ android {
3636
compose true
3737
}
3838
composeOptions {
39-
kotlinCompilerExtensionVersion compose_version
39+
kotlinCompilerExtensionVersion compose_compiler_version
4040
}
4141
}
4242

4343
dependencies {
44-
implementation "androidx.compose.ui:ui:$compose_version"
44+
implementation "androidx.compose.ui:ui:$compose_ui_version"
4545
implementation "androidx.compose.material3:material3:1.0.0-alpha15"
4646

4747
implementation "dev.chrisbanes.snapper:snapper:0.3.0"
Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
package com.commandiron.wheel_picker_compose
22

33
import androidx.compose.foundation.BorderStroke
4+
import androidx.compose.foundation.border
45
import androidx.compose.foundation.layout.*
56
import androidx.compose.foundation.lazy.LazyColumn
67
import androidx.compose.foundation.lazy.LazyListState
78
import androidx.compose.foundation.lazy.rememberLazyListState
89
import androidx.compose.foundation.shape.RoundedCornerShape
9-
import androidx.compose.material3.*
10-
import androidx.compose.runtime.*
10+
import androidx.compose.material3.MaterialTheme
11+
import androidx.compose.material3.Surface
12+
import androidx.compose.runtime.Composable
13+
import androidx.compose.runtime.LaunchedEffect
1114
import androidx.compose.ui.Alignment
1215
import androidx.compose.ui.Modifier
1316
import androidx.compose.ui.graphics.Color
1417
import androidx.compose.ui.graphics.Shape
1518
import androidx.compose.ui.unit.DpSize
1619
import androidx.compose.ui.unit.dp
17-
import dev.chrisbanes.snapper.*
20+
import dev.chrisbanes.snapper.SnapperLayoutInfo
21+
import dev.chrisbanes.snapper.rememberLazyListSnapperLayoutInfo
22+
import dev.chrisbanes.snapper.rememberSnapperFlingBehavior
1823

1924
@Composable
2025
fun WheelPicker(
@@ -27,23 +32,18 @@ fun WheelPicker(
2732
selectorColor: Color = MaterialTheme.colorScheme.primary.copy(alpha = 0.2f),
2833
selectorBorder: BorderStroke? = BorderStroke(1.dp, MaterialTheme.colorScheme.primary),
2934
onScrollFinished: (snappedIndex: Int) -> Int? = { null },
30-
content: @Composable BoxScope.(index: Int, snappedIndex: Int) -> Unit,
35+
content: @Composable BoxScope.(index: Int) -> Unit,
3136
) {
32-
var snappedIndex by remember { mutableStateOf(0) }
33-
val lazyListState = rememberLazyListState(
34-
initialFirstVisibleItemIndex = startIndex
35-
)
36-
val snapperFlingBehavior = rememberSnapperFlingBehavior(
37-
lazyListState = lazyListState,
38-
snapIndex = {
39-
snapperLayoutInfo: SnapperLayoutInfo,
40-
snappedStartIndex: Int,
41-
snappedTargetIndex: Int ->
37+
val lazyListState = rememberLazyListState(startIndex)
38+
val snapperLayoutInfo = rememberLazyListSnapperLayoutInfo(lazyListState = lazyListState)
39+
val isScrollInProgress = lazyListState.isScrollInProgress
4240

43-
snappedIndex = snappedTargetIndex
44-
snappedTargetIndex
41+
LaunchedEffect(isScrollInProgress) {
42+
if(!isScrollInProgress) {
43+
onScrollFinished(calculateSelectedItem(snapperLayoutInfo)?: startIndex)
4544
}
46-
)
45+
}
46+
4747
Box(
4848
modifier = modifier,
4949
contentAlignment = Alignment.Center
@@ -60,10 +60,12 @@ fun WheelPicker(
6060
LazyColumn(
6161
modifier = Modifier
6262
.height(size.height)
63-
.width(size.width),
63+
.width(size.width).border(1.dp, Color.Red),
6464
state = lazyListState,
6565
contentPadding = PaddingValues(vertical = size.height / 3),
66-
flingBehavior = snapperFlingBehavior
66+
flingBehavior = rememberSnapperFlingBehavior(
67+
lazyListState = lazyListState
68+
)
6769
){
6870
items(count){ index ->
6971
Box(
@@ -72,9 +74,16 @@ fun WheelPicker(
7274
.width(size.width),
7375
contentAlignment = Alignment.Center
7476
) {
75-
content(index, lazyListState.layoutInfo.)
77+
content(index)
7678
}
7779
}
7880
}
7981
}
82+
}
83+
84+
private fun calculateSelectedItem(snapperLayoutInfo: SnapperLayoutInfo): Int? {
85+
val currentItemIndex = snapperLayoutInfo.currentItem?.index
86+
return if(currentItemIndex != null) {
87+
if(snapperLayoutInfo.currentItem?.offset != 0) currentItemIndex + 1 else currentItemIndex
88+
} else null
8089
}

wheel-picker-compose/src/main/java/com/commandiron/wheel_picker_compose/WheelTextPicker.kt

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,35 +27,35 @@ fun WheelTextPicker(
2727
selectorBorder: BorderStroke? = BorderStroke(1.dp, MaterialTheme.colorScheme.primary),
2828
onScrollFinished: (snappedIndex: Int) -> Int? = { null },
2929
) {
30-
WheelPicker(
31-
modifier = modifier,
32-
size = size,
33-
startIndex = startIndex,
34-
count = texts.size,
35-
selectorEnabled = selectorEnabled,
36-
selectorShape = selectorShape,
37-
selectorColor = selectorColor,
38-
selectorBorder = selectorBorder,
39-
onScrollFinished = onScrollFinished
40-
){ index, snappedIndex ->
41-
Text(
42-
text = texts[index],
43-
style = textStyle,
44-
color = textColor.copy(
45-
alpha = when(snappedIndex){
46-
index + 1 -> {
47-
0.2f
48-
}
49-
index -> {
50-
1f
51-
}
52-
index - 1 -> {
53-
0.2f
54-
}
55-
else -> 0.2f
56-
}
57-
),
58-
maxLines = 1
59-
)
60-
}
30+
// WheelPicker(
31+
// modifier = modifier,
32+
// size = size,
33+
// startIndex = startIndex,
34+
// count = texts.size,
35+
// selectorEnabled = selectorEnabled,
36+
// selectorShape = selectorShape,
37+
// selectorColor = selectorColor,
38+
// selectorBorder = selectorBorder,
39+
// onScrollFinished = onScrollFinished
40+
// ){ index, snappedIndex ->
41+
// Text(
42+
// text = texts[index],
43+
// style = textStyle,
44+
// color = textColor.copy(
45+
// alpha = when(snappedIndex){
46+
// index + 1 -> {
47+
// 0.2f
48+
// }
49+
// index -> {
50+
// 1f
51+
// }
52+
// index - 1 -> {
53+
// 0.2f
54+
// }
55+
// else -> 0.2f
56+
// }
57+
// ),
58+
// maxLines = 1
59+
// )
60+
// }
6161
}

0 commit comments

Comments
 (0)