0

After couple of days I can use RxJava2 and Retrofit2 in an independent way. Now I'm trying to fulfill the challenge of a Clean Architecture with RxJava2 and Retrofit2. I did a nodejs server for a simple REST service on my local machine.

This is what I got so far, It's working fine, but I'm looking for the correct way to do this.

On My Presenter Class:

private void initLoadContentRx(){
    Observable.just("") //I havent been able to send void even if I dont need an input
            .subscribeOn(Schedulers.newThread())//moving to another thread
            .map(new Function<String, List<BImagenes>>() { //interface for input,output.
                @Override
                public List<BImagenes> apply(String cad){ // cad is the input value
                    Log.d(TAG,"long operation in thread: "+Thread.currentThread().getName()); //making sure we are in a second thread
                    return  loadContentWithRetrofit();
                }
            })
            .observeOn(AndroidSchedulers.mainThread()) // Get the results in Main Thread
            .subscribe(observer); //Observer which get notified when the results are done.
}

Observer:

Observer observer = new Observer() {
    @Override
    public void onSubscribe(Disposable d) { Log.d(TAG,"onSubscribe, thread name >> "+Thread.currentThread().getName()); }

    @Override
    public void onNext(Object value) {
        Log.d(TAG,"on next,  thread name >> "+Thread.currentThread().getName());
        try{
            List<BImagenes> list = (List<BImagenes>) value;
            MySingleton.setList(list);//save the results in a Singleton
            Log.d(TAG,"i got my list parsed as POJO, with retrofit in thread >> "+Thread.currentThread().getName()+", list size >> "+list.size());
        }catch (Exception e){
            Log.d(TAG,"error in parsing >> "+e.toString());
        }

    }

    @Override
    public void onError(Throwable e) { Log.d(TAG,"onError error >>"+e.toString()); }

    @Override
    public void onComplete() {
        Log.d(TAG,"onCompleted  thread name >> "+Thread.currentThread().getName());
        presenterComm.showListView(MySingleton.getList());//Throught a communication interface implemented in Activity, I update the ListView
    }
};

So, after I got my results, I update the ListView in my Activity, through a instance of an interface class that is implemented by this Activity.

gradle dependecies

compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'io.reactivex.rxjava2:rxjava:2.0.1'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'

Thank you for your time

LearningCharlito
  • 327
  • 1
  • 2
  • 20

1 Answers1

0

To implement right way of Retrofit2 with RxJava2 / RxAndroid using MVP Pattern for that you need to be cleared with observer subscriber method. To understand better MVP Pattern view this sample example :

 interface BaseContract {
    interface BaseView {
        //Methods for View
        void onDoSomething();
    }

    interface BasePresenter {
        void doSomething();

    }
}

class BaseMainPresenter implements BaseContract.BasePresenter {
    BaseContract.BaseView view;

    BaseMainPresenter(BaseContract.BaseView view) {
        this.view = view;
    }

    @Override
    public void doSomething() {
        if (view != null)
            view.onDoSomething();
    }
}

class DemoClass implements BaseContract.BaseView {

    //Create object of Presenter 

    /****
     * Example :
     * BaseMainPresenter baseMainPresenter = new BaseMainPresenter(this);
     */
    @Override
    public void onDoSomething() {
        //Deal with Context here.
    }
}

With respect to this MVP Pattern you need to implement Retrofit2 using RxJava You can refer one of my reference of Sample of Retrozing