I have a screen with `AnimatedContent` that uses a...
# compose
c
I have a screen with
AnimatedContent
that uses an enum for state. I’d like to switch to a sealed interface, to pass arguments, but I would lose the use of
Enum.ordinal
. Any recommendations?
Code is like this
Copy code
enum class Panel {
  Main,
  CustomizeItems,
  ItemDetail,
}

...

var currentPanel by remember { mutableStateOf(Panel.Main) }

AnimatedContent(
  targetState = currentPanel,
  transitionSpec = {
    if (targetState.ordinal > initialState.ordinal) {
      slideInHorizontally { width -> width } with
        slideOutHorizontally { width -> -width }
    } else {
      slideInHorizontally { width -> -width } with
        slideOutHorizontally { width -> width }
    }
  },
) { targetPanel ->
  when (targetPanel) {
    ...
  }
}
It all works well. But as you can see, I use
Enum.ordinal
to define my
transitionSpec
. Have you been in this situation, where you depended on
Enum.ordinal
? What approach did you take? Did you switch to a sealed interface? Any recommendations?
I could manually define ordinal in a sealed interface, but this seems brittle
c
I could manually define ordinal in a sealed interface, but this seems brittle
I would argue any use of
Enum.ordinal
would be more brittle, because if the order of the enum declarations ever were to change then the whole logic breaks. For that reason even when I'm using an enum instead of sealed interface, I manually give the enum an
index
property to ensure there will be no unintended behavior changes down the line
c
That’s interesting, I actually hadn’t considered that. Thanks for responding