简体   繁体   中英

Using Kotlin Sealed Classes for routing to different screens

Is it a good idea to create sealed classes as such:

sealed class Route<out T: Any> {
    data class ToRoute1<out T : Any>(val data: T) : Route<T>()
    data class ToRoute2<out T : Any>(val data: T) : Route<T>()
    data class ToRoute3<out T : Any>(val data: T) : Route<T>()
}

So the Route seal class will define all the possible destination that current screen could "route to". So for example "LoginScreen" can have the following sealed class:

sealed class LoginRoute<out T: Any> {
    data class ToSignUp<out T : Any>(val data: T) : LoginRoute<T>()
    data class ToUserDetails<out T : Any>(val data: T) : LoginRoute<T>()
    data class ToSomeOtherPossibleDestination<out T : Any>(val data: T) : LoginRoute<T>()
}

Now on in ViewModel I can have:

val proceedToDestination = MutableLiveData<Event<LoginRoute>>()

then in the view, I could just observe proceedToDestination and do the following:

viewModel.proceedToDestination.observe(this, Observer { route ->
    route?.consume()?.run {
        when (route) {
            LoginRoute.ToSignUp -> // TODO START SIGNUP
            LoginRoute.ToUserDetails-> // TODO GO TO USER DETAILS SCREEN
            LoginRoute.ToSomeOtherPossibleDestination-> // TODO GO TO OTHER POSSIBLE DESTINATIONS
        }
    }
})

My question is whether this is a good idea or a good practice overall? Thanks in advance.

Edit:

Just really want to make this alive again and maybe find some answers.

About sealed class - This is fine. I uploaded our Simple-Router approach which uses similar Routes. You can also benchmark with pretty interesting Kompass router.

Apart from implementation the difference here is in routing responsibility - in solutions I presented Presenter/ViewModel has access to Router and calls navigate(..) on it - in your approach you use Rx/LiveData and let your View handle it but this it's just another logic layer above it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM