Mark Murphy
09/25/2024, 1:20 PMwithRouter()
and BasicRouter
and instead building some sort of composite viewmodel? Or, is the vision that the I/O-handling viewmodel hold a reference to the router and tell it to navigate as needed? Or is there something else?
Thanks!sigmadelta
09/26/2024, 2:37 PMEventHandler
holds a reference to a Router (f.e. I inject it using Koin). Here is the description on the Ballast website for events;
Events are modeled similar to Inputs, but in the other direction; Events are sent from the ViewModel to be processed exactly once by the UI. This would typically be things like requests to navigate to another screen.
So in your example it would work like this in my head.
Button gets pushed ->
.onClick
triggers a postInput
->
Input
gets processed by InputHandler ->
InputHandler triggers suspended I/O work in repo/service/etc... ->
return value of I/O work gets interpreted and triggers corresponding postEvent
->
Event
gets processed in EventHandler and f.e. calls router.trySend(RouterContract.Inputs.GoToDestination(...))
Casey Brooks
09/27/2024, 1:51 PMMark Murphy
09/29/2024, 7:07 PMclass MyScreenViewModel(
coroutineScope: CoroutineScope,
reducer: MyScreenContract.Reducer,
val myScreenRouter: MyScreenRouter = MyScreenRouter(coroutineScope)
) :
BasicViewModel<MyScreenContract.Input, MyScreenContract.Event, MyScreenContract.State>(
config = BallastViewModelConfiguration.Builder()
.withLogging()
.apply { inputStrategy = FifoInputStrategy() }
.withViewModel(
initialState = MyScreenContract.State(),
inputHandler = reducer,
name = "MyScreenViewModel"
)
.build(),
eventHandler = MyScreenContract.EventProcessor(myScreenRouter),
coroutineScope = coroutineScope
)
My screen composable can access the router via the viewmodel property, and I can supply it to my EventProcessor
as well. This allows the whole thing to be torn down when leaving the screen.
Thank you both for the suggestions!Casey Brooks
10/01/2024, 3:53 PMCasey Brooks
10/01/2024, 4:25 PM