Skip to content

Instantly share code, notes, and snippets.

@rodolfoizidoro
Created May 7, 2019 10:53
Show Gist options
  • Save rodolfoizidoro/840c5d29f4baf231fc39347114cc92eb to your computer and use it in GitHub Desktop.
Save rodolfoizidoro/840c5d29f4baf231fc39347114cc92eb to your computer and use it in GitHub Desktop.
class MovieListViewModel(private val repository: MovieRepository) : BaseViewModel() {
private val mMovies = StateMutableLiveData<List<Movie>, Throwable>()
val movies : StateLiveData<List<Movie>, Throwable> get() = mMovies
fun findMovies() {
jobs add launch {
mMovies.loading.value = true
try {
val response = repository.findMovies().await().results
mMovies.success.value = response.sortedByDescending { it.releaseDate }
} catch (t: Throwable) {
mMovies.error.value = t
}
finally {
mMovies.loading.value = false
}
}
}
}
viewModel.movies.observe(this,
onSuccess = { list: List<Movie> ->
rvMovies.adapter = MoviesAdapter(list) {
flowController.openMovieDetail(it)
}
},
onLoading = { isLoading: Boolean ->
progress.show(isLoading)
},
onError = { throwable: Throwable ->
toast(throwable.message.toString())
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment