Guest User

Untitled

a guest
Oct 18th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. class MainViewModel(
  2. private val userService: UserService
  3. ) : ViewModel() {
  4.  
  5. val repositoriesLiveData = MutableLiveData<LiveDataResult<List<Repository>>>()
  6.  
  7. val loadingLiveData = MutableLiveData<Boolean>()
  8.  
  9. /**
  10. * Request user's repositories
  11. * @param githubUser Github usename
  12. */
  13. fun getRepositories(githubUser: String) {
  14. this.setLoadingVisibility(true)
  15. this.userService.getRepositories(githubUser).subscribe(GetRepositoriesConsumer())
  16. }
  17.  
  18. /**
  19. * Set a progress dialog visible on the View
  20. * @param visible visible or not visible
  21. */
  22. fun setLoadingVisibility(visible: Boolean) {
  23. loadingLiveData.postValue(visible)
  24. }
  25.  
  26. /**
  27. * userService.getRepositories() Observer
  28. */
  29. inner class GetRepositoriesConsumer : MaybeObserver<List<Repository>> {
  30. override fun onSubscribe(d: Disposable) {
  31. this@MainViewModel.repositoriesLiveData.postValue(LiveDataResult.loading())
  32. }
  33.  
  34. override fun onError(e: Throwable) {
  35. this@MainViewModel.repositoriesLiveData.postValue(LiveDataResult.error(e))
  36. this@MainViewModel.setLoadingVisibility(false)
  37. }
  38.  
  39. override fun onSuccess(t: List<Repository>) {
  40. this@MainViewModel.repositoriesLiveData.postValue(LiveDataResult.succes(t))
  41. this@MainViewModel.setLoadingVisibility(false)
  42. }
  43.  
  44. override fun onComplete() {
  45. this@MainViewModel.setLoadingVisibility(false)
  46. }
  47.  
  48. }
  49. }
Add Comment
Please, Sign In to add comment