带你封装MVP架构(下)|青训营笔记(二)

简介: 在 Base 类中,我们需要做的就是把每个 Activity 或者 Fragment 等这些组件,或者对应的 MVP 层会用到的基本操作以及联系都编写好。

BaseActivity/BaseFragment

封装的时候,我们需要设置泛型,传入对应的 Presenter 以及对应的 ViewBinding;最后再继承 BaseView 这个接口

在页面创建和销毁的时候,我们需要对应的做相关的绑定和解绑,这其中除了对 Presenter 层进行绑定,还对 EventBus 做了对应的绑定。这是由于 EventBus 的订阅多会用于 UI 的更新,我们在这两个 Base 类做订阅的注册判断是有必要的;后续的子类想要使用,只需要在写上 @BindEventBus 的注解即可。

关于 EventBus 的封装,可以查看 EventBus封装到项目架构

public abstract class BaseActivity<P extends BasePresenter<? extends BaseView>,VB extends ViewBinding> extends AppCompatActivity implements BaseView{
    private VB binding;
    /**
     * presenter层的引用
     */
    protected P presenter;
    /**
     * 错误
     *
     * @param bean 错误信息
     */
    @Override
    public void onErrorCode(BaseBean bean) {
        ToastUtil.showToast(bean.msg);
    }
    /**
     * 初始化presenter,也是与Activity的绑定
     *
     * @return 返回new的Presenter层的值
     */
    protected abstract P createPresenter();
    /**
     * 载入view的一些操作
     */
    protected abstract void initView();
    /**
     * 载入数据操作
     */
    protected abstract void initData();
    /**
     * {@inheritDoc}
     * <p>
     * Perform initialization of all fragments.
     *
     * @param savedInstanceState
     */
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(this.getClass().isAnnotationPresent(BindEventBus.class)){
            EventBus.getDefault().register(this);
        }
        UltimateBarX.statusBarOnly(this)
                .light(true)
                .transparent()
                .apply();
        //强制使用竖屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        binding = ViewBindingUtil.inflateWithGeneric(this, getLayoutInflater());
        setContentView(binding.getRoot());
        presenter = createPresenter();
        initView();
        initData();
    }
    /**
     * 解除presenter与Activity的绑定
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(this.getClass().isAnnotationPresent(BindEventBus.class)){
            EventBus.getDefault().unregister(this);
        }
        if (presenter != null){
            presenter.detachView();
        }
    }
    @Override
    public void showLoading() {
        MyUtil.showLoading(this);
    }
    @Override
    public void SuccessHideLoading() {
        MyUtil.dismissSuccessLoading();
    }
    @Override
    public void FailedHideLoading() {
        MyUtil.dismissFailedLoading();
    }
    /**
     * 查看当前是否为深色模式
     *
     * @param context 传入当前context
     * @return 返回ture 偶然false
     */
    public Boolean getDarkModeStatus(Context context){
        int mode = context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
        return mode == Configuration.UI_MODE_NIGHT_YES;
    }
    protected VB getBinding() {
        return binding;
    }
}
复制代码


public abstract class BaseFragment<P extends BasePresenter,VB extends ViewBinding> extends Fragment implements BaseView {
    protected Context mContext;
    protected P presenter;
    private VB binding;
    /**
     * 显示加载中
     */
    @Override
    public void showLoading() {
        MyUtil.showLoading(mContext);
    }
    /**
     * 操作成功隐藏dialog和显示成功
     */
    @Override
    public void SuccessHideLoading() {
        MyUtil.dismissSuccessLoading();
    }
    /**
     * 操作失败隐藏dialog和显示失败
     */
    @Override
    public void FailedHideLoading() {
        MyUtil.dismissFailedLoading();
    }
    /**
     * 创建 presenter
     *
     * @return presenter
     */
    protected abstract P createPresenter();
    /**
     * 在这里要返回view的根路径
     *
     * @return 返回绑定的view
     */
    protected VB getBinding() {
        return binding;
    }
    /**
     * 初始化布局
     */
    protected abstract void initView();
    /**
     * 初始化数据
     */
    protected abstract void initData();
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if(this.getClass().isAnnotationPresent(BindEventBus.class)){
            EventBus.getDefault().register(this);
        }
        binding = ViewBindingUtil.inflateWithGeneric(this, inflater, container, false);
        //得到context,在后面的子类Fragment中都可以直接调用
        mContext = ActivityUtil.getCurrentActivity();
        presenter = createPresenter();
        initView();
        initData();
        return binding.getRoot();
    }
    @Override
    public void onResume() {
        super.onResume();
        initListener();
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if(this.getClass().isAnnotationPresent(BindEventBus.class)){
            EventBus.getDefault().unregister(this);
        }
        //销毁时,解除绑定
        if (presenter != null) {
            presenter.detachView();
        }
        binding = null;
    }
    private void initListener() {
    }
    @Override
    public void onErrorCode(BaseBean bean) {
    }
}
复制代码

至此,MVP 的封装就完成了

相关文章
|
2月前
|
监控 负载均衡 Dubbo
|
4月前
|
前端开发 JavaScript 数据库
Flask狼书笔记 | 09_图片社交网站 - 大型项目的架构与需求(2)
9.8 收藏图片 前面已经学习过如何使用关联表来表示多对多关系,缺点是只能表示关系,不能存储数据(如我还想记录下收藏图片的时间戳)。这种情况下,我们可以使用关联模型来表示多对多关系。 在关联模型中,我们将Photo模型与User模型的多对多关系,分离成了User模型和Collect模型的一对多关系,和Photo模型与Collect模型的一对多关系。
66 0
|
2月前
|
存储 传感器 网络协议
《物联网技术》课程笔记——第二章 物联网技术架构
《物联网技术》课程笔记——第二章 物联网技术架构
|
3月前
|
达摩院 Java Apache
惊动“达摩院”的分布式架构笔记:火于互联网,据说来自于清华
一个星期前,一本Java架构笔记突然在互联网上爆火。因为内容的深度和广度,甚至连阿里最牛的研发中心都被惊动了,而且作者一周后直接被阿里挖走后定级P8,据说作者来自于清华。
|
3月前
|
设计模式 存储 前端开发
【各种问题处理】MVC、MVP、MVVM 、MVI、VIPER 架构(设计模式)
【1月更文挑战第13天】【各种问题处理】MVC、MVP、MVVM 、MVI、VIPER 架构(设计模式)
|
3月前
|
安全 数据挖掘 定位技术
笔记 - 《业务架构解构与实践》
《业务架构解构与实践》的笔记
|
3月前
|
运维 Cloud Native 安全
笔记 - 《阿里云云原生架构实践》
《阿里云云原生架构实践》的笔记
|
3月前
|
NoSQL Java 程序员
阿里开发人员献礼“Java架构成长笔记”,深入内核,拒绝蒙圈
提起阿里,行外人联想到的关键词无非是“交易”、“淘宝”、“支付宝”,但对于程序员来说,阿里庞大的技术体系才是最吸引人的。实际上阿里作为国内一线互联网公司的头把交椅,内部的技术体系和发展都是备受关注的,对于程序员来说,能够进到阿里工作,就是对自己的技术水平进行一个提升和学习。
阿里开发人员献礼“Java架构成长笔记”,深入内核,拒绝蒙圈
|
3月前
|
消息中间件 架构师 Java
Java架构速成笔记:七大专题,1425页考点,挑战P8岗
我们都知道,在程序员的职业生涯中,有多个发展方向,不过就数据表明,近年来选择架构师方向的开发人员也越来越多。
|
3月前
|
SQL 存储 关系型数据库
华为大佬的“百万级”MySQL笔记,基础+优化+架构一键搞定
MySQL不用多说,大家都知道它是目前最为活跃热门的开源数据库,由于成本低,操作简易的特点,所以在互联网企业中被广泛使用,即使是头部的BATJ。由此可见,想要在互联网行业混得风生水起,或者说想要进入BATJ等一线互联网公司,那么熟练掌握MySQL必定是一块必要的敲门砖。