Skip to content

Commit 99896eb

Browse files
committed
- sharedAnimationSpec added.
1 parent 53146d9 commit 99896eb

File tree

2 files changed

+45
-14
lines changed

2 files changed

+45
-14
lines changed

animatable-compose/src/main/java/com/commandiron/animatable_compose/state/AnimatableState.kt

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,61 @@ import kotlin.math.absoluteValue
1616

1717
@Composable
1818
fun rememberSharedAnimatableState(
19-
animatableStates: List<AnimatableState>
19+
animatableStates: List<AnimatableState>,
20+
toTargetAnimationSpec: AnimationSpec<Float>? = null,
21+
toInitialAnimationSpec: AnimationSpec<Float>? = null,
2022
): SharedAnimatableState {
2123
return remember {
2224
SharedAnimatableState(
23-
animatableStates = animatableStates
25+
animatableStates = animatableStates,
26+
toTargetAnimationSpec = toTargetAnimationSpec,
27+
toInitialAnimationSpec = toInitialAnimationSpec
2428
)
2529
}
2630
}
2731

2832
data class SharedAnimatableState(
29-
private val animatableStates: List<AnimatableState>
33+
private var animatableStates: List<AnimatableState>,
34+
private val toTargetAnimationSpec: AnimationSpec<Float>? = null,
35+
private val toInitialAnimationSpec: AnimationSpec<Float>? = null,
3036
) {
37+
private val states by mutableStateOf(
38+
if(toTargetAnimationSpec != null && toInitialAnimationSpec != null) {
39+
animatableStates.map {
40+
it.copy(
41+
toTargetAnimationSpec = toTargetAnimationSpec,
42+
toInitialAnimationSpec = toInitialAnimationSpec
43+
)
44+
}
45+
} else {
46+
if(toTargetAnimationSpec != null && toInitialAnimationSpec == null) {
47+
animatableStates.map {
48+
it.copy(
49+
toTargetAnimationSpec = toTargetAnimationSpec
50+
)
51+
}
52+
} else if(toTargetAnimationSpec == null && toInitialAnimationSpec != null) {
53+
animatableStates.map {
54+
it.copy(
55+
toTargetAnimationSpec = toTargetAnimationSpec
56+
)
57+
}
58+
} else {
59+
animatableStates
60+
}
61+
}
62+
)
63+
3164
fun getState(
3265
animatableStateTag: AnimatableStateTag,
3366
index: Int
3467
): AnimatableState? {
35-
val states = animatableStates.filter { it.animatableStateTag == animatableStateTag }
36-
return states.getOrNull(index)
68+
val filteredStates = states.filter { it.animatableStateTag == animatableStateTag }
69+
return filteredStates.getOrNull(index)
3770
}
3871

3972
fun animate() {
40-
animatableStates.forEach { it.animate() }
73+
states.forEach { it.animate() }
4174
}
4275

4376
fun animateToTarget() {

app/src/main/java/com/commandiron/animatablecompose/Show4AnimatableCardWithText.kt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.commandiron.animatablecompose
22

3-
import androidx.compose.animation.core.Spring
4-
import androidx.compose.animation.core.spring
3+
import androidx.compose.animation.core.*
54
import androidx.compose.foundation.clickable
65
import androidx.compose.foundation.layout.Box
76
import androidx.compose.foundation.layout.fillMaxSize
@@ -26,19 +25,18 @@ fun Show4AnimatableCardWithText() {
2625
initialSize = DpSize(width = 50.dp, height = 25.dp),
2726
targetSize = DpSize(width = 300.dp, height = 150.dp),
2827
initialShape = CircleShape,
29-
targetShape = RoundedCornerShape(16.dp),
30-
toTargetAnimationSpec = spring(Spring.DampingRatioHighBouncy, Spring.StiffnessVeryLow)
28+
targetShape = RoundedCornerShape(16.dp)
3129
)
3230
val animatableTextState = rememberAnimatableTextState(
3331
initialFontSize = 4.sp,
34-
targetFontSize = 36.sp,
35-
toTargetAnimationSpec = spring(Spring.DampingRatioHighBouncy, Spring.StiffnessVeryLow)
32+
targetFontSize = 36.sp
3633
)
3734
val sharedAnimatableState = rememberSharedAnimatableState(
38-
listOf(
35+
animatableStates = listOf(
3936
animatableCardState,
4037
animatableTextState
41-
)
38+
),
39+
toTargetAnimationSpec = infiniteRepeatable(tween(1000), RepeatMode.Reverse)
4240
)
4341
Box(
4442
modifier = Modifier

0 commit comments

Comments
 (0)