SHARE
TWEET

Repository

a guest Oct 22nd, 2019 114 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.example.mvvmlivedatarxjava.repository;
  2.  
  3. import androidx.lifecycle.MutableLiveData;
  4.  
  5. import com.example.mvvmlivedatarxjava.model.ResponseNews;
  6. import com.example.mvvmlivedatarxjava.model.Status;
  7. import com.example.mvvmlivedatarxjava.network.ApiFactory;
  8. import com.example.mvvmlivedatarxjava.network.ApiServices;
  9.  
  10. import io.reactivex.android.schedulers.AndroidSchedulers;
  11. import io.reactivex.disposables.CompositeDisposable;
  12. import io.reactivex.disposables.Disposable;
  13. import io.reactivex.functions.Consumer;
  14. import io.reactivex.schedulers.Schedulers;
  15.  
  16. public class MainRepository {
  17.  
  18.     private static MainRepository mainRepository;
  19.     private ApiServices apiServices;
  20.     private CompositeDisposable compositeDisposable = new CompositeDisposable();
  21.     MutableLiveData<Status> status = new MutableLiveData<>();
  22.     MutableLiveData<Boolean> isLoading = new MutableLiveData<>();
  23.     MutableLiveData<Boolean> isError = new MutableLiveData<>();
  24.  
  25.  
  26.     public static MainRepository getInstance(){
  27.         if (mainRepository == null){
  28.             mainRepository = new MainRepository();
  29.         }
  30.         return mainRepository;
  31.     }
  32.  
  33.     public MainRepository(){
  34.         apiServices = ApiFactory.getRetrofit().create(ApiServices.class);
  35.     }
  36.  
  37.     public MutableLiveData<ResponseNews> getData(String sources, String apiKey){
  38.         MutableLiveData<ResponseNews> liveData = new MutableLiveData<>();
  39.  
  40.         isLoading.setValue(true);
  41.         Disposable disposable = apiServices.getData(sources, apiKey)
  42.                 .subscribeOn(Schedulers.io())
  43.                 .observeOn(AndroidSchedulers.mainThread())
  44.                 .subscribe(new Consumer<ResponseNews>() {
  45.                     @Override
  46.                     public void accept(ResponseNews responseNews) throws Exception {
  47.                         liveData.setValue(responseNews);
  48.                         isLoading.setValue(false);
  49.                     }
  50.                 }, new Consumer<Throwable>() {
  51.                     @Override
  52.                     public void accept(Throwable throwable) throws Exception {
  53.                         getError();
  54.                         isLoading.setValue(false);
  55.                         isError.setValue(true);
  56.                     }
  57.                 });
  58.         compositeDisposable.add(disposable);
  59.         return liveData;
  60.     }
  61.  
  62.     public MutableLiveData<Status> getStatus(){
  63.         status.setValue(Status.SUCESS);
  64.         return status;
  65.     }
  66.  
  67.     public MutableLiveData<Boolean> getIsLoading(){
  68.         return isLoading;
  69.     }
  70.  
  71.     public MutableLiveData<Boolean> getError(){
  72.         return getError();
  73.     }
  74.  
  75. }
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