package com.wasu.cs.mvp.presenter;

import com.wasu.cs.mvp.IView.MvpView;

/**
 * Presenter基类
 * Tips:使用BasePresenter或者BasePresenter子类的View必须是实现
 * 了MvpView接口或者是MvpView子接口
 */
public class BasePresenter<T extends MvpView> implements Presenter<T> {

    private T mMvpView;

    /**
     *
     * @param mvpView  把Activity,View或者Fragment的引用复制过来，方便在调用View接口做回调
     */
    @Override
    public void attachView(T mvpView) {
        this.mMvpView = mvpView;
    }


    @Override
    public void detachView() {
        this.mMvpView = null;
    }


    public boolean isViewAttached() {
        return mMvpView != null;
    }


    public T getMvpView() {
        return mMvpView;
    }


    public void checkViewAttached() {
        if (!isViewAttached()) throw new MvpViewNotAttachedException();
    }


    public static class MvpViewNotAttachedException extends RuntimeException {
        public MvpViewNotAttachedException() {
            super("Please call Presenter.attachView(MvpView) before" +
                    " requesting data to the Presenter");
        }
    }
}
