Search code examples
androidrx-javarx-java2

Migrate method RxJava1 to RxJava2


I have inherited an app and I am trying to update the RxJava library.

I am having enough problems since I do not quite understand how it is programmed, I would like to know if someone can help me :)

Before publishing I searched enough on Google and here to find out how to migrate but I can't find a solution, since everything I see doesn't work.

I have an interface and a class with the methods that use Rx.

This is the interface method:

@GET(QUERY_ENTITIES)
Observable<Response<List<DBFile>>> dbFileList(@Path("classId") String classId, @QueryMap Map<String, String> parameters);

And this is the Rx method:

private void loadDBData(final OnDataRequestListener onDataRequestListener,
                        final OnDBCallbackListener onDBCallbackListener,
                        String classId, Map<String, String> parameters) {

    /* REQUESTS THE DBs' DATA */
    mRestInterface
            .dbFileList(classId, parameters)
            .subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread())
            .first(new Func1<Response<List<DBFile>>, Boolean>() {
                @Override
                public Boolean call(Response<List<DBFile>> listResponse) {
                    if(listResponse.code() == HttpURLConnection.HTTP_FORBIDDEN) {
                        return false;
                    }
                    return true;

                }
            })
            /* Separates the DB list from the response */
            .flatMap(new Func1<Response<List<DBFile>>, Observable<DBFile>>() {
                @Override
                public Observable<DBFile> call(Response<List<DBFile>> responseJobs) {
                    return Observable.from(responseJobs.body());
                }

            })
            /* Transforms the data into a db data observable */
            .flatMap(new Func1<DBFile, Observable<DBFile>>() {
                @Override
                public Observable<DBFile> call(DBFile data) {
                    return Observable.just(data);
                }
            })
            /* Define retry (3 Times) */
            .retry(new Func2<Integer, Throwable, Boolean>() {
                @Override
                public Boolean call(Integer attempts, Throwable throwable) {
                    return attempts <= TOTAL_RETRIES;
                }
            })
            /*
             Transforms the data into a list to receive the answer just when the entire list is loaded
             */
            .toList()
            /* Defines a request timeout */
            .timeout(CONNECTION_TIMEOUT, TimeUnit.SECONDS)
            /* Sends the data to the main presenter */
            .subscribe(new Observer<List<DBFile>>() {

                @Override
                public void onCompleted() {
                    if (onDBCallbackListener instanceof VerifyApplicationUpdatesPresenter){
                        ((VerifyApplicationUpdatesPresenter) onDBCallbackListener).checkConf();
                    }else{
                        onDataRequestListener.onCompleted();
                    }
                }

                @Override
                public void onError(Throwable e) {
                    if(e instanceof NoSuchElementException){
                        onDataRequestListener.onDataError(e.toString());
                    }else {
                        onDataRequestListener.onDataError();
                    }
                }

                @Override
                public void onNext(List<DBFile> dbFileList) {
                    if (onDBCallbackListener != null) {
                        onDBCallbackListener.onDBDataCollected(dbFileList);
                    }

                }
            });

}

I would greatly appreciate your help! Thank you!


Solution

  • Change you Interface return type like below

     @GET(QUERY_ENTITIES)
    Observable<List<DBFile>> dbFileList(@Path("classId") String classId, @QueryMap Map<String, String> parameters);
    

    Change your api call function like below

    mRestInterface.dbFileList(classId, parameters)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                /* Define retry (3 Times) */
                .retry(3)
                /* Defines a request timeout */
                .timeout(CONNECTION_TIMEOUT, TimeUnit.SECONDS)
                /* Sends the data to the main presenter */
                .subscribe(new Observer<List<DBFile>>() {
                    @Override
                    public void onSubscribe(Disposable disposable) {
    
                    }
    
                    @Override
                    public void onNext(List<DBFile> dbFiles) {
                        if (onDBCallbackListener != null) {
                            onDBCallbackListener.onDBDataCollected(dbFileList);
                        }
                    }
    
                    @Override
                    public void onError(Throwable throwable) {
                        if(e instanceof NoSuchElementException){
                            onDataRequestListener.onDataError(e.toString());
                        }else {
                            onDataRequestListener.onDataError();
                        }
                    }
    
                    @Override
                    public void onComplete() {
                        if (onDBCallbackListener instanceof VerifyApplicationUpdatesPresenter){
                            ((VerifyApplicationUpdatesPresenter) onDBCallbackListener).checkConf();
                        }else{
                            onDataRequestListener.onCompleted();
                        }
                    }
                });