Skip to content

Commit 02fee44

Browse files
committed
AnimatableSpacer.kt added.
1 parent c4be894 commit 02fee44

File tree

3 files changed

+117
-5
lines changed

3 files changed

+117
-5
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,78 @@
11
package com.commandiron.animatable_compose
22

3+
import androidx.compose.foundation.layout.Spacer
4+
import androidx.compose.foundation.layout.height
5+
import androidx.compose.foundation.layout.width
6+
import androidx.compose.runtime.Composable
7+
import androidx.compose.ui.Modifier
8+
import androidx.compose.ui.unit.Dp
9+
import androidx.compose.ui.unit.DpSize
10+
import com.commandiron.animatable_compose.state.AnimatableState
11+
import com.commandiron.animatable_compose.state.AnimatableStateTag
12+
import com.commandiron.animatable_compose.state.SharedAnimatableState
13+
14+
@Composable
15+
fun AnimatableSpacer(
16+
modifier: Modifier = Modifier,
17+
state: SharedAnimatableState,
18+
stateIndex: Int = 0,
19+
fixedWidth: Dp = Dp.Unspecified,
20+
fixedHeight: Dp = Dp.Unspecified,
21+
) {
22+
val stateIn = state.getState(AnimatableStateTag.SPACER, stateIndex) ?: throw (
23+
IllegalArgumentException("no animatableState has this index: $stateIndex")
24+
)
25+
Spacer(
26+
modifier = Modifier
27+
.width(
28+
when (fixedWidth) {
29+
Dp.Unspecified -> if (stateIn.animatedSize == DpSize.Unspecified) {
30+
Dp.Unspecified
31+
} else stateIn.animatedSize.width
32+
Dp.Infinity -> stateIn.screenWidth
33+
else -> fixedWidth
34+
}
35+
)
36+
.height(
37+
when (fixedHeight) {
38+
Dp.Unspecified -> if (stateIn.animatedSize == DpSize.Unspecified) {
39+
Dp.Unspecified
40+
} else stateIn.animatedSize.height
41+
Dp.Infinity -> stateIn.screenHeight
42+
else -> fixedHeight
43+
}
44+
)
45+
.then(modifier)
46+
)
47+
}
48+
49+
@Composable
50+
fun AnimatableSpacer(
51+
modifier: Modifier = Modifier,
52+
state: AnimatableState,
53+
fixedWidth: Dp = Dp.Unspecified,
54+
fixedHeight: Dp = Dp.Unspecified,
55+
) {
56+
Spacer(
57+
modifier = Modifier
58+
.width(
59+
when (fixedWidth) {
60+
Dp.Unspecified -> if (state.animatedSize == DpSize.Unspecified) {
61+
Dp.Unspecified
62+
} else state.animatedSize.width
63+
Dp.Infinity -> state.screenWidth
64+
else -> fixedWidth
65+
}
66+
)
67+
.height(
68+
when (fixedHeight) {
69+
Dp.Unspecified -> if (state.animatedSize == DpSize.Unspecified) {
70+
Dp.Unspecified
71+
} else state.animatedSize.height
72+
Dp.Infinity -> state.screenHeight
73+
else -> fixedHeight
74+
}
75+
)
76+
.then(modifier)
77+
)
78+
}

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,7 @@ enum class AnimationState {
907907
}
908908

909909
enum class AnimatableStateTag {
910-
BOX, TEXT, CARD, ICON, LAZY_ROW
910+
BOX, TEXT, CARD, ICON, LAZY_ROW, SPACER
911911
}
912912

913913
@Composable
@@ -1178,4 +1178,26 @@ fun rememberAnimatableLazyRowState(
11781178
onAnimation = onAnimation
11791179
)
11801180
}
1181+
}
1182+
1183+
@Composable
1184+
fun rememberAnimatableSpacerState(
1185+
index: Int = 0,
1186+
initialSize: DpSize? = null,
1187+
targetSize: DpSize? = null,
1188+
toTargetSizeAnimationSpec: AnimationSpec<Size>? = null,
1189+
toInitialSizeAnimationSpec: AnimationSpec<Size>? = null,
1190+
onSizeAnimation: (AnimationState) -> Unit = {},
1191+
): AnimatableState {
1192+
return remember {
1193+
AnimatableState(
1194+
animatableStateTag = AnimatableStateTag.SPACER,
1195+
index = index,
1196+
initialSize = initialSize,
1197+
targetSize = targetSize,
1198+
toTargetSizeAnimationSpec = toTargetSizeAnimationSpec,
1199+
toInitialSizeAnimationSpec = toInitialSizeAnimationSpec,
1200+
onSizeAnimation = onSizeAnimation
1201+
)
1202+
}
11811203
}

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@ import androidx.compose.ui.layout.ContentScale
1515
import androidx.compose.ui.text.font.FontWeight
1616
import androidx.compose.ui.unit.*
1717
import coil.compose.AsyncImage
18-
import com.commandiron.animatable_compose.AnimatableBox
19-
import com.commandiron.animatable_compose.AnimatableCard
20-
import com.commandiron.animatable_compose.AnimatableLazyRow
21-
import com.commandiron.animatable_compose.AnimatableText
18+
import com.commandiron.animatable_compose.*
2219
import com.commandiron.animatable_compose.state.*
2320
import dev.chrisbanes.snapper.ExperimentalSnapperApi
2421
import dev.chrisbanes.snapper.SnapOffsets
@@ -63,13 +60,18 @@ fun Show8InfoCard() {
6360
initialOffset = DpOffset(x = 0.dp, y = 300.dp),
6461
targetOffset = DpOffset(x = 0.dp, y = 0.dp)
6562
)
63+
val animatableSpacerState = rememberAnimatableSpacerState(
64+
initialSize = DpSize(width = 0.dp, height = 0.dp),
65+
targetSize = DpSize(width = 0.dp, height = 16.dp),
66+
)
6667

6768

6869
val infoCards by remember { mutableStateOf(InfoCard.infoCards) }
6970

7071
val cardStates = mutableListOf<AnimatableState>()
7172
val boxStates = mutableListOf<AnimatableState>()
7273
val textStates = mutableListOf<AnimatableState>()
74+
val spacerStates = mutableListOf<AnimatableState>()
7375

7476
infoCards.indices.forEach { index ->
7577
cardStates.add(
@@ -103,6 +105,13 @@ fun Show8InfoCard() {
103105
toTargetAnimationSpec = tween(250)
104106
)
105107
)
108+
spacerStates.add(
109+
animatableSpacerState.copy(
110+
index = index,
111+
toTargetAnimationSpec = tween(250),
112+
toInitialAnimationSpec = tween(250)
113+
)
114+
)
106115

107116
}
108117

@@ -111,6 +120,7 @@ fun Show8InfoCard() {
111120
+ cardStates
112121
+ boxStates
113122
+ textStates
123+
+ spacerStates
114124
)
115125

116126
Box(
@@ -155,6 +165,10 @@ fun Show8InfoCard() {
155165
fontSize = 22.sp,
156166
fontWeight = FontWeight.Bold
157167
)
168+
AnimatableSpacer(
169+
stateIndex = index,
170+
state = sharedAnimatableState
171+
)
158172
AnimatableText(
159173
text = infoCards[index].info,
160174
stateIndex = index,

0 commit comments

Comments
 (0)