Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class InfoPresenterImpl extends MvpBasePresenter<InfoView> implements InfoPresenter {
- @NonNull
- private final InfoModel model;
- @Nullable
- private Subscription observing;
- @Nullable
- private Subscription updating;
- public InfoPresenterImpl(@NonNull InfoModel model) {
- this.model = model;
- }
- @Override
- public void attachView(InfoView attached) {
- super.attachView(attached);
- if (!isSubscribed(observing)) {
- observing = model.observeInfo().subscribe(s -> {
- InfoView view = getView();
- if (isViewAttached()) {
- view.setData(s);
- view.showContent();
- }
- }, t -> {
- if (isViewAttached()) {
- getView().showError(t, false);
- }
- });
- }
- }
- @Override
- public void detachView(boolean retainInstance) {
- super.detachView(retainInstance);
- if (!retainInstance) {
- tryToUnsubscribe(observing);
- tryToUnsubscribe(updating);
- }
- }
- @Override
- @AnyThread
- public void loadInformation(boolean pullToRefresh) {
- getView().showLoading(pullToRefresh);
- tryToUnsubscribe(updating);
- updating = model.updateInfo().subscribe(ignored -> { }, t -> {
- if (isViewAttached()) {
- getView().showError(t, true);
- }
- });
- }
- private void tryToUnsubscribe(@Nullable Subscription subscription) {
- if (isSubscribed(subscription)) {
- subscription.unsubscribe();
- }
- }
- private boolean isSubscribed(@Nullable Subscription subscription) {
- return subscription != null && !subscription.isUnsubscribed();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement