Is this code correct? Performant? Basically we ha...
# compose-android
b
Is this code correct? Performant? Basically we have a tv guide box where this triangle and line goes across the box as the show progress.
Copy code
@Composable
private fun ScheduleTimeTracker(width: Dp, model: ItemModel) {
    val trackerWidth = 8.dp
    val trackerHeight = 6.dp
    val position = remember { mutableStateOf(width.times(model.timeLeftInPercent())) }  //so 30 minutes into an hour should timeLeftInPercent would be .50f
    val offsetState: Dp by animateDpAsState(
        position.value,
        animationSpec = tween<Dp>(model.timeLeftInMillis(), easing = FastOutLinearInEasing),  //30 minute show it should take this vertical line 30 minutes to travel the horizontal length of the box
        label = "time tracker"
    )

    Box(
        modifier = Modifier
            .width(trackerWidth)
            .offset(x = offsetState),
        contentAlignment = Alignment.TopCenter
    ) {
        Spacer(
            Modifier
                .width(1.dp)
                .fillMaxHeight()
                .background(Color.Red)
        )
        Triangle(color = Color.Red, Modifier.size(trackerWidth, height = trackerHeight))
    }

    LaunchedEffect(true) {
        position.value = width.minus(4.dp)
    }
}
I ask also, because at the top of this function I had a println and I noticed that it was printing it with every part of the animation. I think that is expected because it recomposes to animate the vertical line, but wanted to verify.
f
it will be more performant if you use the offset lambda,
Copy code
modifier = modifier.offset { IntOffset(x = offsetState, y = 0) }