SHARE
TWEET

Untitled

a guest Oct 22nd, 2019 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package me.alhaz.tutorial.architecturecomponent.models.repositories.movie.remote
  2.  
  3. import androidx.lifecycle.LiveData
  4. import androidx.lifecycle.MutableLiveData
  5. import me.alhaz.tutorial.architecturecomponent.BuildConfig
  6. import me.alhaz.tutorial.architecturecomponent.helper.RetrofitConfig
  7. import me.alhaz.tutorial.architecturecomponent.helper.valueobject.ApiResponse
  8. import me.alhaz.tutorial.architecturecomponent.models.repositories.movie.remote.response.Movie
  9. import me.alhaz.tutorial.architecturecomponent.models.repositories.movie.remote.response.MovieResponse
  10. import retrofit2.Call
  11. import retrofit2.Callback
  12. import retrofit2.Response
  13.  
  14. class MovieRemoteRepository {
  15.  
  16.     fun getListMovie(): LiveData<ApiResponse<ArrayList<Movie>>> {
  17.  
  18.         val moviesLiveData = MutableLiveData<ApiResponse<ArrayList<Movie>>>()
  19.  
  20.         RetrofitConfig().getMovieService().getListMovie(BuildConfig.API_KEY, BuildConfig.LANGUAGE).enqueue(object: Callback<MovieResponse> {
  21.             override fun onResponse(call: Call<MovieResponse>, response: Response<MovieResponse>) {
  22.                 if (response.isSuccessful) {
  23.                     val responseData = response.body()
  24.                     responseData?.let { moviePopularResponse ->
  25.                         moviePopularResponse.results?.let { moviesResult ->
  26.                             val movies = ArrayList<Movie>()
  27.                             movies.addAll(moviesResult)
  28.                             moviesLiveData.postValue(ApiResponse.success(movies))
  29.                         }
  30.                     }
  31.                 }
  32.                 else {
  33.  
  34.                 }
  35.             }
  36.  
  37.             override fun onFailure(call: Call<MovieResponse>, t: Throwable) {
  38.                 // todo change to ApiResonse.error(message, object)
  39.                 moviesLiveData.postValue(null)
  40.             }
  41.         })
  42.         return moviesLiveData
  43.     }
  44.  
  45.     fun getDetailMovie(movieID: Long): LiveData<ApiResponse<Movie>> {
  46.  
  47.         val movieLiveData = MutableLiveData<ApiResponse<Movie>>()
  48.         RetrofitConfig().getMovieService().getDetailMovie(movieID, BuildConfig.API_KEY, BuildConfig.LANGUAGE).enqueue(object: Callback<Movie> {
  49.             override fun onResponse(call: Call<Movie>, response: Response<Movie>) {
  50.                 if (response.isSuccessful) {
  51.                     val responseData = response.body()
  52.                     responseData?.let { movie ->
  53.                         movieLiveData.postValue(ApiResponse.success(movie))
  54.                     }
  55.                 }
  56.                 else {
  57.  
  58.                 }
  59.             }
  60.  
  61.             override fun onFailure(call: Call<Movie>, t: Throwable) {
  62.                 // todo change to ApiResonse.error(message, object)
  63.                 movieLiveData.postValue(null)
  64.             }
  65.         })
  66.         return movieLiveData
  67.     }
  68.  
  69. }
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
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top