package com.turlir.abakgists.base;


import android.support.annotation.Nullable;

import com.turlir.abakgists.R;

import java.lang.ref.WeakReference;
import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

public abstract class BasePresenter<T extends BaseView> {

    private static final ObservableSchedulersTransformer STANDARD_SCHEDULER
            = new ObservableSchedulersTransformer();

    private final CompositeSubscription subs = new CompositeSubscription();

    private WeakReference<T> view = new WeakReference<>(null);

    public void attach(T view) {
        this.view = new WeakReference<>(view);
    }

    public void detach() {
        subs.clear();
    }

    @Nullable
    public final T getView() {
        return view.get();
    }

    protected void addSubscription(Subscription s) {
        subs.add(s);
    }

    protected void removeSubscription(Subscription s) {
        subs.remove(s);
    }

    protected <E> Observable.Transformer<E, E> defaultScheduler() {
        return (Observable.Transformer<E, E>) STANDARD_SCHEDULER;
    }

    private static final class ObservableSchedulersTransformer<T>
            implements Observable.Transformer<T, T> {
        @Override
        public Observable<T> call(Observable<T> upstream) {
            return upstream
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread());
        }
    }

    protected abstract class Handler<E> extends Subscriber<E> {

        @Override
        public void onCompleted() {

        }

        @Override
        public void onError(Throwable e) {
            processError(e);
        }
    }

    protected void processError(Throwable e) {
        e.printStackTrace();
        T view = getView();
        if (view != null) {
            CharSequence msg = view.getContext().getString(R.string.error_general);
            view.showError(msg.toString());
        }
    }


}

@@ -30,16 +30,59 @@
 base;%0A%0A%0A
+import android.support.annotation.NonNull;%0A
 import a
@@ -1593,32 +1593,1059 @@
        %7D%0A    %7D%0A%0A
+    protected abstract class ErrorHandler%3CE%3E extends Subscriber%3CE%3E %7B%0A%0A        private final TroubleSelector mRobot;%0A%0A        protected ErrorHandler() %7B%0A            super();%0A            mRobot = new TroubleSelector(additionalSituation());%0A        %7D%0A%0A        @Override%0A        public void onCompleted() %7B%0A%0A        %7D%0A%0A        @Override%0A        public void onError(Throwable throwable) %7B%0A            if (throwable instanceof Exception) %7B%0A                TroubleSelector.ErrorSituation callback =%0A                        mRobot.select((Exception) throwable, isDataAvailable(), isError());%0A%0A                callback.perform(interpreter(), (Exception) throwable);%0A            %7D%0A        %7D%0A%0A        @NonNull%0A        protected TroubleSelector.ErrorSituation%5B%5D additionalSituation() %7B%0A            return new TroubleSelector.ErrorSituation%5B0%5D;%0A        %7D%0A%0A        protected abstract boolean isError();%0A%0A        protected abstract boolean isDataAvailable();%0A%0A        protected abstract TroubleSelector.ErrorInterpreter interpreter();%0A    %7D%0A%0A
     protected ab

integrate TroubleSelector in BasePresenter