voyager icon indicating copy to clipboard operation
voyager copied to clipboard

Navigation from ScreeModel

Open GtechGovind opened this issue 1 year ago • 2 comments

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.

GtechGovind avatar Apr 02 '24 15:04 GtechGovind

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
       }
 }

akardas16 avatar Apr 05 '24 12:04 akardas16

There are two main approaches that I know:

  1. With UiState - set some flag that indicates you need to navigate somewhere, then consume it by unsetting it screenmodel.navigetedToSomeScreen().
  2. With one time effects - use some kind of Channel<Event> that exposes events as Flow<Event>

nvkleban avatar Apr 07 '24 12:04 nvkleban