Using `AnimatedVisibility` & trying to dynamic...
# compose
t
Using
AnimatedVisibility
& trying to dynamically update the transition values by calculating them based on mutable states; so, when a component enters/exits the enter/exit animation can be different every time. Just wanted to get some feedback on the approach 🧵
gist: https://gist.github.com/drinkthestars/015503abcf9e02098221c4c128ef3662 the key here being the creation of enter/exit transitions (as well as the one-off
Float
that is also being transitioned)
Copy code
AnimatedVisibility(
        modifier = modifier,
        visibleState = state.visibility,
        enter = state.inDirection.value.enterTransition(),
        exit = state.outDirection.value.exitTransition()
    )
Copy code
val animatedRotZ by transition.animateFloat { enterExitState ->
            when (enterExitState) {
                EnterExitState.PreEnter -> {
                    when (state.inDirection.value) {
                        Direction.In.Left -> -90f
                        Direction.In.Right -> 90f
                        Direction.In.Up -> 0f
                        Direction.In.Down -> 0f
                    }
                }
                EnterExitState.Visible -> 0f
                EnterExitState.PostExit -> {
                    when (state.outDirection.value) {
                        Direction.Out.Left -> -90f
                        Direction.Out.Right -> 90f
                        Direction.Out.Up -> 0f
                        Direction.Out.Down -> 0f
                    }
                }
            }
        }
Not sure if this is the best way of going about this or if there are alternatives...
d
This looks pretty nice. What are your concerns?
t
Thanks Doris! No functional concerns, this works great! Just wanted to make sure I’m using it correctly and there are no glaring performance holes
d
The only small performance improvement I'd suggest here is to add @stable to
fun Direction.In.enterTransition()
and the exit equivalent fun. That would allow the function call to be skipped by the compiler plugin when the input is the same. 🙂 See: https://github.com/androidx/androidx/blob/androidx-main/compose/docs/compose-api-guidelines.md#stable-types
t
Great idea, thank you for that! Marked the models/those functions as
@Stable
works great 🙏🏼
👏 1