Skip to content

Instantly share code, notes, and snippets.

@susonthapa
Created July 16, 2020 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save susonthapa/8d0547097301e85fffe35cbb4141d4b3 to your computer and use it in GitHub Desktop.
Save susonthapa/8d0547097301e85fffe35cbb4141d4b3 to your computer and use it in GitHub Desktop.
updated activity
private fun fetchMovies() {
// remove any previous subscription
disposable?.dispose()
disposable = viewModel.getAllMovies()
.subscribe({
Timber.d("movies: $it")
when (it) {
is Lce.Loading -> {
if (adapter.itemCount == 0 && !binding.swipeToRefresh.isRefreshing) {
Timber.d("showing full screen loader")
binding.moviesRecycler.showLoadingView()
} else {
Timber.d("showing swipe refresh loader")
binding.swipeToRefresh.isRefreshing = true
}
}
is Lce.Content -> {
// check for empty list, it's better to check in view model
if (it.packet.isEmpty()) {
binding.moviesRecycler.showEmptyView()
} else {
binding.moviesRecycler.hideAllViews()
}
adapter.submitList(it.packet)
Timber.d("stopping swipe refresh")
binding.swipeToRefresh.isRefreshing = false
}
is Lce.Error -> {
// only show error if the list is empty
if (adapter.itemCount == 0) {
binding.moviesRecycler.showErrorView(it.throwable?.message)
}
binding.swipeToRefresh.isRefreshing = false
}
}
}, {
it.printStackTrace()
Timber.w("movies stream error")
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment