Is it possible to snap an animation, `Animatable` ...
# compose
d
Is it possible to snap an animation,
Animatable
or
updateTransition
, during the same composition? When I update the state or call
snapTo
, the update happens in the next frame or so.
d
Could you share a bit more about your use case?
d
Basically, I'm implementing
LazyColumn
drag and drop, and I want the items in the list to animate into their new positions. To do this, when an item moves to the next/previous position, I use
graphicsLayer(translationY = ....)
to draw the item in the previous position, then I animate the item into it's new position. When I detect the change in position, I want to snap the translation to the old position immediately. Right now, the item would be rendered in the new position first, then snap to the old position, then animate back to the new position.
d
The only way to achieve that is create a new
Animatable
when you need to snap (in the current composition pass):
var translation by remember { mutableStateOf(Animatable(...)) }
and mutate that translation as needed. The
LaunchedEffect
that triggers animations for
Animatable
would now need to have
translation
added to the keys.
d
Ah thanks! Is that the "correct" way or a workaround?
Also, is the
mutableStateOf
necessary? Could I just do
val translation by remember(changed) { Animatable(...) }
?
d
That'd be a workaround. But it's unclear to me what a correct and non-confusing API for that would be at the moment. We are trying to avoid one-off APIs. If a similar usage pattern emerges in the future, we would be able to create an API based on the pattern.
Also, is the 
mutableStateOf
 necessary? Could I just do 
val translation by remember(changed) { Animatable(...) }
Yes, you could do that.
mutableStateOf
would be necessary if you need to mutate translation in a coroutine. 🙂
d
Ahhh, got it.
My use case is arguably a workaround for
LazyColumn
not having drag and drop (or the necessary primitives I guess), so it may not be worth considering just yet. Depends on what the
LazyColumn
devs think I guess.