模块化解耦框架RxFluxArchitecture4-依赖库与依赖注入

栏目: Android · 发布时间: 6年前

core-arch-annotations 是注解库,配合注解编译库 core-arch-processor 使用,实现 Application 多模块共存。

2. 数据总线 core-eventbus

core-eventbusEventBus 基础上添加 Tag 功能,配合注解编译库 core-eventbus-processor 使用,提高效率。

3. 框架核心 core-arch

3.1 核心接口

  • RxSubscriberView 实现该接口的 View, RxFlux 根据其生命周期自动注册订阅、取消订阅,接收 core-eventbus 发送的通知。
  • RxFluxView<T extends ViewModel> 实现该接口的 View,可获取对应的 Store 并关联 View 的生命周期。
  • RxAppLifecycle Application 生命周期代理接口。

3.2 核心类

  • RxApp 继承 DaggerApplication ,为使用 @ContributesAndroidInjector 注解的 Activity 自动生成依赖注入器。使用反射获取编译生成类 RxAppLifecycleImpl ,实现 Application 多模块共存。
  • RxFlux 管理 View 生命周期,关联 View 与 Store,使用 @Inject 标注构造方法注入。
  • RxDispatcher 使用 core-eventbus 对 View 和 Store 注册订阅、发送通知、取消订阅,使用 @Inject 标注构造方法注入。
  • RxActionManager 管理 RxActionio.reactivex.disposables.Disposable 对应关系,使用 @Inject 标注构造方法注入。
  • RxStoreFactory 实现 ViewModelProvider.Factory ,为 View 提供 Store,使用 @Inject 标注构造方法注入。
  • RxActionCreator 所有 ActionCreator 的父类,封装 RxDispatcherRxActionManager ,为子类提供公共方法。
  • RxFluxModule 全局依赖注入仓库,提供 ViewModelProvider.Factory 的实现类 RxStoreFactory 实例对象,提供 Context 的子类 Application 实例对象。

3.3 核心View

  • RxFluxActivity<T>RxFluxFragment<T>RxFluxDialog<T> 实现 RxFluxView<T>RxSubscriberView 接口方法,是所有 View 的父类。
  • RxFluxActivity<T> 实现 dagger.android.supportHasSupportFragmentInjector 接口,为使用 @ContributesAndroidInjector 注解的 Fragment 自动生成依赖注入器。

通用库

core-common 封装自定义父类和自定义常用 工具 方法,添加通用依赖,使用时可按自己编程习惯重新编写。

1. 包 base

  • BaseApp 继承 RxApp ,全局使用的Application,初始化全局使用的方法工具。
  • BaseView 自定义 View 接口。
  • BaseActivity<T> 实现 BaseView ,继承 RxFluxActivity<T> ,使用 ButterKnife ,自定义全局响应 RxLoadingRxErrorRxRetry
  • BaseFragment<T> 实现 BaseView ,继承 RxFluxFragment<T> ,使用 ButterKnife ,自定义 ToolBarMenu
  • BaseDialog<T> 实现 BaseView ,继承 RxFluxDialog<T> ,使用 ButterKnife

2. 包 common

  • CommonActionCreator 可全局使用的 ActionCreator,使用 @Inject 标注构造方法注入。
  • CommonLoadingDialog 可全局使用的进度弹框,使用 @Inject 标注构造方法注入。
  • CommonHttpInterceptor 可全局使用的 OkHttp 拦截器,使用 @Inject 标注构造方法注入。
  • CommonException 自定义异常。
  • CommonModule 通用全局依赖注入仓库,依赖 RxFluxModule
  • CommonUtils 常用自定义工具。
  • CommonLoadMoreView 自定义 BaseRecyclerViewAdapterHelper 加载样式。

功能库

1. 图片解析 core-image

core-image 封装 Glide ,解析图片。

2. 常用工具 core-utils

  • ActivityUtils 自定义 Activity 中常用方法。
  • LocalStorageUtils 封装 Fastjson 的工具类,使用 @Inject 标注构造方法注入。

3. 下载进度提醒 core-progress

core-progress 使用 OkHttpRetrofit ,依赖核心库 core-arch ,完成下载进度提醒,使用 @Inject 标注构造方法注入。

4. 缓存Cookie core-cookie

core-cookie 接口认证使用 Cookie 缓存,使用 @Inject 标注构造方法注入。

壳模块

壳模块 module-appSimpleApplication 继承 BaseApp ,使用依赖注入器 SimpleComponent ,实现依赖注入。

@RxAppBody
public class SimpleApplication extends BaseApp {
    @Override
    protected AndroidInjector<? extends DaggerApplication> applicationInjector() {
        return DaggerSimpleComponent.builder().application(this).build();
    }
}
复制代码

SimpleComponent 中添加业务模块依赖注入仓库、通用全局依赖注入仓库 CommonModule 、Dagger.Android支持仓库 AndroidSupportInjectionModule

@Singleton
@Component(modules = {
        GanAppModule.class,
        WanAppModule.class,
        com.huyingbao.module.wan.kotlin.module.WanAppModule.class,
        CommonModule.class,
        AndroidSupportInjectionModule.class})
public interface SimpleComponent extends AndroidInjector<SimpleApplication> {
    @Component.Builder
    interface Builder {
        @BindsInstance
        SimpleComponent.Builder application(Application application);

        SimpleComponent build();
    }
}
复制代码

业务模块

业务模块 module-wan 依赖注入仓库 WanAppModule ,包含 WanInjectActivityModuleWanStoreModule

  • WanInjectActivityModule 通过注解 @ContributesAndroidInjector ,自动生成 Activity 的依赖注入器。
  • WanStoreModule 提供 Store 对象仓库,Store 使用 @Inject 标注构造方法注入。

每个 Activity 的注入器中添加 WanInjectFragmentModuleWanInjectFragmentModule 通过注解 @ContributesAndroidInjector ,自动生成 Fragment 的依赖注入器。

@Module
public abstract class WanInjectActivityModule {
    @ActivityScope
    @ContributesAndroidInjector(modules = WanInjectFragmentModule.class)
    abstract ArticleActivity injectArticleActivity();

    @ActivityScope
    @ContributesAndroidInjector(modules = WanInjectFragmentModule.class)
    abstract LoginActivity injectLoginActivity();
}
复制代码

源码

开源模块化解耦框架 RxFluxArchitecture ,欢迎大家点赞Fork,更欢迎点评指导。


以上所述就是小编给大家介绍的《模块化解耦框架RxFluxArchitecture4-依赖库与依赖注入》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Mechanics of Web Handling

The Mechanics of Web Handling

David R. Roisum

This unique book covers many aspects of web handling for manufacturing, converting, and printing. The book is applicable to any web including paper, film, foil, nonwovens, and textiles. The Mech......一起来看看 《The Mechanics of Web Handling》 这本书的介绍吧!

SHA 加密
SHA 加密

SHA 加密工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具