BaseMVP

作者: 啊了个支 | 来源:发表于2018-03-28 11:19 被阅读0次
    public class TUtils {
        public static <T> T getT(Object o, int i){
            try {
                return ((Class<T>)(((ParameterizedType)(o.getClass().getGenericSuperclass())).getActualTypeArguments()[i])).newInstance();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (ClassCastException e){
                e.printStackTrace();
            }
            return null;
        }
    }
    
    
    public abstract class BasePresenter<M,V> {
        private WeakReference<V> weakRefView;
    
    
        public M mModle;
    
        public void setVM(M m,V v){
            this.mModle=m;
            weakRefView = new WeakReference<V>(v);
        }
    
        public void detach() {
            if(isAttach()) {
                weakRefView.clear();
                weakRefView = null;
            }
        }
    
        public V getView(){
            return isAttach()?weakRefView.get():null;
        }
    
        protected boolean isAttach() {
            return weakRefView != null && weakRefView.get() != null;
        }
    }
    
    
    public abstract class BaseFragment<P extends BasePresenter, M extends BaseModle> extends Fragment {
        public P mPresenter;
        public M mModle;
        private View rootView;
        private Unbinder unbinder;
        private Context mConetxt;
    
        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            this.mConetxt=getActivity();
        }
    
    @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            if (rootView == null)
                rootView = inflater.inflate(getLayoutID(), container, false);
            unbinder =ButterKnife.bind(this,rootView);
            initPresenter();
            initView();
            return rootView;
        }
    
        /**
         * 通过Class跳转界面
         **/
        public void startActivity(Class<?> cls) {
            startActivity(cls, null);
        }
    
        /**
         * 含有Bundle通过Class跳转界面
         **/
        public void startActivity(Class<?> cls, Bundle bundle) {
            Intent intent = new Intent();
            intent.setClass(getActivity(), cls);
            if (bundle != null) {
                intent.putExtras(bundle);
            }
            startActivity(intent);
        }
    
    /**
         * 通过Class跳转界面
         **/
        public void startActivityForResult(Class<?> cls, int requestCode) {
            startActivityForResult(cls, null, requestCode);
        }
    
        /**
         * 含有Bundle通过Class跳转界面
         **/
        public void startActivityForResult(Class<?> cls, Bundle bundle,
                                           int requestCode) {
            Intent intent = new Intent();
            intent.setClass(getActivity(), cls);
            if (bundle != null) {
                intent.putExtras(bundle);
            }
            startActivityForResult(intent, requestCode);
        }
    
        @Override
        public void onDestroyView() {
            super.onDestroyView();
            if(mPresenter!=null)
                mPresenter.detach();
            unbinder.unbind();
        }
    
      protected abstract void initView();
    
        protected abstract void initPresenter();
    
        protected abstract int getLayoutID();
    }
    
    
    public abstract class BaseEventActivity<P extends BasePresenter, M extends BaseModle,E extends RxEvent> extends BaseActivity<P,M>{
        protected abstract void onEventReceive(E e);
    
        protected abstract void onEventError();
    
        protected abstract void register(Class<E> clazz);
    }
    
    
    
    public abstract class BaseActivity<P extends BasePresenter, M extends BaseModle> extends AppCompatActivity{
        //public RxManager mRxManage = new RxManager();
        @Inject
        public RxManager mRxManage;
    
        public P mPresenter;
        public M mModle;
        public Context mContext;
    
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(getLayoutID());
            ButterKnife.bind(this);
            mContext = this;
            mPresenter = TUtils.getT(this, 0);
            mModle = TUtils.getT(this, 1);
            injectRxManager(((IRx)getApplication()).getmRxcomponent());
            initPresenter();
            initView();
        }
    
     protected abstract void injectRxManager(RxBusComponent rxBusComponent);
    
        protected abstract void initView();
    
        protected abstract void initPresenter();
    
        protected abstract int getLayoutID();
    
        /**
         * 通过Class跳转界面
         **/
        public void startActivity(Class<?> cls) {
            startActivity(cls, null);
        }
    
        /**
         * 含有Bundle通过Class跳转界面
         **/
        public void startActivity(Class<?> cls, Bundle bundle) {
            Intent intent = new Intent();
            intent.setClass(this, cls);
            if (bundle != null) {
                intent.putExtras(bundle);
            }
            startActivity(intent);
        }
    
     /**
         * 通过Class跳转界面
         **/
        public void startActivityForResult(Class<?> cls, int requestCode) {
            startActivityForResult(cls, null, requestCode);
        }
    
        /**
         * 含有Bundle通过Class跳转界面
         **/
        public void startActivityForResult(Class<?> cls, Bundle bundle, int requestCode) {
            Intent intent = new Intent();
            intent.setClass(this, cls);
            if (bundle != null) {
                intent.putExtras(bundle);
            }
            startActivityForResult(intent, requestCode);
        }
    
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            if (mPresenter != null) {
                mPresenter.detach();
            }
            if(mRxManage!=null)
            mRxManage.unSubscribe(this);
        }
    
    }
    
    
    public abstract class RxAplication extends Application implements IRx{
        private RxBusComponent mRxcomponent;
    
        @Override
        public void onCreate() {
            super.onCreate();
            injectRxBus();
            onRxCreated();
        }
    
        protected abstract void onRxCreated();
    
    
        @Override
        public void injectRxBus() {
            mRxcomponent= DaggerRxBusComponent.builder().rxMoudle(new RxMoudle()).build();
        }
    
        @Override
        public RxBusComponent getmRxcomponent() {
            return mRxcomponent;
        }
    }
    
    
    
    
    

    相关文章

      网友评论

          本文标题:BaseMVP

          本文链接:https://www.haomeiwen.com/subject/pbjycftx.html