一个全新的 Android 组件化通信工具

GitHub
Gitee

ComponentBus 这个项目已经内部使用了一段时间, 经过几次迭代.

他非常小巧, 且功能强大, 并且配有 IDEA 插件作为辅助.

ComponentBus 利用 ASM、KSP, 使组件间的通信变得简单且高效.

第一步组件间通信

新建一个 Module, 我们给他添加一个接口

kotlin 复制代码
@Component(componentName = "Test")
object ComponentTest {

    @Action(actionName = "init")
    fun init(debug: Boolean) {
        ...
    }
	
	@Action(actionName = "getId")
    fun getId(): String {
        return "id-001"
    }

	@Action(actionName = "openUserPage", interceptorName = ["LoginInterceptor"])
	fun openUserPage() {
        val newIntent = Intent(MyApplication.application, UserActivity::class.java)
        newIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
        MyApplication.application.startActivity(newIntent)
    }
}

我们可以看到, 任何方法、参数、返回值都可作为通信 Action, 只要给他加上 Action 注解.

并且我们可以给他添加拦截器, 当条件不满足时进行拦截, 并做其他操作.

由于 module 间没有依赖, 返回值应该是所有 module 都可以引用到的类型.

组件间调用, 参数默认值目前不支持使用.

第二部调用其他组件API

新建一个 Module, 我们调用另一个 Module 的 API

kotlin 复制代码
ComponentBus.with("Test", "init")
	.params("debug", true)
	.callSync<Unit>()

val result = ComponentBus.with("Test", "getId")
	.callSync<String>()
if (result.isSuccess) {
	val id = result.data!!
}

就是这么简单, 不需要接口下沉.

这里有个问题, 那就是 componentName、actionName 都是字符串, 使用上不方便, 需要查看名称、复制.

为了解决这个问题, 我专门开发了一款 IDEA 插件, 辅助使用.

IDEA 插件

插件搜索 componentBus

拦截器

全局拦截器

kotlin 复制代码
/**  
 * 全局日志拦截器  
 */  
object LogGlobalInterceptor : GlobalInterceptor() {  
    override suspend fun <T> intercept(chain: Chain) = chain.proceed<T>().apply {  
        UtilsLog.log("Component: ${chain.request.componentName}${Utils.separatorLine}Action: ${chain.request.action}${Utils.separatorLine}Result: ($code) $msg $data", "Component")  
    }  
    override fun <T> interceptSync(chain: Chain) = chain.proceedSync<T>().apply {  
        UtilsLog.log("Component: ${chain.request.componentName}${Utils.separatorLine}Action: ${chain.request.action}${Utils.separatorLine}Result: ($code) $msg $data", "Component")  
    }  
}

普通拦截器

kotlin 复制代码
/**  
 * 判断是否是登录的拦截器  
 * 未登录会进入登录页面  
 */  
object LoginInterceptor : IInterceptor {  
    override suspend fun <T> intercept(chain: Chain): Result<T> {  
        return if (UsercenterComponent.isLoginLiveData.value == true) {  
            chain.proceed()  
        } else {  
            showLogin()  
            Result.resultError(-3, "拦截, 进入登录页")  
        }  
    }  
  
    override fun <T> interceptSync(chain: Chain): Result<T> {  
        return if (UsercenterComponent.isLoginLiveData.value == true) {  
            chain.proceedSync()  
        } else {  
            showLogin()  
            Result.resultError(-3, "拦截, 进入登录页")  
        }  
    }  
}

END

更多详情在 GitHub

欢迎感兴趣的朋友提供反馈和建议。

相关推荐
沐言人生2 小时前
Android10 Framework—Init进程-9.服务端属性值初始化
android·android studio·android jetpack
追光天使2 小时前
【Mac】和【安卓手机】 通过有线方式实现投屏
android·macos·智能手机·投屏·有线
小雨cc5566ru2 小时前
uniapp+Android智慧居家养老服务平台 0fjae微信小程序
android·微信小程序·uni-app
一切皆是定数3 小时前
Android车载——VehicleHal初始化(Android 11)
android·gitee
一切皆是定数3 小时前
Android车载——VehicleHal运行流程(Android 11)
android
problc3 小时前
Android 组件化利器:WMRouter 与 DRouter 的选择与实践
android·java
图王大胜4 小时前
Android SystemUI组件(11)SystemUIVisibility解读
android·framework·systemui·visibility
服装学院的IT男8 小时前
【Android 13源码分析】Activity生命周期之onCreate,onStart,onResume-2
android
Arms2068 小时前
android 全面屏最底部栏沉浸式
android
服装学院的IT男8 小时前
【Android 源码分析】Activity生命周期之onStop-1
android