Skip to content

Commit 4f60d56

Browse files
committed
[PC-1413] StopAtProgressLottie 정지 로직 최적화 (derivedStateOf, snapshotFlow)
1 parent 95a423f commit 4f60d56

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

feature/onboarding/src/main/java/com/puzzle/onboarding/ui/components/StopAtProgressLottie.kt

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@ package com.puzzle.onboarding.ui.components
33
import androidx.annotation.RawRes
44
import androidx.compose.runtime.Composable
55
import androidx.compose.runtime.LaunchedEffect
6+
import androidx.compose.runtime.State
7+
import androidx.compose.runtime.derivedStateOf
68
import androidx.compose.runtime.getValue
79
import androidx.compose.runtime.mutableStateOf
810
import androidx.compose.runtime.remember
911
import androidx.compose.runtime.setValue
12+
import androidx.compose.runtime.snapshotFlow
1013
import androidx.compose.ui.Modifier
1114
import androidx.compose.ui.layout.ContentScale
1215
import 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

Comments
 (0)