@@ -9,6 +9,7 @@ import androidx.compose.foundation.layout.fillMaxWidth
9
9
import androidx.compose.runtime.Composable
10
10
import androidx.compose.runtime.LaunchedEffect
11
11
import androidx.compose.runtime.getValue
12
+ import androidx.compose.runtime.mutableStateOf
12
13
import androidx.compose.runtime.remember
13
14
import androidx.compose.ui.Modifier
14
15
import androidx.compose.ui.graphics.graphicsLayer
@@ -26,12 +27,17 @@ fun LottieAnimation(
26
27
contentScale : ContentScale = ContentScale .FillWidth ,
27
28
scaleXAdjustment : Float = 1f,
28
29
scaleYAdjustment : Float = 1f,
30
+ play : Boolean = iterations == 1,
31
+ restartOnPlay : Boolean = false,
29
32
onAnimationEnd : (() -> Unit )? = null,
30
33
) {
31
34
val composition by rememberLottieComposition(LottieCompositionSpec .RawRes (resId))
32
- val progress by animateLottieCompositionAsState(
35
+ val isPlaying = remember { mutableStateOf(iterations == LottieConstants .IterateForever || play) }
36
+ val animationState = animateLottieCompositionAsState(
33
37
composition = composition,
34
38
iterations = iterations,
39
+ isPlaying = isPlaying.value,
40
+ restartOnPlay = restartOnPlay,
35
41
)
36
42
val alpha = remember { Animatable (0f ) }
37
43
@@ -44,9 +50,18 @@ fun LottieAnimation(
44
50
}
45
51
}
46
52
47
- LaunchedEffect (progress) {
48
- if (progress == 1f ) {
53
+ LaunchedEffect (play) {
54
+ if (play) {
55
+ isPlaying.value = true
56
+ }
57
+ }
58
+
59
+ LaunchedEffect (animationState.progress) {
60
+ if (animationState.progress == 1f ) {
49
61
onAnimationEnd?.invoke()
62
+ if (iterations == 1 ) {
63
+ isPlaying.value = false
64
+ }
50
65
}
51
66
}
52
67
@@ -62,7 +77,7 @@ fun LottieAnimation(
62
77
if (composition != null ) {
63
78
com.airbnb.lottie.compose.LottieAnimation (
64
79
composition = composition,
65
- progress = { progress },
80
+ progress = { animationState. progress },
66
81
modifier = Modifier .fillMaxSize(),
67
82
)
68
83
}
0 commit comments