So I have this code: ```val emojiState = remember ...
# compose
s
So I have this code:
Copy code
val emojiState = remember { mutableStateOf(EmojiState.HIDDEN) }
val definition = transitionDefinition<EmojiState> {
    state(EmojiState.HIDDEN) {
        this[emojiSize] = 0.dp
    }

    state(EmojiState.START) {
        this[emojiSize] = 20.dp
    }

    state(EmojiState.END) {
        this[emojiSize] = 50.dp
    }

    transition(fromState = EmojiState.START, toState = EmojiState.END) {
        interruptionHandling = InterruptionHandling.SNAP_TO_END
        emojiSize using tween(3000, easing = FastOutSlowInEasing)
    }
}

val transitionState = transition(
    definition = createEmojiTransition(),
    initState = emojiState.value,
    toState = if (emojiState.value == EmojiState.START) EmojiState.END else EmojiState.HIDDEN,
    onStateChangeFinished = {
        emojiState.value = it
    }
)
When I change
emojiState
to
HIDDEN
while the transition from
START
to
END
is going on, it looks like the transition isn't interrupted and still going on in the background. For example, I change the transition to
HIDDEN
1000 millis after starting the
START
to
END
transition, then after 2000 millis the
onStateChangeFinished
is run and it sets my state back to
END
. Is there some way I can interrupt the transition and prevent
onStateChangeFinished
to be called for interrupted transitions. Or maybe my logic is at fault here?
Also, I wanna learn some advanced animations so if there is an animation-heavy sample, I'd like to take a look
a
I think Wajahat Karim's https://github.com/wajahatkarim3/droidcon2020/ is pretty neat.
s
Yeah I've gone through it but I'm expecting some more advanced stuff like his dino game
b
Not sure if advanced enough, but I have a couple samples here: https://github.com/Bruno125/Compose-Challenges
👍 1
s
Thanks!
d
When
initState
gets changed (or when
TransitionDefinition
is re-created), the animation starts over. That's probably why the animation isn't interrupted. Both seem to be happening here. Unless you need the transition to start an animation as soon as it gets composed for the first time, I'd recommend leaving it undefined. You might also want to remember the
TransitionDefinition
if it's not created at the file level. 🙂
s
I actually got it working and made the definition really simple by removing the
HIDDEN
state. But I didn't get what you mean by leaving it undefined. I was starting from
HIDDEN
so the transition won't go off when it first composes. Also, thanks for the remember tip I wasn't doing that 🙂
d
By "leaving it undefined" I meant you can just leave initState to its default value. The default for initState is the same as the
toState
when it is first composed. Therefore the default behavior when you don't define
initState
is that the transition doesn't go off when it's first composed. 🙂
s
Okay got it that's what I was doing I guess. Thanks!