相关系列文章
模块化解耦框架RxFluxArchitecture1-框架简介
模块化解耦框架RxFluxArchitecture2-基本功能实现
模块化解耦框架RxFluxArchitecture3-订阅管理绑定生命周期
模块化解耦框架RxFluxArchitecture4-依赖库与依赖注入
模块化解耦框架RxFluxArchitecture5-Application多模块共存
框架中使用 Dagger.Android 实现依赖注入功能。
框架图
核心库
1、注解库core-arch-annotations
core-arch-annotations
是注解库,配合注解编译库core-arch-processor
使用,实现 Application 多模块共存。
2、数据总线库core-eventbus
core-eventbus
在 EventBus 基础上添加 Tag 功能,配合注解编译库core-eventbus-processor
使用,提高效率。
3、框架核心库core-arch
- 依赖
core-arch-annotations
、core-eventbus
。 - 依赖第三方框架Dagger、RxJava、Androidx.Core、Androidx.Appcompat、Androidx.Lifecycle。
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
管理RxAction
与io.reactivex.disposables.Disposable
对应关系,使用@Inject
标注构造方法注入。 -
RxStoreFactory
实现ViewModelProvider.Factory
,为 View 提供 Store,使用@Inject
标注构造方法注入。 -
RxActionCreator
所有 ActionCreator 的父类,封装RxDispatcher
和RxActionManager
,为子类提供公共方法。 -
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
封装自定义父类和自定义常用工具方法,添加通用依赖,使用时可按自己编程习惯重新编写。
- 依赖核心库
core-arch
,唯一不可变动,其余依赖都可替换。 - 依赖
core-image
、core-utils
、core-progress
、core-cookie
。 - 依赖第三方框架Arouter、ButterKnife、OkHttp、Retrofit、Logger、Gson、LeakCanary、BaseRecyclerViewAdapterHelper。
1、包base
-
BaseApp
继承RxApp
,全局使用的Application,初始化全局使用的方法工具。 -
BaseView
自定义 View 接口。 -
BaseActivity<T>
实现BaseView
,继承RxFluxActivity<T>
,使用ButterKnife,自定义全局响应RxLoading
、RxError
、RxRetry
。 -
BaseFragment<T>
实现BaseView
,继承RxFluxFragment<T>
,使用ButterKnife,自定义ToolBar
、Menu
。 -
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
使用OkHttp、Retrofit,依赖核心库core-arch
,完成下载进度提醒,使用@Inject
标注构造方法注入。
4、缓存Cookie库core-cookie
core-cookie
接口认证使用 Cookie 缓存,使用@Inject
标注构造方法注入。
壳模块
壳模块module-app
中SimpleApplication
继承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
,包含WanInjectActivityModule
和WanStoreModule
。
-
WanInjectActivityModule
通过注解@ContributesAndroidInjector
,自动生成 Activity 的依赖注入器。 -
WanStoreModule
提供 Store 对象 组成的Map<Class<? extends ViewModel>, Provider<ViewModel>>
,作为RxStoreFactory
的构造方法入参,Store 使用@Inject
标注构造方法注入。
每个 Activity 的注入器中添加WanInjectFragmentModule
,WanInjectFragmentModule
通过注解@ContributesAndroidInjector
,自动生成 Fragment 的依赖注入器。
@Module
public abstract class WanInjectActivityModule {
@ActivityScope
@ContributesAndroidInjector(modules = WanInjectFragmentModule.class)
abstract ArticleActivity injectArticleActivity();
@ActivityScope
@ContributesAndroidInjector(modules = WanInjectFragmentModule.class)
abstract LoginActivity injectLoginActivity();
}
依赖注入
1、Activity 与 Fragment 依赖注入器自动生成
2、Store 依赖注入实现
源码
开源模块化解耦框架RxFluxArchitecture,欢迎大家点赞Fork,更欢迎点评指导。