Created
October 19, 2021 14:29
-
-
Save tunjid/84a98b76807023805bc2edc57e239488 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lifecycleScope.launch { | |
repoAdapter.loadStateFlow.collect { loadState -> | |
// Show a retry header if there was an error refreshing, and items were previously | |
// cached OR default to the default prepend state | |
header.loadState = loadState.mediator | |
?.refresh | |
?.takeIf { it is LoadState.Error && repoAdapter.itemCount > 0 } | |
?: loadState.prepend | |
val isListEmpty = loadState.refresh is LoadState.NotLoading && repoAdapter.itemCount == 0 | |
// show empty list | |
emptyList.isVisible = isListEmpty | |
// Only show the list if refresh succeeds, either from the the local db or the remote. | |
list.isVisible = loadState.source.refresh is LoadState.NotLoading || loadState.mediator?.refresh is LoadState.NotLoading | |
// Show loading spinner during initial load or refresh. | |
progressBar.isVisible = loadState.mediator?.refresh is LoadState.Loading | |
// Show the retry state if initial load or refresh fails. | |
retryButton.isVisible = loadState.mediator?.refresh is LoadState.Error && repoAdapter.itemCount == 0 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment