Skip to content

Commit 74637df

Browse files
committed
chore(fc): tick on each item change
Signed-off-by: Brandon McAnsh <[email protected]>
1 parent af29c91 commit 74637df

File tree

2 files changed

+22
-35
lines changed

2 files changed

+22
-35
lines changed

ui/components/src/main/kotlin/com/getcode/ui/components/picker/Picker.kt

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,14 @@ import androidx.compose.runtime.setValue
2020
import androidx.compose.runtime.snapshotFlow
2121
import androidx.compose.ui.Alignment
2222
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
2623
import androidx.compose.ui.graphics.Color
27-
import androidx.compose.ui.graphics.CompositingStrategy
28-
import androidx.compose.ui.graphics.graphicsLayer
2924
import androidx.compose.ui.platform.LocalDensity
3025
import androidx.compose.ui.text.TextStyle
3126
import androidx.compose.ui.text.rememberTextMeasurer
3227
import androidx.compose.ui.text.style.TextOverflow
3328
import androidx.compose.ui.unit.dp
3429
import com.getcode.theme.CodeTheme
30+
import com.getcode.ui.utils.fadingEdge
3531
import com.getcode.ui.utils.measured
3632
import com.getcode.util.vibration.LocalVibrator
3733
import 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
174155
private fun pixelsToDp(pixels: Int) = with(LocalDensity.current) { pixels.toDp() }

ui/components/src/main/kotlin/com/getcode/ui/utils/Modifier.kt

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -366,12 +366,18 @@ fun LazyGridState.isVerticallyScrolledToStart(): Boolean {
366366
return firstItem == null || firstItem.offset.y == 0
367367
}
368368

369-
fun Modifier.footerShadow() = this.drawWithContent {
370-
drawContent()
371-
drawRect(
372-
brush = Brush.verticalGradient(
373-
endY = size.height * 0.12f,
374-
colors = listOf(Color(0x10000000), Color.Transparent),
375-
),
376-
)
377-
}
369+
@Composable
370+
fun Modifier.fadingEdge(
371+
brush: Brush = remember {
372+
Brush.verticalGradient(
373+
0f to Color.Transparent,
374+
0.5f to Color.Black,
375+
1f to Color.Transparent
376+
)
377+
}
378+
) = this
379+
.graphicsLayer(compositingStrategy = CompositingStrategy.Offscreen)
380+
.drawWithContent {
381+
drawContent()
382+
drawRect(brush = brush, blendMode = BlendMode.DstIn)
383+
}

0 commit comments

Comments
 (0)