Skip to main content
deleted 88 characters in body; edited tags
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

I started using Retrofit and RxJava recently. and I love it. I am working on an Android project. and I am following MVP Repository pattern as specified in Android Architecture Components,. I'm still learning though.

  1. fruitsRepository is a Repository that can get data from local or remote source.
  2. .getFeaturedFruits() calls retrofit internally if remote source is selected
  3. compositeDisposable is CompositeDisposable
  4. UniversalResponse is a class having httpStatusCode, isSuccess and List<T>
  5. view is an implementation of UI from FeaturedFruitsContract.View

I am looking for some improvements to this. Can I reduce this somehow? If I have to make 5 different API calls, my presenter gets really bigger and repetitive.

So, how can I improve this code? I was thinking about making an adapter for Observer I am using to subscribe.

p.s.

  1. fruitsRepository is a Repository that can get data from local or remote source.

  2. .getFeaturedFruits() calls retrofit internally if remote source is selected

  3. compositeDisposable is CompositeDisposable

  4. UniversalResponse is a class having httpStatusCode, isSuccess and List

  5. view is an implementation of UI from FeaturedFruitsContract.View

If more information is required, please comment.

I started using Retrofit and RxJava recently. I love it. I am working on an Android project. I am following MVP Repository pattern as specified in Android Architecture Components, still learning though.

I am looking for some improvements to this. Can I reduce this somehow? If I have to make 5 different API calls, my presenter gets really bigger and repetitive.

So, how can I improve this code? I was thinking about making an adapter for Observer I am using to subscribe.

p.s.

  1. fruitsRepository is a Repository that can get data from local or remote source.

  2. .getFeaturedFruits() calls retrofit internally if remote source is selected

  3. compositeDisposable is CompositeDisposable

  4. UniversalResponse is a class having httpStatusCode, isSuccess and List

  5. view is an implementation of UI from FeaturedFruitsContract.View

If more information is required, please comment.

I started using Retrofit and RxJava recently and I love it. I am working on an Android project and I am following MVP Repository pattern as specified in Android Architecture Components. I'm still learning though.

  1. fruitsRepository is a Repository that can get data from local or remote source.
  2. .getFeaturedFruits() calls retrofit internally if remote source is selected
  3. compositeDisposable is CompositeDisposable
  4. UniversalResponse is a class having httpStatusCode, isSuccess and List<T>
  5. view is an implementation of UI from FeaturedFruitsContract.View

I am looking for some improvements to this. Can I reduce this somehow? If I have to make 5 different API calls, my presenter gets really bigger and repetitive. I was thinking about making an adapter for Observer I am using to subscribe.

edited body
Source Link
fruitsRepository
  .getFeaturedFruits()
  .subscribeOn(Schedulers.io())
  .observeOn(AndroidSchedulers.mainThread())
  .subscribe(new Observer<UniversalResponse<Fruit>>() {
    
    @Override
    public void onSubscribe(Disposable d) {
        compositeDisposable.add(d);
    }

    @Override
    public void onNext(UniversalResponse<Fruit> universalResponse) {

        if (view.isLost()) return;
        view.setProgressVisibility(false);

        if (universalResponse.isSessionExpired()) {

            view.onSessionExpired();
            return;
        }

        if (universalResponse.isError()) {

            if (universalResponse.isServerError()) {
                view.showErrorViewerPage(universalResponse.getServerErrorMessage());
            }

            view.showFeaturedFruitsError(universalResponse.getMessage());
            return;
        }

        List<Fruit> fruits = universalResponse.getItems();
        if (fruits != null && fruits.size() > 0) {

            view.showNoFeaturedFruitsshowFeaturedFruits(fruits);
        }
        else {
            view.showFeaturedFruitsshowNoFeaturedFruits(fruits);
        }
    }

    @Override
    public void onError(Throwable e) {

        if (view.isLost()) return;
        view.setProgressVisibility(false);

        view.showFeaturedFruitsError(e.getMessage());
    }

    @Override
    public void onComplete() {

        if (view.isLost()) return;
        view.setProgressVisibility(false);
    }
});
fruitsRepository
  .getFeaturedFruits()
  .subscribeOn(Schedulers.io())
  .observeOn(AndroidSchedulers.mainThread())
  .subscribe(new Observer<UniversalResponse<Fruit>>() {
    
    @Override
    public void onSubscribe(Disposable d) {
        compositeDisposable.add(d);
    }

    @Override
    public void onNext(UniversalResponse<Fruit> universalResponse) {

        if (view.isLost()) return;
        view.setProgressVisibility(false);

        if (universalResponse.isSessionExpired()) {

            view.onSessionExpired();
            return;
        }

        if (universalResponse.isError()) {

            if (universalResponse.isServerError()) {
                view.showErrorViewerPage(universalResponse.getServerErrorMessage());
            }

            view.showFeaturedFruitsError(universalResponse.getMessage());
            return;
        }

        List<Fruit> fruits = universalResponse.getItems();
        if (fruits != null && fruits.size() > 0) {

            view.showNoFeaturedFruits();
        }
        else {
            view.showFeaturedFruits(fruits);
        }
    }

    @Override
    public void onError(Throwable e) {

        if (view.isLost()) return;
        view.setProgressVisibility(false);

        view.showFeaturedFruitsError(e.getMessage());
    }

    @Override
    public void onComplete() {

        if (view.isLost()) return;
        view.setProgressVisibility(false);
    }
});
fruitsRepository
  .getFeaturedFruits()
  .subscribeOn(Schedulers.io())
  .observeOn(AndroidSchedulers.mainThread())
  .subscribe(new Observer<UniversalResponse<Fruit>>() {
    
    @Override
    public void onSubscribe(Disposable d) {
        compositeDisposable.add(d);
    }

    @Override
    public void onNext(UniversalResponse<Fruit> universalResponse) {

        if (view.isLost()) return;
        view.setProgressVisibility(false);

        if (universalResponse.isSessionExpired()) {

            view.onSessionExpired();
            return;
        }

        if (universalResponse.isError()) {

            if (universalResponse.isServerError()) {
                view.showErrorViewerPage(universalResponse.getServerErrorMessage());
            }

            view.showFeaturedFruitsError(universalResponse.getMessage());
            return;
        }

        List<Fruit> fruits = universalResponse.getItems();
        if (fruits != null && fruits.size() > 0) {

            view.showFeaturedFruits(fruits);
        }
        else {
            view.showNoFeaturedFruits();
        }
    }

    @Override
    public void onError(Throwable e) {

        if (view.isLost()) return;
        view.setProgressVisibility(false);

        view.showFeaturedFruitsError(e.getMessage());
    }

    @Override
    public void onComplete() {

        if (view.isLost()) return;
        view.setProgressVisibility(false);
    }
});
Source Link

About handling data from an API call through RX and Retrofit

I started using Retrofit and RxJava recently. I love it. I am working on an Android project. I am following MVP Repository pattern as specified in Android Architecture Components, still learning though.

Whenever I have to make an API call, I do it like this in the Presenter:

fruitsRepository
  .getFeaturedFruits()
  .subscribeOn(Schedulers.io())
  .observeOn(AndroidSchedulers.mainThread())
  .subscribe(new Observer<UniversalResponse<Fruit>>() {
    
    @Override
    public void onSubscribe(Disposable d) {
        compositeDisposable.add(d);
    }

    @Override
    public void onNext(UniversalResponse<Fruit> universalResponse) {

        if (view.isLost()) return;
        view.setProgressVisibility(false);

        if (universalResponse.isSessionExpired()) {

            view.onSessionExpired();
            return;
        }

        if (universalResponse.isError()) {

            if (universalResponse.isServerError()) {
                view.showErrorViewerPage(universalResponse.getServerErrorMessage());
            }

            view.showFeaturedFruitsError(universalResponse.getMessage());
            return;
        }

        List<Fruit> fruits = universalResponse.getItems();
        if (fruits != null && fruits.size() > 0) {

            view.showNoFeaturedFruits();
        }
        else {
            view.showFeaturedFruits(fruits);
        }
    }

    @Override
    public void onError(Throwable e) {

        if (view.isLost()) return;
        view.setProgressVisibility(false);

        view.showFeaturedFruitsError(e.getMessage());
    }

    @Override
    public void onComplete() {

        if (view.isLost()) return;
        view.setProgressVisibility(false);
    }
});

I am looking for some improvements to this. Can I reduce this somehow? If I have to make 5 different API calls, my presenter gets really bigger and repetitive.

So, how can I improve this code? I was thinking about making an adapter for Observer I am using to subscribe.

p.s.

  1. fruitsRepository is a Repository that can get data from local or remote source.

  2. .getFeaturedFruits() calls retrofit internally if remote source is selected

  3. compositeDisposable is CompositeDisposable

  4. UniversalResponse is a class having httpStatusCode, isSuccess and List

  5. view is an implementation of UI from FeaturedFruitsContract.View

If more information is required, please comment.