Modify: 事件总线
This commit is contained in:
@ -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
|
||||
}
|
||||
@ -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()
|
||||
}
|
||||
}
|
||||
@ -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()
|
||||
}
|
||||
}
|
||||
@ -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
|
||||
@ -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)
|
||||
}
|
||||
@ -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)
|
||||
Reference in New Issue
Block a user