Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MainViewModel(
- private val userService: UserService
- ) : ViewModel() {
- val repositoriesLiveData = MutableLiveData<LiveDataResult<List<Repository>>>()
- val loadingLiveData = MutableLiveData<Boolean>()
- /**
- * Request user's repositories
- * @param githubUser Github usename
- */
- fun getRepositories(githubUser: String) {
- this.setLoadingVisibility(true)
- this.userService.getRepositories(githubUser).subscribe(GetRepositoriesConsumer())
- }
- /**
- * Set a progress dialog visible on the View
- * @param visible visible or not visible
- */
- fun setLoadingVisibility(visible: Boolean) {
- loadingLiveData.postValue(visible)
- }
- /**
- * userService.getRepositories() Observer
- */
- inner class GetRepositoriesConsumer : MaybeObserver<List<Repository>> {
- override fun onSubscribe(d: Disposable) {
- this@MainViewModel.repositoriesLiveData.postValue(LiveDataResult.loading())
- }
- override fun onError(e: Throwable) {
- this@MainViewModel.repositoriesLiveData.postValue(LiveDataResult.error(e))
- this@MainViewModel.setLoadingVisibility(false)
- }
- override fun onSuccess(t: List<Repository>) {
- this@MainViewModel.repositoriesLiveData.postValue(LiveDataResult.succes(t))
- this@MainViewModel.setLoadingVisibility(false)
- }
- override fun onComplete() {
- this@MainViewModel.setLoadingVisibility(false)
- }
- }
- }
Add Comment
Please, Sign In to add comment