Hello,
I’m having a hard time finding an implementation that will allow me to provide different actions on a button click as an example.
Let’s say there are three types of users A, B and C. On a button click user A calls method A, user B calls method B and user C calls method A and B.
I am trying to use this with MVVM. So far I’ve got this idea:
Having defined an ActionState
class, that is observed in the MainActivity. Should work like a ViewState
.
sealed class ActionState {
object UserTypeAAcceptOrder : ActionState()
object UserTypeBAcceptOrder : ActionState()
object UserTypeCAcceptOrder : ActionState()
}
viewModel.actionState.observe(this) { state ->
when (state) {
is MainActivityViewModel.ActionState.UserTypeAAcceptOrder -> methodA()
is MainActivityViewModel.ActionState.UserTypeBAcceptOrder -> methodB()
is MainActivityViewModel.ActionState.UserTypeCAcceptOrder -> {
methodA()
methodB()
}
}
}
I don’t know if this is a good approach, I find it ok, but I’m not happy with it. Happy to hear your opinion and your suggestions. Let me know if there are things that need clarification.
LE: I assume there are cases where the logic can be moved outside the Activity/Fragment.