Michael Ferguson
1 min readMay 10, 2020

--

You can also find gains by having multiple view state observers. Each observer can then map the “big” view state down into something discreet for each UI element. Finally, if you add a distinctUntilChanged call into the mix most observers are very quiet.

In my own implementation I use a single data class to define all view states but the idea is fundamentally the same. Find the discreet portion of the view state a given UI element needs to change state, map down to just that discreet portion, suppress duplicates with distinctUntilChange . It doesn’t eliminate the problem, but it definitely reduces the magnitude of it.

eg. (with the help of some extension functions to clean things up)

data class ViewState(val name, val enabled, ...)viewModel.viewState
.map { it.name }
.distinctUntilChanged()
.observe(this) {
button.text = it
}
viewModel.viewState
.map { it.enabled }
.distinctUntilChanged
.observe(this) {
if (it) thing2.visibility = View.VISIBLE else thing2.visibility = View.GONE
}

--

--

Michael Ferguson
Michael Ferguson

Written by Michael Ferguson

Android software developer. Views and opinions expressed are my own and not that of my employer.

No responses yet