https://kotlinlang.org logo
c

Chris Grigg

02/26/2021, 8:50 PM
hey guys, i’m migrating an
animatedFloat
to
remember { Animatable(initialValue = 0f) }
. Changing this involves replacing an
onEnd
callback and I’m using async/await for it, I was wondering if this is a good way of approaching it? Complete code snippets are below in the reply thread.
Previously, i was kicking off an animation at load using 
DisposableEffect
 but it’s necessary for me to switch it to 
LaunchedEffect
 and, more importantly, I was using an 
onEnd
 callback in 
animateTo
. It looked like this:
Copy code
DisposableEffect(powerValue) {
        animatedProgress.snapTo(0f)
        animatedProgress.animateTo(1f, tween(durationMillis = 200), onEnd = { _: AnimationEndReason, _: Float ->
            setPowerValueWas(powerValue)
            animatedProgress.snapTo(0f)
        })

        onDispose {}
    }
Since 
onEnd
 was removed, would an appropriate way of handling this be the use of async/await to examine 
endReason
?
Copy code
LaunchedEffect(powerValue) {
        animatedProgress.snapTo(0f)
        val result = async { animatedProgress.animateTo(1f, tween(durationMillis = 200)) }.await()
        when (result.endReason) {
            AnimationEndReason.Finished -> {
                setPowerValueWas(powerValue)
                animatedProgress.snapTo(0f)
            }
        }
    }
d

Dominaezzz

02/26/2021, 8:53 PM
You don't need async/await . Just call the method directly.
1
c

Chris Grigg

02/26/2021, 8:55 PM
oh jeez, i’m fading. of course, it wouldn’t run asynchronously unless i launched a new coroutine, right?
d

Dominaezzz

02/26/2021, 8:55 PM
Indeed.
c

Chris Grigg

02/26/2021, 8:55 PM
of course of course
thank you
d

Doris Liu

02/26/2021, 10:33 PM
And yes... you could just call it directly, and examine the returned result. If you don't have bounds set on the
Animatable
, you don't even need to check the end reason - the
animateTo
only returns when it finishes (or reaches bounds). In the case of interruption, there'll be a
CancellationException
thrown
c

Chris Grigg

02/26/2021, 10:33 PM
that’s a great tip, thank you
👍 1