fix nothing to see here showing when it shouldn't

This commit is contained in:
Matthieu 2021-04-28 15:12:21 +02:00
parent 6efa946809
commit 34660c1280
2 changed files with 6 additions and 5 deletions

View File

@ -55,7 +55,7 @@ internal fun <T: Any> initAdapter(
if(!progressBar.isVisible && swipeRefreshLayout.isRefreshing) {
// Stop loading spinner when loading is done
swipeRefreshLayout.isRefreshing = loadState.mediator?.refresh is LoadState.Loading
swipeRefreshLayout.isRefreshing = loadState.refresh is LoadState.Loading
} else {
// ProgressBar should stop showing as soon as the source stops loading ("source"
// meaning the database, so don't wait on the network)
@ -63,7 +63,7 @@ internal fun <T: Any> initAdapter(
if(!sourceLoading && recyclerView.size > 0){
recyclerView.isVisible = true
progressBar.isVisible = false
} else if(recyclerView.size == 0
} else if(adapter.itemCount == 0
&& loadState.append is LoadState.NotLoading
&& loadState.append.endOfPaginationReached){
progressBar.isVisible = false
@ -73,7 +73,7 @@ internal fun <T: Any> initAdapter(
}
// Toast on any error, regardless of whether it came from RemoteMediator or PagingSource
// Show any error, regardless of whether it came from RemoteMediator or PagingSource
val errorState = loadState.source.append as? LoadState.Error
?: loadState.source.prepend as? LoadState.Error
?: loadState.source.refresh as? LoadState.Error
@ -83,6 +83,7 @@ internal fun <T: Any> initAdapter(
errorState?.let {
showError(motionLayout = motionLayout, errorLayout = errorLayout, errorText = it.error.toString())
}
// If the state is not an error, hide the error layout
if(errorState == null) {
showError(motionLayout = motionLayout, errorLayout = errorLayout, show = false, errorText = "")
}

View File

@ -46,11 +46,11 @@ open class CachedFeedFragment<T: FeedContentDatabase> : BaseFragment() {
internal fun initSearch() {
// Scroll to top when the list is refreshed from network.
lifecycleScope.launchWhenCreated {
lifecycleScope.launchWhenStarted {
adapter.loadStateFlow
// Only emit when REFRESH LoadState for RemoteMediator changes.
.distinctUntilChangedBy {
it.source.refresh
it.refresh
}
// Only react to cases where Remote REFRESH completes i.e., NotLoading.
.filter { it.refresh is NotLoading}