-
Notifications
You must be signed in to change notification settings - Fork 36
6. Rendering State
James Shvarts edited this page Dec 11, 2018
·
1 revision
Assuming that our UI layer is implemented using Fragments, we can observe LiveData<State>
contained within a corresponding ViewModel.
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel = ViewModelProviders.of(this,
viewModelFactory.get(NoteListViewModel::class.java)
viewModel.observableState.observe(this, Observer { state ->
state?.let { renderState(state) }
})
where the renderState(state)
function could be something like:
private fun renderState(state: State) {
with(state) {
when {
isLoading -> renderLoadingState()
isError -> renderErrorState()
else -> renderNotesState(notes)
}
}
}
where each of the helper render
functions would be as simple as:
private fun renderLoadingState() {
loadingIndicator.visibility = View.VISIBLE
}
Having a central renderState(state)
function gives us a single place to look if the UI is not rendering what we expect. Combined with logging States that come in, we can spot and resolve bugs easier.