refactor: 优化 RegisterEventBus.kt

This commit is contained in:
Quyunshuo
2023-03-12 20:11:28 +08:00
parent 03b20dd7cf
commit f8ee74e045
4 changed files with 78 additions and 26 deletions

View File

@ -28,14 +28,22 @@ abstract class BaseFrameActivity<VB : ViewBinding, VM : BaseViewModel> : AppComp
protected abstract val mViewModel: VM
/**
* 是否有 [RegisterEventBus] 注解,避免重复调用 [Class.isAnnotation]
*/
private var mHaveRegisterEventBus = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(mBinding.root)
// ARouter 依赖注入
ARouter.getInstance().inject(this)
// TODO: EventBus 注册与解除注册逻辑优化
// 注册EventBus
if (javaClass.isAnnotationPresent(EventBusRegister::class.java)) EventBusUtils.register(this)
// 根据子类是否有 RegisterEventBus 注解決定是否进行注册 EventBus
if (javaClass.isAnnotationPresent(RegisterEventBus::class.java)) {
mHaveRegisterEventBus = true
EventBusUtils.register(this)
}
setStatusBar()
mBinding.initView()
@ -76,10 +84,10 @@ abstract class BaseFrameActivity<VB : ViewBinding, VM : BaseViewModel> : AppComp
}
override fun onDestroy() {
// TODO: EventBus 注册与解除注册逻辑优化
if (javaClass.isAnnotationPresent(EventBusRegister::class.java)) EventBusUtils.unRegister(
this
)
// 根据子类是否有 RegisterEventBus 注解决定是否进行注册 EventBus
if (mHaveRegisterEventBus) {
EventBusUtils.unRegister(this)
}
super.onDestroy()
}

View File

@ -9,7 +9,7 @@ import androidx.viewbinding.ViewBinding
import com.alibaba.android.arouter.launcher.ARouter
import com.quyunshuo.androidbaseframemvvm.base.mvvm.vm.BaseViewModel
import com.quyunshuo.androidbaseframemvvm.base.utils.BindingReflex
import com.quyunshuo.androidbaseframemvvm.base.utils.EventBusRegister
import com.quyunshuo.androidbaseframemvvm.base.utils.RegisterEventBus
import com.quyunshuo.androidbaseframemvvm.base.utils.EventBusUtils
/**
@ -30,6 +30,11 @@ abstract class BaseFrameFragment<VB : ViewBinding, VM : BaseViewModel> : Fragmen
protected abstract val mViewModel: VM
/**
* 是否有 [RegisterEventBus] 注解,避免重复调用 [Class.isAnnotation]
*/
private var mHaveRegisterEventBus = false
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
@ -43,10 +48,12 @@ abstract class BaseFrameFragment<VB : ViewBinding, VM : BaseViewModel> : Fragmen
super.onViewCreated(view, savedInstanceState)
// ARouter 依赖注入
ARouter.getInstance().inject(this)
// TODO: EventBus 注册与解除注册逻辑优化
// 注册EventBus
if (javaClass.isAnnotationPresent(EventBusRegister::class.java)) EventBusUtils.register(this)
// 根据子类是否有 RegisterEventBus 注解決定是否进行注册 EventBus
if (javaClass.isAnnotationPresent(RegisterEventBus::class.java)) {
mHaveRegisterEventBus = true
EventBusUtils.register(this)
}
_binding?.initView()
initObserve()
initRequestData()
@ -58,10 +65,10 @@ abstract class BaseFrameFragment<VB : ViewBinding, VM : BaseViewModel> : Fragmen
}
override fun onDestroy() {
// TODO: EventBus 注册与解除注册逻辑优化
if (javaClass.isAnnotationPresent(EventBusRegister::class.java)) EventBusUtils.unRegister(
this
)
// 根据子类是否有 RegisterEventBus 注解决定是否进行注册 EventBus
if (mHaveRegisterEventBus) {
EventBusUtils.unRegister(this)
}
super.onDestroy()
}
}

View File

@ -1,11 +0,0 @@
package com.quyunshuo.androidbaseframemvvm.base.utils
// TODO: 注解重命名
/**
* @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,48 @@
package com.quyunshuo.androidbaseframemvvm.base.utils
/**
* 辅助注册 EventBus 的注解
*
* - **使用方式:**
* 在基类中的 `onCreate()`、`onDestroy()` 生命周期回调中去判断当前 Class 对象是否使用了该注解,
* 然后根据结果去注册或反注册
*
* - **为什么不统一注册:**
* 统一注册会在 EventBus 内部集合中留存,每次发送事件时,会遍历集合,过多无用的注册会导致速度变慢,
* 所以最好的方式就是根据需要进行注册,避免无意义的全部注册
*
* - **sample:**
* ```
* abstract class BaseActivity : AppCompatActivity() {
*
* // 是否有 [RegisterEventBus] 注解 避免重复调用 [Class.isAnnotation]
* private var mHaveRegisterEventBus = false
* override fun onCreate(savedInstanceState: Bundle?) {
* super.onCreate(savedInstanceState)
* // 根据子类是否有 RegisterEventBus 注解決定是否进行注册 EventBus
* if (javaClass.isAnnotationPresent(RegisterEventBus::class.java)) {
* mHaveRegisterEventBus = true
* EventBusUtils.register(this)
* }
* }
*
* override fun onDestroy() {
* // 根据子类是否有 RegisterEventBus 注解决定是否进行注册 EventBus
* if (mHaveRegisterEventBus) {
* EventBusUtils.unRegister(this)
* }
* super.onDestroy()
* }
* }
*
* // 子类:
* @RegisterEventBus
* class SampleActivity : BaseActivity()
* ```
*
* @author Qu Yunshuo
* @since 2020/8/29
*/
@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
annotation class RegisterEventBus