Advertisement
AndrewSireko

SignUpPresentor

Jan 18th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.15 KB | None | 0 0
  1. class SignUpPresenter implements SignUpContract.Presenter {
  2.  
  3.     private final DataRepository repository;
  4.     private final SignUpContract.View view;
  5.     private final CompositeSubscription subscriptions = new CompositeSubscription();
  6.  
  7.     SignUpPresenter(@NonNull DataRepository repository, @NonNull SignUpContract.View view) {
  8.         this.repository = checkNotNull(repository);
  9.         this.view = checkNotNull(view);
  10.     }
  11.  
  12.     @Override
  13.     public void stop() {
  14.         RxUtils.unsubscribe(subscriptions);
  15.     }
  16.  
  17.     @Override
  18.     public void checkEmail(String email) {
  19.         view.showProgress();
  20.         subscriptions.add(repository.checkEmail(email)
  21.                 .subscribeOn(Schedulers.io())
  22.                 .observeOn(AndroidSchedulers.mainThread())
  23.                 .subscribe(this::validationSuccess, throwable -> responseError(throwable.getMessage())));
  24.     }
  25.  
  26.     @Override
  27.     public void checkOrgName(String email, String orgName) {
  28.         view.showProgress();
  29.         subscriptions.add(repository.checkOrgName(email, orgName)
  30.                 .subscribeOn(Schedulers.io())
  31.                 .observeOn(AndroidSchedulers.mainThread())
  32.                 .subscribe(this::validationSuccess, throwable -> responseError(throwable.getMessage())));
  33.     }
  34.  
  35.     @Override
  36.     public void checkOrgLink(String link) {
  37.         view.showProgress();
  38.         subscriptions.add(repository.checkOrgLink(link)
  39.                 .subscribeOn(Schedulers.io())
  40.                 .observeOn(AndroidSchedulers.mainThread())
  41.                 .subscribe(this::validationSuccess, throwable -> responseError(throwable.getMessage())));
  42.     }
  43.  
  44.     @Override
  45.     public void checkUserName(String userName) {
  46.         view.showProgress();
  47.         subscriptions.add(repository.checkUserName(userName)
  48.                 .subscribeOn(Schedulers.io())
  49.                 .observeOn(AndroidSchedulers.mainThread())
  50.                 .subscribe(this::validationSuccess, throwable -> responseError(throwable.getMessage())));
  51.     }
  52.  
  53.     @Override
  54.     public void signUp(String email, String orgName, String orgLink, String userName, String password) {
  55.         view.showProgress();
  56.         subscriptions.add(repository.signUp(email, orgName, orgLink, userName, password)
  57.                 .subscribeOn(Schedulers.io())
  58.                 .observeOn(AndroidSchedulers.mainThread())
  59.                 .subscribe(SignUpPresenter.this::signUpSuccess, throwable -> responseError(throwable.getMessage())));
  60.     }
  61.  
  62.     private void validationSuccess(PhoxResponse<ValidationStatus> response) {
  63.         view.hideProgress();
  64.         if (response.isSuccess()) {
  65.             view.validationSuccess();
  66.         } else {
  67.             view.showError(response.getError().getMessage());
  68.         }
  69.     }
  70.  
  71.     private void signUpSuccess(PhoxResponse<User> response) {
  72.         if (response.isSuccess()) {
  73.             view.onSignUpSuccess(response.getData());
  74.         } else {
  75.             view.hideProgress();
  76.             view.showError(response.getError().getMessage());
  77.         }
  78.     }
  79.  
  80.     private void responseError(String message) {
  81.         view.hideProgress();
  82.         view.showError(message);
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement