Currently I've this approach in my app:
ViewState(one viewState to each screen)
sealed class CategoriesViewState {
object Loading : CategoriesViewState()
data class Error(
val errorMessage: String,
val messageType: UIComponentType
) : CategoriesViewState()
data class CategoryList(
val categories: List<Category>
) : CategoriesViewState()
}
And I observe this state in my fragments/activites using live data:
viewModel.viewState.observe(viewLifecycleOwner, Observer {
when (it) {
is CategoriesViewState.Loading -> {
progress_bar.visibility = View.VISIBLE
Log.d(TAG, "LOADING")
}
is CategoriesViewState.Error -> {
progress_bar.visibility = View.GONE
Log.d(TAG, "ERROR")
}
is CategoriesViewState.CategoryList -> {
progress_bar.visibility = View.GONE
Log.d(TAG, "DATA")
}
}
})
And it is working fine.
BUT it seems to me inefficient as the app grows.
Let's say I've 20 screens in my app: I'll need 20 viewStates, I'll need to write the same when statement in every screen, I'll need to write this ugly Visible/Gone in every screen(not to mention I need to set Loading state in every call)
Maybe I'm totally wrong and it's common approach, But to me it seems like A LOT of code duplication.
I haven't a specific question, I Just wanna know if it is common approach in Android Development and if not, what am I doing wrong in my code?