Egor K.
11/29/2020, 10:28 AMArkadii Ivanov
11/29/2020, 10:35 AMEgor K.
11/29/2020, 10:36 AMArkadii Ivanov
11/29/2020, 10:48 AMinterface TodoListView : MviView<Model, Event> {
fun showError(text: String)
// Omitted code
}
Bind labels to view:
override fun onViewCreated(todoListView: TodoListView, viewLifecycle: Lifecycle) {
bind(viewLifecycle, BinderLifecycleMode.START_STOP) {
todoListStore.labels bindTo { todoListView.handleLabel(it) }
}
}
private fun TodoListView.handleLabel(label: TodoListStore.Label): Unit =
when (label) {
is TodoListStore.Label.LoadingError -> showError(text = "Loading failed")
}
Also you may prefer a separate sealed class in the view: Action. In this case you can write a mapper from Label to Action. Then you will just handle all Actions in the view.
Both examples are exhaustive, so if you add one more Label the code won't compile unless you cover the new case.Egor K.
11/29/2020, 11:06 AMfun onViewCreated(todoListView: TodoListView, todoListfragment: TodoListFragment) {
bind(viewLifecycle, BinderLifecycleMode.START_STOP) {
todoListStore.labels bindTo { todoListView.handleLabel(it, todoListfragment) }
}
}
private fun handleLabel(label: TodoListStore.Label, fragment: TodoListFragment) {
when (label) {
is TodoListStore.Label.LoadingError -> fragment.showDialog()
}
In Fragment:
fun showDialog() {
// some dialog?
}
Arkadii Ivanov
11/29/2020, 11:13 AMContext
into onViewCreated
.
private class ViewLabelHandler(
private val view: TodoListView,
private val context: Context
) : (TodoListStore.Label) -> Unit {
override fun invoke(label: TodoListStore.Label) =
when (label) {
is TodoListStore.Label.LoadingError -> view.showError(text = context.getString(R.string.error_msg))
}
}
override fun onViewCreated(todoListView: TodoListView, viewLifecycle: Lifecycle, context: Context) {
bind(viewLifecycle, BinderLifecycleMode.START_STOP) {
todoListStore.labels bindTo ViewLabelHandler(todoListView, context)
}
}
Context
with an interface:
interface Resources {
val errorMsg: String
}
Egor K.
11/29/2020, 11:26 AM