Chris Fillmore
09/29/2022, 4:15 PMAnimatedContent
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?Chris Fillmore
09/29/2022, 4:15 PMenum 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) {
...
}
}
Chris Fillmore
09/29/2022, 4:16 PMEnum.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?Chris Fillmore
09/29/2022, 4:17 PMChuck Stein
09/29/2022, 4:26 PMI 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 lineChris Fillmore
09/29/2022, 6:44 PM