-
Notifications
You must be signed in to change notification settings - Fork 26
Open
Description
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.
Lines 24 to 51 in dc5d66c
| /** | |
| * 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
UiStatefor data flow execution states. - Define the base
uiStateStateFlow<HashMap<Int, UiState>> inBaseViewModelto handle separate UiState for each data flow. This helps avoid defining many states for all data flows in a ViewModel. - Sync all
UiStatewithisLoadingStateFlow to handle the "global loading state".
Who Benefits?
Developers