Created
May 7, 2019 10:53
-
-
Save rodolfoizidoro/840c5d29f4baf231fc39347114cc92eb to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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 | |
} | |
} | |
} | |
} |
This file contains hidden or 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
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