@@ -20,18 +20,14 @@ import androidx.compose.runtime.setValue
2020import androidx.compose.runtime.snapshotFlow
2121import androidx.compose.ui.Alignment
2222import androidx.compose.ui.Modifier
23- import androidx.compose.ui.draw.drawWithContent
24- import androidx.compose.ui.graphics.BlendMode
25- import androidx.compose.ui.graphics.Brush
2623import androidx.compose.ui.graphics.Color
27- import androidx.compose.ui.graphics.CompositingStrategy
28- import androidx.compose.ui.graphics.graphicsLayer
2924import androidx.compose.ui.platform.LocalDensity
3025import androidx.compose.ui.text.TextStyle
3126import androidx.compose.ui.text.rememberTextMeasurer
3227import androidx.compose.ui.text.style.TextOverflow
3328import androidx.compose.ui.unit.dp
3429import com.getcode.theme.CodeTheme
30+ import com.getcode.ui.utils.fadingEdge
3531import com.getcode.ui.utils.measured
3632import com.getcode.util.vibration.LocalVibrator
3733import kotlinx.coroutines.Dispatchers
@@ -86,34 +82,26 @@ fun <T> Picker(
8682
8783 var itemHeight by remember { mutableStateOf(0 .dp) }
8884
89- val fadingEdgeGradient = remember {
90- Brush .verticalGradient(
91- 0f to Color .Transparent ,
92- 0.5f to Color .Black ,
93- 1f to Color .Transparent
94- )
95- }
96-
9785 val vibrator = LocalVibrator .current
9886
9987 LaunchedEffect (items) {
10088 snapshotFlow { listState.firstVisibleItemIndex to listState.layoutInfo.visibleItemsInfo.lastOrNull()?.index }
101- .debounce(300 .milliseconds)
10289 .map { (first, last) ->
10390 val index = ((first + (last ? : first)) / 2 ).coerceIn(1 .. items.lastIndex)
10491 getItem(index)
10592 }
10693 .distinctUntilChanged()
94+ .onEach { vibrator.tick() }
95+ .debounce(300 .milliseconds)
10796 .onEach { item ->
108- vibrator.tick()
10997 withContext(Dispatchers .Main ) {
11098 state.selectedItem = state.items.find { state.labelForItem(it) == item }
11199 }
112100 }.launchIn(this )
113101 }
114102
115103 val textMeasurer = rememberTextMeasurer()
116- val buffer = with (LocalDensity .current) { CodeTheme .dimens.grid.x4.roundToPx() }
104+ val buffer = with (LocalDensity .current) { CodeTheme .dimens.grid.x4.roundToPx() }
117105 val itemWidthPixels = remember(items) {
118106 items.maxOfOrNull {
119107 textMeasurer.measure(text = it, style = textStyle, maxLines = 1 ).size.width + buffer
@@ -129,7 +117,7 @@ fun <T> Picker(
129117 modifier = Modifier
130118 .fillMaxWidth()
131119 .height(itemHeight * visibleItemsCount)
132- .fadingEdge(fadingEdgeGradient )
120+ .fadingEdge()
133121 ) {
134122 itemsIndexed(items) { _, item ->
135123 Text (
@@ -154,21 +142,14 @@ fun <T> Picker(
154142 style = textStyle.copy(Color .White ),
155143 maxLines = 1 ,
156144 overflow = TextOverflow .Ellipsis ,
157- modifier = Modifier .align(Alignment .Center )
145+ modifier = Modifier
146+ .align(Alignment .Center )
158147 .padding(end = pixelsToDp(itemWidthPixels + buffer))
159148 )
160149 }
161150 }
162151 }
163-
164152}
165153
166- private fun Modifier.fadingEdge (brush : Brush ) = this
167- .graphicsLayer(compositingStrategy = CompositingStrategy .Offscreen )
168- .drawWithContent {
169- drawContent()
170- drawRect(brush = brush, blendMode = BlendMode .DstIn )
171- }
172-
173154@Composable
174155private fun pixelsToDp (pixels : Int ) = with (LocalDensity .current) { pixels.toDp() }
0 commit comments