Google breaks, I fix

Slider fixes
This commit is contained in:
sadellie 2023-07-31 10:24:12 +03:00
parent 9cfb35b03e
commit 4a7bb0cd1d

View File

@ -57,7 +57,10 @@ fun UnittoSlider(
onValueChange: (Float) -> Unit, onValueChange: (Float) -> Unit,
onValueChangeFinished: (Float) -> Unit = {} onValueChangeFinished: (Float) -> Unit = {}
) { ) {
val animated = animateFloatAsState(targetValue = value) val animated = animateFloatAsState(
targetValue = value.roundToInt().toFloat(),
animationSpec = spring()
)
Slider( Slider(
value = animated.value, value = animated.value,
@ -66,7 +69,6 @@ fun UnittoSlider(
valueRange = valueRange, valueRange = valueRange,
onValueChangeFinished = { onValueChangeFinished(animated.value) }, onValueChangeFinished = { onValueChangeFinished(animated.value) },
track = { sliderPosition -> SquigglyTrack(sliderPosition) }, track = { sliderPosition -> SquigglyTrack(sliderPosition) },
steps = valueRange.endInclusive.roundToInt(),
) )
} }
@ -81,9 +83,8 @@ private fun SquigglyTrack(
val coroutineScope = rememberCoroutineScope() val coroutineScope = rememberCoroutineScope()
var direct by remember { mutableFloatStateOf(0.72f) } var direct by remember { mutableFloatStateOf(0.72f) }
val animatedDirect = animateFloatAsState(direct, spring(stiffness = Spring.StiffnessLow)) val animatedDirect = animateFloatAsState(direct, spring(stiffness = Spring.StiffnessLow))
val slider = sliderState.valueRange.endInclusive
LaunchedEffect(sliderState.valueRange.endInclusive) { LaunchedEffect(sliderState.value) {
coroutineScope.launch { coroutineScope.launch {
delay(200L) delay(200L)
direct *= -1 direct *= -1
@ -97,10 +98,15 @@ private fun SquigglyTrack(
) { ) {
val width = size.width val width = size.width
val height = size.height val height = size.height
val initialOffset = strokeWidth / 2
val thumbPosition = width
.times(sliderState.value)
.div(sliderState.valueRange.endInclusive)
.minus(initialOffset)
val path = Path().apply { val path = Path().apply {
moveTo( moveTo(
x = strokeWidth / 2, x = initialOffset,
y = height.times(0.5f) y = height.times(0.5f)
) )
val amount = ceil(width.div(eachWaveWidth)) val amount = ceil(width.div(eachWaveWidth))
@ -110,7 +116,6 @@ private fun SquigglyTrack(
relativeQuadraticBezierTo( relativeQuadraticBezierTo(
dx1 = eachWaveWidth * 0.5f, dx1 = eachWaveWidth * 0.5f,
// 0.75, because 1.0 was clipping out of bound for some reason
dy1 = height.times(peek), dy1 = height.times(peek),
dx2 = eachWaveWidth, dx2 = eachWaveWidth,
dy2 = 0f dy2 = 0f
@ -121,7 +126,7 @@ private fun SquigglyTrack(
clipRect( clipRect(
top = 0f, top = 0f,
left = 0f, left = 0f,
right = width.times(slider), right = thumbPosition,
bottom = height, bottom = height,
clipOp = ClipOp.Intersect clipOp = ClipOp.Intersect
) { ) {
@ -134,7 +139,7 @@ private fun SquigglyTrack(
drawLine( drawLine(
color = unfilledColor, color = unfilledColor,
start = Offset(width.times(slider), height.times(0.5f)), start = Offset(thumbPosition, height.times(0.5f)),
end = Offset(width, height.times(0.5f)), end = Offset(width, height.times(0.5f)),
strokeWidth = strokeWidth, strokeWidth = strokeWidth,
cap = StrokeCap.Round cap = StrokeCap.Round
@ -148,11 +153,11 @@ private fun SquigglyTrack(
@Preview(device = "spec:width=1920dp,height=1080dp,dpi=480") @Preview(device = "spec:width=1920dp,height=1080dp,dpi=480")
@Composable @Composable
private fun PreviewNewSlider() { private fun PreviewNewSlider() {
var currentValue by remember { mutableFloatStateOf(0.9f) } var currentValue by remember { mutableFloatStateOf(9f) }
UnittoSlider( UnittoSlider(
value = currentValue, value = currentValue,
valueRange = 0f..1f, valueRange = 0f..16f,
onValueChange = { currentValue = it } onValueChange = { currentValue = it }
) )
} }