|
| 1 | +package team.aliens.dms.android.core.designsystem |
| 2 | + |
| 3 | +import androidx.compose.foundation.ExperimentalFoundationApi |
| 4 | +import androidx.compose.foundation.gestures.snapping.rememberSnapFlingBehavior |
| 5 | +import androidx.compose.foundation.layout.Box |
| 6 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 7 | +import androidx.compose.foundation.layout.height |
| 8 | +import androidx.compose.foundation.lazy.LazyColumn |
| 9 | +import androidx.compose.foundation.lazy.rememberLazyListState |
| 10 | +import androidx.compose.material3.LocalContentColor |
| 11 | +import androidx.compose.material3.LocalTextStyle |
| 12 | +import androidx.compose.material3.Text |
| 13 | +import androidx.compose.runtime.Composable |
| 14 | +import androidx.compose.runtime.LaunchedEffect |
| 15 | +import androidx.compose.runtime.getValue |
| 16 | +import androidx.compose.runtime.mutableIntStateOf |
| 17 | +import androidx.compose.runtime.mutableStateOf |
| 18 | +import androidx.compose.runtime.remember |
| 19 | +import androidx.compose.runtime.setValue |
| 20 | +import androidx.compose.runtime.snapshotFlow |
| 21 | +import androidx.compose.ui.Alignment |
| 22 | +import 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 |
| 26 | +import androidx.compose.ui.graphics.Color |
| 27 | +import androidx.compose.ui.graphics.CompositingStrategy |
| 28 | +import androidx.compose.ui.graphics.graphicsLayer |
| 29 | +import androidx.compose.ui.layout.onSizeChanged |
| 30 | +import androidx.compose.ui.platform.LocalDensity |
| 31 | +import androidx.compose.ui.text.TextStyle |
| 32 | +import androidx.compose.ui.text.style.TextOverflow |
| 33 | +import kotlinx.coroutines.flow.distinctUntilChanged |
| 34 | +import kotlinx.coroutines.flow.map |
| 35 | + |
| 36 | +@OptIn(ExperimentalFoundationApi::class) |
| 37 | +@Composable |
| 38 | +fun Picker( |
| 39 | + items: List<String>, |
| 40 | + state: PickerState = rememberPickerState(), |
| 41 | + modifier: Modifier = Modifier, |
| 42 | + startIndex: Int = 0, |
| 43 | + visibleItemsCount: Int = 3, |
| 44 | + textModifier: Modifier = Modifier, |
| 45 | + textStyle: TextStyle = LocalTextStyle.current, |
| 46 | +) { |
| 47 | + val visibleItemsMiddle = visibleItemsCount / 2 |
| 48 | + val listScrollCount = Integer.MAX_VALUE |
| 49 | + val listScrollMiddle = listScrollCount / 2 |
| 50 | + val listStartIndex = listScrollMiddle - listScrollMiddle % items.size - visibleItemsMiddle + startIndex |
| 51 | + |
| 52 | + fun getItem(index: Int) = items[index % items.size] |
| 53 | + |
| 54 | + val listState = rememberLazyListState(initialFirstVisibleItemIndex = listStartIndex) |
| 55 | + val flingBehavior = rememberSnapFlingBehavior(lazyListState = listState) |
| 56 | + |
| 57 | + val itemHeightPixels = remember { mutableIntStateOf(0) } |
| 58 | + val itemHeightDp = pixelsToDp(itemHeightPixels.value) |
| 59 | + |
| 60 | + val fadingEdgeGradient = remember { |
| 61 | + Brush.verticalGradient( |
| 62 | + 0f to Color.Transparent, |
| 63 | + 0.5f to Color.Black, |
| 64 | + 1f to Color.Transparent |
| 65 | + ) |
| 66 | + } |
| 67 | + |
| 68 | + LaunchedEffect(listState) { |
| 69 | + snapshotFlow { listState.firstVisibleItemIndex } |
| 70 | + .map { index -> getItem(index + visibleItemsMiddle) } |
| 71 | + .distinctUntilChanged() |
| 72 | + .collect { item -> state.selectedItem = item } |
| 73 | + } |
| 74 | + |
| 75 | + Box(modifier = modifier) { |
| 76 | + LazyColumn( |
| 77 | + state = listState, |
| 78 | + flingBehavior = flingBehavior, |
| 79 | + horizontalAlignment = Alignment.CenterHorizontally, |
| 80 | + modifier = Modifier |
| 81 | + .fillMaxWidth() |
| 82 | + .height(itemHeightDp * visibleItemsCount) |
| 83 | + .fadingEdge(fadingEdgeGradient) |
| 84 | + ) { |
| 85 | + items(listScrollCount) { index -> |
| 86 | + Text( |
| 87 | + text = getItem(index), |
| 88 | + maxLines = 1, |
| 89 | + overflow = TextOverflow.Ellipsis, |
| 90 | + style = textStyle, |
| 91 | + modifier = Modifier |
| 92 | + .onSizeChanged { size -> itemHeightPixels.value = size.height } |
| 93 | + .then(textModifier) |
| 94 | + ) |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +private fun Modifier.fadingEdge(brush: Brush) = this |
| 101 | + .graphicsLayer(compositingStrategy = CompositingStrategy.Offscreen) |
| 102 | + .drawWithContent { |
| 103 | + drawContent() |
| 104 | + drawRect(brush = brush, blendMode = BlendMode.DstIn) |
| 105 | + } |
| 106 | + |
| 107 | +@Composable |
| 108 | +private fun pixelsToDp(pixels: Int) = with(LocalDensity.current) { pixels.toDp() } |
| 109 | + |
| 110 | +@Composable |
| 111 | +fun rememberPickerState() = remember { PickerState() } |
| 112 | + |
| 113 | +class PickerState { |
| 114 | + var selectedItem by mutableStateOf("") |
| 115 | +} |
0 commit comments