Modify: 事件总线

This commit is contained in:
Quyunshuo
2020-08-29 14:57:03 +08:00
parent 0d68e2a7c9
commit b30ce8fdf0
12 changed files with 125 additions and 4 deletions

View File

@ -68,7 +68,9 @@ dependencies {
api GitHub.ARoute
api GitHub.RecyclerViewAdapter
api GitHub.StatusBar
api GitHub.EventBus
kapt GitHub.GlideCompiler
kapt GitHub.ARouteCompiler
kapt GitHub.EventBusAPT
}

View File

@ -6,6 +6,8 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.viewbinding.ViewBinding
import com.alibaba.android.arouter.launcher.ARouter
import com.quyunshuo.base.utils.EventBusRegister
import com.quyunshuo.base.utils.EventBusUtils
/**
* @Author: QuYunShuo
@ -22,14 +24,23 @@ abstract class BaseFrameActivity<VB : ViewBinding, VM : ViewModel>(private val v
protected val mBinding: VB by lazy(mode = LazyThreadSafetyMode.NONE) { initViewBinding() }
protected abstract fun initViewBinding(): VB
protected abstract fun initView()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(mBinding.root)
// ARouter 依赖注入
ARouter.getInstance().inject(this)
// 注册EventBus
if (javaClass.isAnnotationPresent(EventBusRegister::class.java)) EventBusUtils.register(this)
initView()
}
protected abstract fun initViewBinding(): VB
protected abstract fun initView()
override fun onDestroy() {
if (javaClass.isAnnotationPresent(EventBusRegister::class.java)) EventBusUtils.unRegister(
this
)
super.onDestroy()
}
}

View File

@ -9,6 +9,8 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.viewbinding.ViewBinding
import com.alibaba.android.arouter.launcher.ARouter
import com.quyunshuo.base.utils.EventBusRegister
import com.quyunshuo.base.utils.EventBusUtils
/**
* @Author: QuYunShuo
@ -41,6 +43,15 @@ abstract class BaseFrameFragment<VB : ViewBinding, VM : ViewModel>(private val v
super.onViewCreated(view, savedInstanceState)
// ARouter 依赖注入
ARouter.getInstance().inject(this)
// 注册EventBus
if (javaClass.isAnnotationPresent(EventBusRegister::class.java)) EventBusUtils.register(this)
initView()
}
override fun onDestroy() {
if (javaClass.isAnnotationPresent(EventBusRegister::class.java)) EventBusUtils.unRegister(
this
)
super.onDestroy()
}
}

View File

@ -0,0 +1,11 @@
package com.quyunshuo.base.utils
/**
* @Author: QuYunShuo
* @Time: 2020/8/29
* @Class: BindEventBus
* @Remark: 辅助注册EventBus注解
*/
@Target(AnnotationTarget.CLASS)
@kotlin.annotation.Retention(AnnotationRetention.RUNTIME)
annotation class EventBusRegister

View File

@ -0,0 +1,54 @@
package com.quyunshuo.base.utils
import org.greenrobot.eventbus.EventBus
/**
* @Author: QuYunShuo
* @Time: 2020/8/29
* @Class: EventBusUtil
* @Remark: EventBus工具类
*/
object EventBusUtils {
/**
* 订阅
* @param subscriber 订阅者
*/
fun register(subscriber: Any) = EventBus.getDefault().register(subscriber)
/**
* 解除注册
* @param subscriber 订阅者
*/
fun unRegister(subscriber: Any) = EventBus.getDefault().unregister(subscriber)
/**
* 发送普通事件
* @param event 事件
*/
fun postEvent(event: Any) = EventBus.getDefault().post(event)
/**
* 发送粘性事件
* @param stickyEvent 粘性事件
*/
fun postStickyEvent(stickyEvent: Any) = EventBus.getDefault().postSticky(stickyEvent)
/**
* 手动获取粘性事件
* @param stickyEventType 粘性事件
* @param <T> 事件泛型
* @return 返回给定事件类型的最近粘性事件
*/
fun <T> getStickyEvent(stickyEventType: Class<T>): T =
EventBus.getDefault().getStickyEvent(stickyEventType)
/**
* 手动删除粘性事件
* @param stickyEventType 粘性事件
* @param <T> 事件泛型
* @return 返回给定事件类型的最近粘性事件
*/
fun <T> removeStickyEvent(stickyEventType: Class<T>): T =
EventBus.getDefault().removeStickyEvent(stickyEventType)
}

View File

@ -12,6 +12,7 @@ import kotlinx.coroutines.flow.flowOn
/**
* 以顶层函数存在的常用工具方法
* startPolling() -> 开启一个轮询
* sendEvent() -> 发送普通EventBus事件
*/
/**
@ -32,4 +33,9 @@ suspend fun startPolling(intervals: Long, block: () -> Unit) {
.catch { Log.e("flow", "startPolling: $it") }
.flowOn(Dispatchers.Main)
.collect { block.invoke() }
}
}
/**
* 发送普通EventBus事件
*/
fun sendEvent(event: Any) = EventBusUtils.postEvent(event)

View File

@ -43,4 +43,5 @@ dependencies {
kapt GitHub.GlideCompiler
kapt GitHub.ARouteCompiler
kapt GitHub.EventBusAPT
}

View File

@ -3,10 +3,15 @@ package com.quyunshuo.main
import android.widget.Toast
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.quyunshuo.base.ktx.toast
import com.quyunshuo.base.utils.EventBusRegister
import com.quyunshuo.base.utils.sendEvent
import com.quyunshuo.common.bean.TestBean
import com.quyunshuo.common.constant.RouteKey
import com.quyunshuo.common.constant.RouteUrl
import com.quyunshuo.common.ui.BaseActivity
import com.quyunshuo.main.databinding.MainActivityMainBinding
import org.greenrobot.eventbus.Subscribe
/**
* @Author: QuYunShuo
@ -14,6 +19,7 @@ import com.quyunshuo.main.databinding.MainActivityMainBinding
* @Class: MainActivity
* @Remark: 主界面Activity
*/
@EventBusRegister
@Route(path = RouteUrl.MainActivity)
class MainActivity :
BaseActivity<MainActivityMainBinding, MainViewModel>(MainViewModel::class.java) {
@ -31,5 +37,11 @@ class MainActivity :
ARouter.getInstance().build(RouteUrl.MainActivity2)
.withString(RouteKey.KEY_NAME, "ARouter").navigation()
}
sendEvent(TestBean("EventBus"))
}
@Subscribe
fun onEvent(event: TestBean) {
toast(event.msgTest)
}
}

View File

@ -1,6 +1,7 @@
# Android项目框架(组件化 + Kotlin + MVVM + Jetpack )
note: 1. 测试打包脚本
2. 对比开源项目flow的封装
资源文件相关{
资源相关文件属于项目相关的因此需要放在Common组件内不要放在Base组件里

View File

@ -1,7 +1,9 @@
package com.quyunshuo.androidbaseframemvvm
import androidx.multidex.MultiDex
import com.quyunshuo.androidbaseframemvvm.eventbus.index.MainEventIndex
import com.quyunshuo.common.CommonApplication
import org.greenrobot.eventbus.EventBus
/**
* @Author: QuYunShuo
@ -13,6 +15,11 @@ class AppApplication : CommonApplication() {
override fun initialize() {
MultiDex.install(this)
// 开启EventBusAPT,优化反射效率
EventBus
.builder()
.addIndex(MainEventIndex())
.installDefaultEventBus()
super.initialize()
}
}

View File

@ -39,6 +39,7 @@ object Version {
const val ARouteCompiler = "1.2.2" // 阿里路由 APT
const val RecyclerViewAdapter = "3.0.4" // RecyclerViewAdapter
const val StatusBar = "1.5.1" // 状态栏
const val EventBus = "3.2.0"
}
object AndroidX {
@ -91,4 +92,6 @@ object GitHub {
const val RecyclerViewAdapter =
"com.github.CymChad:BaseRecyclerViewAdapterHelper:${Version.RecyclerViewAdapter}"
const val StatusBar = "com.jaeger.statusbarutil:library:${Version.StatusBar}"
const val EventBus = "org.greenrobot:eventbus:${Version.EventBus}"
const val EventBusAPT = "org.greenrobot:eventbus-annotation-processor:${Version.EventBus}"
}

View File

@ -63,7 +63,8 @@ android {
kapt {
arguments {
arg("AROUTER_MODULE_NAME", project.getName())
arg("AROUTER_MODULE_NAME", project.name)
arg("eventBusIndex", "${BuildConfig.applicationId}.eventbus.index.${project.name.replace('Lib_', '')}EventIndex")
}
}
@ -77,4 +78,5 @@ dependencies {
androidTestImplementation AndroidX.TestEspresso
kapt GitHub.GlideCompiler
kapt GitHub.ARouteCompiler
kapt GitHub.EventBusAPT
}