animate*AsState has a repeat function, but when yo...
# compose
g
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
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
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
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
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
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
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
why not just use a repeatable animation spec?
Copy code
animateTo(360f, repeatable(iterations = 3, tween(1000, …)))
👀 1
g
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
Wow that exists 🤦‍♂️ I only knew of the infinite repeatable. Sorry about misleading you there ☠️
g
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!