@@ -3,10 +3,13 @@ package com.puzzle.onboarding.ui.components
33import androidx.annotation.RawRes
44import androidx.compose.runtime.Composable
55import androidx.compose.runtime.LaunchedEffect
6+ import androidx.compose.runtime.State
7+ import androidx.compose.runtime.derivedStateOf
68import androidx.compose.runtime.getValue
79import androidx.compose.runtime.mutableStateOf
810import androidx.compose.runtime.remember
911import androidx.compose.runtime.setValue
12+ import androidx.compose.runtime.snapshotFlow
1013import androidx.compose.ui.Modifier
1114import androidx.compose.ui.layout.ContentScale
1215import androidx.compose.ui.tooling.preview.Preview
@@ -30,29 +33,35 @@ internal fun StopAtProgressLottie(
3033 LottieCompositionSpec .RawRes (lottieRes)
3134 )
3235
33- val animProgress by animateLottieCompositionAsState(
36+ val animProgressState : State < Float > = animateLottieCompositionAsState(
3437 composition = composition,
3538 iterations = 1 ,
3639 isPlaying = shouldPlay,
3740 restartOnPlay = true
3841 )
3942
4043 var isStopped by remember { mutableStateOf(false ) }
41-
4244 var stoppedProgress by remember { mutableStateOf(0f ) }
4345
44- LaunchedEffect (animProgress, shouldPlay) {
46+ val hasReachedTarget by remember {
47+ derivedStateOf { animProgressState.value >= targetProgress }
48+ }
49+
50+ LaunchedEffect (shouldPlay) {
4551 if (shouldPlay) {
4652 isStopped = false
4753 }
4854
49- if (! isStopped && animProgress >= targetProgress) {
50- isStopped = true
51- stoppedProgress = targetProgress
52- }
55+ snapshotFlow { hasReachedTarget to isStopped }
56+ .collect { (reached, alreadyStopped) ->
57+ if (! alreadyStopped && reached) {
58+ isStopped = true
59+ stoppedProgress = targetProgress
60+ }
61+ }
5362 }
5463
55- val finalProgress = if (isStopped) stoppedProgress else animProgress
64+ val finalProgress = if (isStopped) stoppedProgress else animProgressState.value
5665
5766 LottieAnimation (
5867 composition = composition,
0 commit comments