voyager
voyager copied to clipboard
Navigation from ScreeModel
In my current scenario, I'm seeking a solution to navigate from a ScreenModel to another screen. Regrettably, I haven't discovered a suitable method to accomplish this yet. Could you please provide guidance or suggestions on how to enable this navigation? Any assistance would be greatly appreciated. Thank you.
you can achieve it with callbacks easily (kotlin callbacks)
assume below function is in your ScreenModel
suspend fun loginUser(result:(data: UserResponse?, error: String?) -> Unit){
repository.loginUser(model){response, error ->
response?.let {
result(it, null)
}
error?.let {
Log.i("error", "loginUser Error: $it")
result(null, it)
}
}
}
you can call it in your screen like below
screenModel.loginUser{data, error ->
response?.let{
//navigate another screen in here
}
}
There are two main approaches that I know:
- With UiState - set some flag that indicates you need to navigate somewhere, then consume it by unsetting it
screenmodel.navigetedToSomeScreen(). - With one time effects - use some kind of Channel<Event> that exposes events as Flow<Event>