lazt omen
04/29/2022, 6:37 AMCasey Brooks
04/29/2022, 2:53 PMlazt omen
04/29/2022, 2:59 PMCasey Brooks
04/29/2022, 3:05 PMArkadii Ivanov
04/29/2022, 9:02 PMlazt omen
04/30/2022, 3:35 PMobject RouterContract {
data class State(
val currentPage: PageRoute
)
sealed class Inputs {
data class Navigate(val newRoute: PageRoute) : Inputs()
}
sealed class Events {
}
}
class RouterInputHandler : InputHandler<RouterContract.Inputs, RouterContract.Events, RouterContract.State> {
override suspend fun InputHandlerScope<RouterContract.Inputs, RouterContract.Events, RouterContract.State>.handleInput(
input: RouterContract.Inputs
) = when (input) {
is RouterContract.Inputs.Navigate -> {
updateState {
it.copy(
currentPage = input.newRoute
)
}
}
}
I have no idea what I'm supposed to do next. I want to be able to access this instance in all my component tree so I can change the current route.Casey Brooks
04/30/2022, 4:04 PMBasicViewModel
as the base class, and there’s an eventHandler { }
helper function for stubbing
out the required EventHandler
parameter of it.
class RouterViewModel(
viewModelCoroutineScope: CoroutineScope
) : BasicViewModel<
RouterContract.Inputs,
RouterContract.Events,
RouterContract.State>(
config = BallastViewModelConfiguration.Builder()
.forViewModel(
initialState = RouterContract.State(),
inputHandler = RouterInputHandler(),
name = "Router",
),
eventHandler = eventHandler { },
coroutineScope = viewModelCoroutineScope,
)
From here, you need to create an instance of the ViewModel and observe it in the UI, as shown in the Ballast Compose Desktop examplelazt omen
04/30/2022, 4:54 PMCasey Brooks
04/30/2022, 8:01 PMpostInput
callback through your composition tree, and only at the root do the Composable functions actually get tied to the ViewModel. They’re just normal callbacks at that point. The source for the ScoreKeeper example should help you understand how it works. If you passed the whole ViewModel through CompositionLocals, the idea is the same that the ViewModel lives at the root composable for the screen, and it’s just accessed implicitly rather than explicitly.
For the actual navigation, you’d create Inputs for each navigation destination, and then those Inputs fire off the Events that actually perform the navigation. Give me a minute to throw together a quick example for youlazt omen
04/30/2022, 8:37 PMCasey Brooks
04/30/2022, 8:41 PMState
object around, and the callback is just for the generic Inputs superclass, even it only 1 input type if sent from a deeper Composable function.
And for a postEvent
callback, no, you don’t need that. You can’t post an Event directly to the ViewModel from “outside”, it can only be sent as part of handling an input.lazt omen
04/30/2022, 9:25 PMCasey Brooks
05/01/2022, 1:40 AM