How can I perform an action & then run an anim...
# compose
z
How can I perform an action & then run an animation, and run an animation & then perform an action in compose?
In the regular view days of Android I would just create an animation in place and attach start/end listeners to it.
Copy code
fun removeFromWindow() {
    manager.removeView(this)
}
e.g. Removing the view from the window should happen after the animation finishes. Similar but reverse story when adding the view. My code thus far has a mutableState<Boolean> which indicates whether the view should be attached, but there are no start/end listeners so I dont know where to go from there.
c
Compose uses suspending functions for animations. So you can call
animatable.animateTo(…)
then it will suspend until the animation is done. In the next line, you can set the state to remove the view.
z
Thank you! Would that be something like this?
Copy code
var wasVisible by remember {
    mutableStateOf(false)
}

LaunchedEffect(visible) {
    if (!wasVisible) {
        // addToWindow
    }

    val animatable = Animatable(
        initialValue = if (wasVisible) 1f else 0f
    )

    animatable.animateTo(
        targetValue = if (visible) 1f else 0f
    )

    if (!visible) {
        // removeFromWindow 
    }

    wasVisible = visible
}
c
You should move animatable out of the effect, and remember it. See here.
z
Ouch, I just realized a couple of things that will make this much harder to implement (probably). For one, none of the compose code runs until after addToWindow has been called - the composable never gets called before the view is rendered, obviously, but totally missed on my part.. And since Im running this inside a foreground service, removeFromWindow is called when the service is being destroyed, and theres no time to run the animation at all. The latter of which is the most important, you can clearly see it just disappearing. SInce this isnt related to compose Ill continue digging to find a workaround, thanks for the advice on Animatable, it helped!
👍 1