Scott Kruse
07/21/2021, 8:40 PMsealed class Action {
data class ButtonClickAction(val data: Int): Action()
}
interface ActionHandler {
suspend fun handleAction(action: Action)
}
class Fragment() {
button.setOnClickListener {
lifecycleScope.launch {
viewModel.handleAction(ButtonClickAction(data = 100))
}
}
}
class ViewModel(
private val otherActionHandler: ActionHandler,
private val otherActionHandler2: ActionHandler): ActionHandler {
override suspend fun handleAction(action: Action) {
when(action) {
is ButtonClickAction -> {
otherActionHandler.handleAction(action)
}
else -> {
otherActionHandler2.handleAction(action)
}
}
}
}