带你封装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月前
|
设计模式 存储 前端开发
MVVM、MVC、MVP三种常见软件架构设计模式的区别
MVC、MVP 和 MVVM 是三种常见的软件架构设计模式,主要通过分离关注点的方式来组织代码结构,优化开发效率。
74 12
|
3月前
|
设计模式 前端开发 Android开发
Android应用开发中的MVP架构模式解析
【5月更文挑战第25天】本文深入探讨了在Android应用开发中广泛采用的一种设计模式——Model-View-Presenter (MVP)。文章首先概述了MVP架构的基本概念和组件,接着分析了它与传统MVC模式的区别,并详细阐述了如何在实际开发中实现MVP架构。最后,通过一个具体案例,展示了MVP架构如何提高代码的可维护性和可测试性,以及它给开发者带来的其他潜在好处。
|
3月前
|
运维 Oracle 容灾
Oracle dataguard 容灾技术实战(笔记),教你一种更清晰的Linux运维架构
Oracle dataguard 容灾技术实战(笔记),教你一种更清晰的Linux运维架构
|
16天前
|
存储 负载均衡 架构师
架构笔记汇总
架构笔记汇总
50 1
|
1月前
|
存储 缓存 运维
Lustre架构介绍的阅读笔记-HSM
HSM(Hierarchical Storage Management)是数据分级存储管理,根据数据生命周期、访问特性和设备成本,自动在CPU寄存器、缓存、主存、SSD、HDD、光盘、磁带库等不同存储层级间迁移数据。数据热度分为热、温、冷、冰,对应不同成本、性能和容量。迁移策略可基于人工判断或系统自动计算,并确保业务I/O不受影响、数据一致性。访问频率增加时,数据可反向迁移至更高层级。
|
1月前
|
存储 消息中间件 缓存
Lustre架构介绍的阅读笔记-NFS兼容性
Lustre是分布式NFS系统,融合了分布式系统和NFS特性。它支持线性扩展容量和性能,提供POSIX语义,隐藏复杂存储细节。关键技术涉及分布式计算、缓存、锁、事务、通信(RPC、消息队列、同步/异步模式)、选举、任务调度、健康检查、负载均衡、集群管理和QoS。数据一致性、复制(副本、EC)、热点管理及多种上层协议(如NFS、S3)也是重点。分布式存储通过扩容提升读写带宽和IOPS。
|
2月前
|
Java API Android开发
技术经验分享:Android源码笔记——Camera系统架构
技术经验分享:Android源码笔记——Camera系统架构
31 0
|
2月前
|
移动开发 小程序 安全
基础入门-APP架构&小程序&H5+Vue语言&Web封装&原生开发&Flutter
基础入门-APP架构&小程序&H5+Vue语言&Web封装&原生开发&Flutter
|
3月前
|
安全 网络协议 网络安全
网络安全笔记整理,你花了多久弄明白架构设计
网络安全笔记整理,你花了多久弄明白架构设计
|
2天前
|
监控 负载均衡 API
从单体到微服务:架构转型之道
【8月更文挑战第17天】从单体架构到微服务架构的转型是一项复杂而系统的工程,需要综合考虑技术、团队、文化等多个方面的因素。通过合理的规划和实施策略,可以克服转型过程中的挑战,实现系统架构的升级和优化。微服务架构以其高度的模块化、可扩展性和灵活性,为业务的持续发展和创新提供了坚实的技术保障。