https://kotlinlang.org logo
#compose
Title
# compose
g

Garret Yoder

03/21/2024, 3:36 PM
animate*AsState has a repeat function, but when you don't want a state to control it is there a good way to have a manually fired animation repeat n number of times? Eg;
Copy code
val rotation = remember { Animatable(0f) }
repeat(3) {
    rotation.animateTo(360f, tween(1000))
    rotation.snapTo(0f)
}
works, it just seems like there'd be a shorthand for it that I feel like I'm glossing over in the animation docs
s

Stylianos Gakis

03/21/2024, 3:40 PM
Repeating a certain amount of times looks like a great job for exactly what you are showing here. And I assume you have the repeat function inside a LaunchedEffect right?
g

Garret Yoder

03/21/2024, 3:42 PM
Yup I just dumped the code in one place for an example. I guess I just expected the repeat function from animate*AsState to carry over into Animatable too.
s

Stylianos Gakis

03/21/2024, 3:45 PM
Copy code
val rotation = remember { Animatable(0f) }
LaunchedEffect(Unit) {
  repeat(3) {
    rotation.animateTo(360f, tween(1000))
    rotation.snapTo(0f)
  }
}
So this just works for you correctly, right?
g

Garret Yoder

03/21/2024, 3:57 PM
This is sitting being fired when a button gets clicked, so not LaunchedEffect it's just in a normal coroutine.launch.
Copy code
val rotation = remember { Animatable(0f) }
OutlinedButton(
    onClick = {
        scope.launch {
            repeat(3) {
                rotation.animateTo(360f, tween(1000, easing = LinearEasing))
                rotation.snapTo(0f)
            }
...
s

Stylianos Gakis

03/21/2024, 4:06 PM
Just be careful with this, if you click it again it will just have those two jobs working at the same time and overriding each other. Do you want that?
g

Garret Yoder

03/21/2024, 4:09 PM
Yeah I'm aware of that case, the button is already state controlled and can't be clicked more than once while a task is going on. Theres a task happening in the background that's difficult to monitor for progress so I just slapped 3 seconds on it as a timeout to give it time, as it shouldn't ever take >1s, so 3 was the safety range. If there's not a built in repeat that I'm just missing then I think I'm good, thats working. I'll probably eventually do the work to track when the task is done, it's just not on my radar as a priority yet.
z

Zach Klippenstein (he/him) [MOD]

03/21/2024, 6:32 PM
why not just use a repeatable animation spec?
Copy code
animateTo(360f, repeatable(iterations = 3, tween(1000, …)))
👀 1
g

Garret Yoder

03/21/2024, 6:36 PM
Yup thats exactly what I was looking for, I figured there'd just be a way to repeat it from the animation builder and I was missing it in the docs. I think I missed that repeatable was also an AnimationSpec
s

Stylianos Gakis

03/21/2024, 6:38 PM
Wow that exists 🤦‍♂️ I only knew of the infinite repeatable. Sorry about misleading you there ☠️
g

Garret Yoder

03/21/2024, 6:40 PM
No worries, I didn't know it existed outside of animate*AsState either, I had just had a feeling that it would exist if it existed elsewhere which is why I asked. Thanks!
34 Views