SHARE
TWEET

Untitled

a guest May 23rd, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class MoviesDispatcher @Inject constructor(private val repository: MovieRepository) {
  2.  
  3.     fun dispatchAction(action: Action) = liveData {
  4.         when (action) {
  5.             is MoviesAction.GetMoviesAction -> {
  6.                 emit(MoviesResult.Loading)
  7.                 emit(getPopularMovies())
  8.             }
  9.         }
  10.     }
  11.  
  12.     private suspend fun getPopularMovies(): MoviesResult {
  13.         try {
  14.             val response = repository.getPopularMovies().await()
  15.             if (response.isSuccessful) {
  16.                 response.body()?.let { responseBody ->
  17.                     responseBody.results?.run {
  18.                         return MoviesResult.Success(this)
  19.                     }
  20.                 }
  21.             }
  22.         } catch (t: Throwable) {
  23.             return MoviesResult.Failure(t.localizedMessage)
  24.         }
  25.  
  26.         return MoviesResult.Failure("Failure to retrieve movies.")
  27.     }
  28. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top