For one-shot events in Jetpack Compose (with MVIKo...
# mvikotlin
j
For one-shot events in Jetpack Compose (with MVIKotlin + Decompose) is the best way to fire two Intents/Labels? The first setting a flag in the Model, second undoing it? I can't figure out how to get Compose to recognize the same event twice otherwise but I see issues down the line with this workaround.
Ex:
Copy code
Intent.ShowThing -> State(showThing = true), Intent.SomeOtherAction -> State(showThing = true),
Intent.HideThing -> State(showThing = false)
a
What you mean by "recognize the same event twice"?
j
If I create a variable in the model for one time events :
Copy code
val viewActions: Value<ViewActions>
Where ViewActions is a sealed class
Copy code
sealed class ViewActions{
object ShowSnackbar: ViewActions()
}
the first time I set that value, the event will fire but the second time it does not. Because it is an object compose doesn't care it has been set twice? It detects the same value and doesn't recall the view function?
Searching for MVI related solutions generally seemed to be a mix of different workarounds
a
The
Value
thing is converted to Compose
State
for observations. I don't think it is a right way to deliver one time events. I think it has
equals
check somewhere under the hood. I would say that snack bar visibility should be either part of the state, or should be delivered via e.g. an
Observable
or a
Flow
. So you could subscribe for it in the UI layer. Or you can introduce your own
SnackBarController
and pass it to the
Component
.
👍 1
j
ah the
equals
makes a lot of sense there. I will dig into all three of those. Thanks for the point in the right direction
👍 1
Works like a charm.
a
So what approach did you choose?
j
A simple observable subscription now. I'm looking into the Reaktive sources more to understand what I'm doing here. I'm coming from Xamarin Forms for multiplatform so things are a bit different to say the least! I want to make sure I'm not leaking that subscription from the composable
a
You will be safe if you unsubsribe in onDispose {}. Also maybe worth to ask in #compose channel about any best practices.
j
I've been keeping my eye on that channel for recent updates as documentation on the web is all over the place, hah! I'll go back a bit and read up. As always, much appreciated on nudging in the right directions.
👍 1