Skip to content

Instantly share code, notes, and snippets.

@zsoltk
Last active September 10, 2022 22:36
Show Gist options
  • Save zsoltk/ed86882c60f8cb0d6a79c4b0dc2b8bfa to your computer and use it in GitHub Desktop.
Save zsoltk/ed86882c60f8cb0d6a79c4b0dc2b8bfa to your computer and use it in GitHub Desktop.
class CardsTransitionHandler<T>(
private val transitionSpec: TransitionSpec<Cards.State, Float> = {
spring(stiffness = Spring.StiffnessVeryLow)
}
) : ModifierTransitionHandler<T, Cards.State>() {
private fun Cards.State.toProps() =
when (this) {
is Queued -> queued
is Bottom -> bottom
is Top -> top
is VoteLike -> voteLike
is VotePass -> votePass
}
override fun createModifier(
modifier: Modifier,
transition: Transition<Cards.State>,
descriptor: TransitionDescriptor<T, Cards.State>
): Modifier = modifier.composed {
val rotation = transition.animateFloat(
transitionSpec = transitionSpec,
targetValueByState = { it.toProps().rotation })
val scale = transition.animateFloat(
transitionSpec = transitionSpec,
targetValueByState = { it.toProps().scale })
val dpOffsetX = transition.animateFloat(
transitionSpec = transitionSpec,
targetValueByState = { it.toProps().offsetX.value })
val zIndex = transition.animateFloat(
transitionSpec = transitionSpec,
targetValueByState = { it.toProps().zIndex })
return@composed this
.offset {
IntOffset(x = (this.density * (dpOffsetX.value)).roundToInt(), y = 0)
}
.zIndex(zIndex.value)
.graphicsLayer(
rotationZ = rotation.value
)
.scale(scale.value)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment