-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWheelPicker.kt
132 lines (123 loc) · 4.85 KB
/
WheelPicker.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package com.compose.widget.picker
import androidx.compose.foundation.gestures.animateScrollBy
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.material.Divider
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.launch
import kotlin.math.abs
@Composable
fun <T> WheelPicker(
modifier: Modifier = Modifier,
items: List<T> = emptyList(),
selectedItem: T? = null,
itemHeight: Dp = 32.dp,
divider: NumberPickerDivider = NumberPickerDivider(),
itemStyles: ItemStyles = ItemStyles(),
onItemChanged: (T) -> Unit = {},
itemToString: (T) -> String = { it.toString() }
) {
val scope = rememberCoroutineScope()
var currItem by remember { mutableStateOf(selectedItem) }
var listHeightInPixels by remember { mutableStateOf(0) }
var itemHeightInPixels by remember { mutableStateOf(0) }
val listState = rememberSaveable(saver = LazyListState.Saver) {
if (items.isNotEmpty()) {
val centerList = Int.MAX_VALUE / 2 - (Int.MAX_VALUE / 2) % items.size
val selectedIndex = selectedItem?.let { items.indexOf(selectedItem) } ?: 0
LazyListState(
firstVisibleItemIndex = centerList + selectedIndex
)
} else {
LazyListState()
}
}
if (listState.isScrollInProgress.not() && itemHeightInPixels > 0) {
val needScrollTop = -listState.firstVisibleItemScrollOffset + itemHeightInPixels / 2
if (abs(listState.firstVisibleItemScrollOffset - itemHeightInPixels / 2) > 1) {
LaunchedEffect(key1 = listState) {
scope.launch {
listState.animateScrollBy(needScrollTop.toFloat())
if (items[listState.firstVisibleItemIndex % items.size] != currItem) {
currItem = items[listState.firstVisibleItemIndex % items.size]
onItemChanged(currItem!!)
}
}
}
}
}
Box(modifier = modifier, contentAlignment = Alignment.Center) {
if (items.isNotEmpty()) {
LazyColumn(
contentPadding = PaddingValues(top = (listHeightInPixels / 2).toDp()),
state = listState,
modifier = Modifier
.fillMaxWidth(1f)
.onGloballyPositioned {
if (listHeightInPixels < itemHeightInPixels) {
listHeightInPixels = it.size.height
}
}
) {
items(Int.MAX_VALUE) { i ->
val index = i % items.size
Box(
contentAlignment = Alignment.Center,
modifier = Modifier
.fillMaxWidth(1f)
.height(itemHeight)
.onGloballyPositioned {
if (itemHeightInPixels == 0) {
itemHeightInPixels = it.size.height
}
}
) {
Text(
text = itemToString(items[index]),
style = if (i == listState.firstVisibleItemIndex) itemStyles.selectedTextStyle else itemStyles.defaultTextStyle
)
}
}
}
}
if (divider.showed) {
Divider(
color = divider.color,
modifier = Modifier
.fillMaxWidth(1f)
.offset(y = -itemHeight / 2)
.offset(x = -divider.indent),
thickness = divider.thickness,
startIndent = divider.indent * 2
)
Divider(
color = divider.color, modifier = Modifier
.fillMaxWidth(1f)
.offset(y = itemHeight / 2)
.offset(x = -divider.indent),
thickness = divider.thickness,
startIndent = divider.indent * 2
)
}
}
}
class NumberPickerDivider(
val showed: Boolean = false,
val color: Color = Color.Transparent,
val thickness: Dp = 1.dp,
val indent: Dp = 0.dp
)
class ItemStyles(
val defaultTextStyle: TextStyle = TextStyle.Default,
val selectedTextStyle: TextStyle = TextStyle.Default
)