Stylianos Gakis
03/22/2023, 6:06 PMColumnScope.AnimatedVisibility
gives this annoying problem of having to do double check for the state, since the content lambda does not give the current state. Like:
AnimatedVisibility(model != null) {
if (model != null) {
ActualComposable(model)
Spacer(Modifier.height(20.dp))
}
}
Since I am not sure if doing !!
in there is safe, as the old content may still be animating out, and it will call !!
on the null item to try and render the item as it’s leaving.
What do you folks usually do for this? Do I just have to do this double null check or is there something I can do instead, some API I may be missing?AnimatedContent(
targetState = model,
label = "AnimatedContent",
transitionSpec = {
ContentTransform(
targetContentEnter = fadeIn() + expandVertically(),
initialContentExit = fadeOut() + shrinkVertically(),
)
},
) { model ->
if (model != null) {
ActualComposable()
}
}
to use AnimatedContent which does in fact give me the current item inside the lamvda, but then I need to specify the ContentTransform myself and so on, which I do like that AnimatedVisibility on a Column puts a sane default there.ephemient
03/22/2023, 6:30 PMStylianos Gakis
03/22/2023, 6:42 PMAlex Vanyo
03/22/2023, 7:02 PMAnimatedVisibility
is getting called, even if model
is null
.
This is a case where the strict data flow of Compose makes the required code a bit more explicit for what’s actually happening.
Let’s say that the animation for AnimatedVisibility
takes 10 minutes to animate out (the world’s slowest remove animation)
Even if you clear the model, something somewhere still needs to hold onto it to continue to render the UI for it for the next 10 minutes. The user could be doing other actions, other things could be changing, and the content inside the AnimatedVisibility
is still being composed and updated.Stylianos Gakis
03/22/2023, 7:07 PMAlex Vanyo
03/22/2023, 7:11 PMAnimatedVisibility
has a sample for managing a list where items can be removed with an animation out which might be interesting to take a look at:
https://developer.android.com/reference/kotlin/androidx/compose/animation/package-summary#AnimatedVisibility(androidx[…]lin.String,kotlin.Function1)
The core idea is that an item is only completely removed from everywhere once it has finished animated out.In theory, what I want in there is for the last known state to be saved as a screenshotWhat you could maybe sort of do is “cache” with
remember
the item to still render with, and keep that around even if the item is deleted higher up.
Or have two lists: the “logical” list, which are the items that will be in the list if all animations resolve, and the “display” list, which might have additional items that are in the process of animating out.Stylianos Gakis
03/22/2023, 7:21 PMActualComposable(model)
, and I handle the null case in there.
In my case I can basically show most of what I was showing already (it’s a card) with the same height and all, so the animation won’t be jumpy.
This model was needed for some of the text only in this case (“Foo” in the video), so the bad thing is just that that text shows nothing instantaneously when it starts leaving instead of also animating out, but I get this animation without adding any extra logic just for this animation like remebering stuff and so on. So that’s much better than what I first had anyway. Thanks so much for helping me out!expandVertically
and shrinkVertically
can take in expandFrom = Alignment.CenterVertically
and shrinkTowards = Alignment.CenterVertically
.
Every time I do anything with the animation APIs it’s always so pleasant to work with to do these little touches that take like no effort, it’s insane.efemoney
03/23/2023, 12:58 PMif I look back at this in 1 month from now I’ll start wondering what the hell I was doingThats what comments are for 😅
Stylianos Gakis
03/23/2023, 5:07 PMThats what comments are forThe comments will start lying before you know it. They’re sneaky like that 😅