Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revamp the UiState handling from the ViewModel to adapt multiple complex UI state logic #576

Open
luongvo opened this issue May 28, 2024 · 0 comments

Comments

@luongvo
Copy link
Member

luongvo commented May 28, 2024

Why

According to the problem in the recent projects to handle multiple complex UI state logic, such as skeleton loading with partial loading or separate/multiple error handling between data flows, we had revamped the UiState handling from the ViewModel to adapt and cover these cases. It's time to bring that new approach to replace the existing deprecated approach injectLoading from the base template.

/**
* To show loading manually, should call `hideLoading` after
*/
protected fun showLoading() {
if (loadingCount == 0) {
_isLoading.value = true
}
loadingCount++
}
/**
* To hide loading manually, should be called after `showLoading`
*/
protected fun hideLoading() {
loadingCount--
if (loadingCount == 0) {
_isLoading.value = false
}
}
protected fun launch(context: CoroutineContext = EmptyCoroutineContext, job: suspend () -> Unit) =
viewModelScope.launch(context) {
job.invoke()
}
protected fun <T> Flow<T>.injectLoading(): Flow<T> = this
.onStart { showLoading() }
.onCompletion { hideLoading() }

What we will do:

  • Define the base UiState for data flow execution states.
  • Define the base uiState StateFlow<HashMap<Int, UiState>> in BaseViewModel to handle separate UiState for each data flow. This helps avoid defining many states for all data flows in a ViewModel.
  • Sync all UiState with isLoading StateFlow to handle the "global loading state".

Who Benefits?

Developers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants