Skip to content

Commit 5989683

Browse files
Animate Shared episode date using sharedBounds
1 parent ade30e2 commit 5989683

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

app/src/main/kotlin/com/mr3y/podcaster/ui/screens/EpisodeDetailsScreen.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.mr3y.podcaster.ui.screens
22

3+
import androidx.compose.animation.SharedTransitionScope
34
import androidx.compose.foundation.background
45
import androidx.compose.foundation.layout.Arrangement
56
import androidx.compose.foundation.layout.Box
@@ -61,6 +62,7 @@ import com.mr3y.podcaster.ui.components.RemoveFromQueueButton
6162
import com.mr3y.podcaster.ui.components.TopBar
6263
import com.mr3y.podcaster.ui.components.rememberHtmlToAnnotatedString
6364
import com.mr3y.podcaster.ui.components.rememberSharedContentState
65+
import com.mr3y.podcaster.ui.components.sharedBounds
6466
import com.mr3y.podcaster.ui.components.sharedElement
6567
import com.mr3y.podcaster.ui.presenter.PodcasterAppState
6668
import com.mr3y.podcaster.ui.presenter.RefreshResult
@@ -280,10 +282,11 @@ private fun EpisodeDetails(
280282
text = formattedEpisodeDate,
281283
color = MaterialTheme.colorScheme.onSurfaceVariant,
282284
style = MaterialTheme.typography.titleMedium,
283-
modifier = Modifier.sharedElement(
285+
modifier = Modifier.sharedBounds(
284286
LocalSharedTransitionScope.current,
285287
LocalAnimatedVisibilityScope.current,
286288
rememberSharedContentState(key = episode.dateSharedTransitionKey),
289+
resizeMode = SharedTransitionScope.ResizeMode.ScaleToBounds(),
287290
),
288291
)
289292
Text(

app/src/main/kotlin/com/mr3y/podcaster/ui/screens/SubscriptionsScreen.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.mr3y.podcaster.ui.screens
22

3+
import androidx.compose.animation.SharedTransitionScope
34
import androidx.compose.foundation.background
45
import androidx.compose.foundation.clickable
56
import androidx.compose.foundation.layout.Arrangement
@@ -80,6 +81,7 @@ import com.mr3y.podcaster.ui.components.RemoveFromQueueButton
8081
import com.mr3y.podcaster.ui.components.TopBar
8182
import com.mr3y.podcaster.ui.components.rememberHtmlToAnnotatedString
8283
import com.mr3y.podcaster.ui.components.rememberSharedContentState
84+
import com.mr3y.podcaster.ui.components.sharedBounds
8385
import com.mr3y.podcaster.ui.components.sharedElement
8486
import com.mr3y.podcaster.ui.presenter.PodcasterAppState
8587
import com.mr3y.podcaster.ui.presenter.RefreshResult
@@ -386,10 +388,11 @@ private fun ColumnScope.EpisodesList(
386388
textAlign = TextAlign.Center,
387389
maxLines = 2,
388390
overflow = TextOverflow.Ellipsis,
389-
modifier = Modifier.sharedElement(
391+
modifier = Modifier.sharedBounds(
390392
LocalSharedTransitionScope.current,
391393
LocalAnimatedVisibilityScope.current,
392394
rememberSharedContentState(key = episode.dateSharedTransitionKey),
395+
resizeMode = SharedTransitionScope.ResizeMode.ScaleToBounds(),
393396
),
394397
)
395398
}

ui/design-system/src/main/kotlin/com/mr3y/podcaster/ui/components/SharedElementTransition.kt

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,27 @@ package com.mr3y.podcaster.ui.components
22

33
import androidx.compose.animation.AnimatedVisibilityScope
44
import androidx.compose.animation.BoundsTransform
5+
import androidx.compose.animation.EnterTransition
6+
import androidx.compose.animation.ExitTransition
57
import androidx.compose.animation.ExperimentalSharedTransitionApi
68
import androidx.compose.animation.SharedTransitionScope
79
import androidx.compose.animation.SharedTransitionScope.OverlayClip
810
import androidx.compose.animation.SharedTransitionScope.PlaceHolderSize
911
import androidx.compose.animation.SharedTransitionScope.PlaceHolderSize.Companion.contentSize
12+
import androidx.compose.animation.SharedTransitionScope.ResizeMode
13+
import androidx.compose.animation.SharedTransitionScope.ResizeMode.Companion.ScaleToBounds
1014
import androidx.compose.animation.core.Spring.StiffnessMediumLow
1115
import androidx.compose.animation.core.VisibilityThreshold
1216
import androidx.compose.animation.core.spring
17+
import androidx.compose.animation.fadeIn
18+
import androidx.compose.animation.fadeOut
1319
import androidx.compose.runtime.Composable
1420
import androidx.compose.runtime.staticCompositionLocalOf
21+
import androidx.compose.ui.Alignment.Companion.Center
1522
import androidx.compose.ui.Modifier
1623
import androidx.compose.ui.geometry.Rect
1724
import androidx.compose.ui.graphics.Path
25+
import androidx.compose.ui.layout.ContentScale
1826
import androidx.compose.ui.unit.Density
1927
import androidx.compose.ui.unit.LayoutDirection
2028

@@ -59,6 +67,44 @@ fun Modifier.sharedElement(
5967
}
6068
}
6169

70+
fun Modifier.sharedBounds(
71+
sharedTransitionScope: SharedTransitionScope?,
72+
animatedVisibilityScope: AnimatedVisibilityScope?,
73+
state: SharedTransitionScope.SharedContentState?,
74+
enter: EnterTransition = fadeIn(),
75+
exit: ExitTransition = fadeOut(),
76+
boundsTransform: BoundsTransform = BoundsTransform { _, _ ->
77+
spring(
78+
stiffness = StiffnessMediumLow,
79+
visibilityThreshold = Rect.VisibilityThreshold,
80+
)
81+
},
82+
resizeMode: ResizeMode = ScaleToBounds(ContentScale.FillWidth, Center),
83+
placeHolderSize: PlaceHolderSize = contentSize,
84+
renderInOverlayDuringTransition: Boolean = true,
85+
zIndexInOverlay: Float = 0f,
86+
clipInOverlayDuringTransition: OverlayClip = ParentClip
87+
): Modifier {
88+
return if (sharedTransitionScope == null || animatedVisibilityScope == null || state == null) {
89+
this
90+
} else {
91+
with(sharedTransitionScope) {
92+
sharedBounds(
93+
state,
94+
animatedVisibilityScope,
95+
enter,
96+
exit,
97+
boundsTransform,
98+
resizeMode,
99+
placeHolderSize,
100+
renderInOverlayDuringTransition,
101+
zIndexInOverlay,
102+
clipInOverlayDuringTransition
103+
)
104+
}
105+
}
106+
}
107+
62108
fun Modifier.skipToLookaheadSize(
63109
sharedTransitionScope: SharedTransitionScope?,
64110
): Modifier {

0 commit comments

Comments
 (0)