[6.1.0] refactor: TaskTabFragment和CurrentTaskFragment通过FlowBus解偶,避免通过TaxiFragment中转;
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
package com.mogo.och.common.module.utils
|
||||
|
||||
/**
|
||||
* @author aibingbing
|
||||
* @date: 2023/9/13
|
||||
* @desc Flow Bus
|
||||
*/
|
||||
import android.util.Log
|
||||
import androidx.lifecycle.*
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.channels.BufferOverflow
|
||||
import kotlinx.coroutines.flow.*
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
/**
|
||||
* FlowBus消息总线
|
||||
*/
|
||||
object FlowBus {
|
||||
private const val TAG = "FlowBus"
|
||||
private val busMap = mutableMapOf<String, EventBus<*>>()
|
||||
private val busStickMap = mutableMapOf<String, StickEventBus<*>>()
|
||||
|
||||
@Synchronized
|
||||
fun <T> with(key: String): EventBus<T> {
|
||||
var eventBus = busMap[key]
|
||||
if (eventBus == null) {
|
||||
eventBus = EventBus<T>(key)
|
||||
busMap[key] = eventBus
|
||||
}
|
||||
return eventBus as EventBus<T>
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun <T> withStick(key: String): StickEventBus<T> {
|
||||
var eventBus = busStickMap[key]
|
||||
if (eventBus == null) {
|
||||
eventBus = StickEventBus<T>(key)
|
||||
busStickMap[key] = eventBus
|
||||
}
|
||||
return eventBus as StickEventBus<T>
|
||||
}
|
||||
|
||||
//真正实现类
|
||||
open class EventBus<T>(private val key: String) : LifecycleObserver {
|
||||
|
||||
//私有对象用于发送消息
|
||||
private val _events: MutableSharedFlow<T> by lazy {
|
||||
obtainEvent()
|
||||
}
|
||||
|
||||
//暴露的公有对象用于接收消息
|
||||
private val events = _events.asSharedFlow()
|
||||
|
||||
open fun obtainEvent(): MutableSharedFlow<T> =
|
||||
MutableSharedFlow(0, 1, BufferOverflow.DROP_OLDEST)
|
||||
|
||||
//主线程接收数据
|
||||
fun register(lifecycleOwner: LifecycleOwner, action: (t: T) -> Unit) {
|
||||
lifecycleOwner.lifecycle.addObserver(this)
|
||||
lifecycleOwner.lifecycleScope.launch {
|
||||
events.collect {
|
||||
try {
|
||||
action(it)
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
Log.e(TAG, "FlowBus - Error:$e")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//协程中发送数据
|
||||
suspend fun post(event: T) {
|
||||
_events.emit(event)
|
||||
}
|
||||
|
||||
//主线程发送数据
|
||||
fun post(scope: CoroutineScope, event: T) {
|
||||
scope.launch {
|
||||
_events.emit(event)
|
||||
}
|
||||
}
|
||||
|
||||
//自动销毁
|
||||
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
|
||||
fun onDestroy() {
|
||||
Log.w(TAG, "FlowBus - 自动onDestroy")
|
||||
val subscriptCount = _events.subscriptionCount.value
|
||||
if (subscriptCount <= 0)
|
||||
busMap.remove(key)
|
||||
}
|
||||
}
|
||||
|
||||
class StickEventBus<T>(key: String) : EventBus<T>(key) {
|
||||
override fun obtainEvent(): MutableSharedFlow<T> =
|
||||
MutableSharedFlow(1, 1, BufferOverflow.DROP_OLDEST)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.mogo.och.taxi.constant
|
||||
|
||||
object TaxiDriverEventConst {
|
||||
val EVENT_TYPE_TAB_FRAGMENT_SHOW_RED_POINT = "event_type_tab_fragment_show_red_point"
|
||||
val EVENT_TYPE_TAB_FRAGMENT_TASK_WITH_ORDER_CHANGED = "event_type_tab_fragment_task_with_order_changed"
|
||||
}
|
||||
@@ -16,7 +16,6 @@ import com.mogo.eagle.core.utilcode.util.ToastUtils
|
||||
import com.mogo.och.common.module.biz.constant.OchCommonConst
|
||||
import com.mogo.och.common.module.biz.provider.LoginService
|
||||
import com.mogo.och.taxi.R
|
||||
import com.mogo.och.taxi.bean.QueryCurrentTaskRespBean
|
||||
import com.mogo.och.taxi.constant.TaxiOrderStatusEnum
|
||||
import com.mogo.och.taxi.network.TaxiDriverLoginImpl
|
||||
import com.mogo.och.taxi.ui.personal.TaxiPersonalDialogFragment
|
||||
@@ -145,11 +144,6 @@ class TaxiFragment : BaseTaxiTabFragment<TaxiFragment, TaxiPresenter>(),
|
||||
taskTabFragment!!.get()!!.onCarTakeOrderStatusChanged()
|
||||
}
|
||||
|
||||
fun updateNextTaskFragment(taskAndOrder: QueryCurrentTaskRespBean.Result?) {
|
||||
if (null == taskTabFragment || taskTabFragment!!.get() == null) return
|
||||
taskTabFragment!!.get()!!.onTaskDataChanged(taskAndOrder)
|
||||
}
|
||||
|
||||
fun switchVRFlatMode(isVRMode: Boolean) {
|
||||
if (mRootView != null) {
|
||||
mRootView.visibility = if (isVRMode) View.VISIBLE else View.GONE
|
||||
|
||||
@@ -27,6 +27,8 @@ import com.mogo.eagle.core.utilcode.util.UiThreadHandler
|
||||
import com.mogo.och.common.module.map.AmapNaviToDestinationModel
|
||||
import com.mogo.och.common.module.map.ICommonNaviChangedCallback
|
||||
import com.mogo.och.common.module.utils.DateTimeUtil
|
||||
import com.mogo.och.common.module.utils.FlowBus
|
||||
import com.mogo.och.common.module.voice.VoiceNotice
|
||||
import com.mogo.och.common.module.wigets.OCHCommitDialog
|
||||
import com.mogo.och.taxi.R
|
||||
import com.mogo.och.taxi.bean.OrderDetail
|
||||
@@ -34,6 +36,7 @@ import com.mogo.och.taxi.bean.QueryCurrentTaskRespBean
|
||||
import com.mogo.och.taxi.bean.StartServiceRespBean
|
||||
import com.mogo.och.taxi.constant.TaskStatusEnum
|
||||
import com.mogo.och.taxi.constant.TaskTypeEnum
|
||||
import com.mogo.och.taxi.constant.TaxiDriverEventConst
|
||||
import com.mogo.och.taxi.constant.TaxiOrderStatusEnum
|
||||
import com.mogo.och.taxi.constant.TaxiUnmannedConst.Companion.TAXI_END_MAP_MAKER
|
||||
import com.mogo.och.taxi.constant.TaxiUnmannedConst.Companion.TAXI_START_MAP_MAKER
|
||||
@@ -164,7 +167,7 @@ class TaxiCurrentTaskFragment : BaseFragment(),
|
||||
if (taskAndOrderUiState.driveToNearestStationTask != null) {
|
||||
updateViewByDriveToNearestStationTask(taskAndOrderUiState.driveToNearestStationTask)
|
||||
} else {
|
||||
updatePrepareTaskDelayUI(0,false)
|
||||
updatePrepareTaskDelayUI(0, false)
|
||||
initContainerView(false)
|
||||
removeAllMapMarker()
|
||||
}
|
||||
@@ -198,7 +201,10 @@ class TaxiCurrentTaskFragment : BaseFragment(),
|
||||
}
|
||||
|
||||
is TaskWithOrderUIState.UpdatePrepareTaskDelay -> {
|
||||
updatePrepareTaskDelayUI(taskAndOrderUiState.delayTime,taskAndOrderUiState.isStart)
|
||||
updatePrepareTaskDelayUI(
|
||||
taskAndOrderUiState.delayTime,
|
||||
taskAndOrderUiState.isStart
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -289,9 +295,21 @@ class TaxiCurrentTaskFragment : BaseFragment(),
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateNextTaskFragment(taskAndOrder: QueryCurrentTaskRespBean.Result?) {
|
||||
mTaxiFragment?.let {
|
||||
it.updateNextTaskFragment(taskAndOrder)
|
||||
private fun updateNextTaskFragment(result: QueryCurrentTaskRespBean.Result?) {
|
||||
FlowBus.with<QueryCurrentTaskRespBean.Result?>(TaxiDriverEventConst.EVENT_TYPE_TAB_FRAGMENT_TASK_WITH_ORDER_CHANGED)
|
||||
.post(this.lifecycleScope, result)
|
||||
|
||||
if (result != null
|
||||
&& result.taskType == TaskTypeEnum.VirtualTask.code
|
||||
&& result.order != null
|
||||
&& result.currentStatus != TaskStatusEnum.CompleteTask.code
|
||||
) {
|
||||
VoiceNotice.showNotice("已为您提前接到下一订单,待完成当前任务后服务")
|
||||
FlowBus.with<Boolean>(TaxiDriverEventConst.EVENT_TYPE_TAB_FRAGMENT_SHOW_RED_POINT)
|
||||
.post(this.lifecycleScope, true)
|
||||
} else {
|
||||
FlowBus.with<Boolean>(TaxiDriverEventConst.EVENT_TYPE_TAB_FRAGMENT_SHOW_RED_POINT)
|
||||
.post(this.lifecycleScope, false)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -392,7 +410,7 @@ class TaxiCurrentTaskFragment : BaseFragment(),
|
||||
taskTypeTv.text = resources.getString(R.string.task_exercise)
|
||||
startStationName.text = startSite.siteName
|
||||
endStationName.text = endSite.siteName
|
||||
updatePrepareTaskDelayUI(0,false)
|
||||
updatePrepareTaskDelayUI(0, false)
|
||||
updateStartAndEndStationPointByStatus(true)
|
||||
}
|
||||
|
||||
@@ -481,7 +499,7 @@ class TaxiCurrentTaskFragment : BaseFragment(),
|
||||
}
|
||||
|
||||
private fun updateOrderUI(order: OrderDetail) {
|
||||
updatePrepareTaskDelayUI(0,false)
|
||||
updatePrepareTaskDelayUI(0, false)
|
||||
taskTypeTv.text = resources.getString(R.string.task_order)
|
||||
taskTypeTv.background =
|
||||
ContextCompat.getDrawable(
|
||||
@@ -620,7 +638,7 @@ class TaxiCurrentTaskFragment : BaseFragment(),
|
||||
|
||||
override fun onDestroyView() {
|
||||
AmapNaviToDestinationModel.getInstance(context).destroyAmaNavi()
|
||||
updatePrepareTaskDelayUI(0,false)
|
||||
updatePrepareTaskDelayUI(0, false)
|
||||
super.onDestroyView()
|
||||
}
|
||||
|
||||
|
||||
@@ -15,11 +15,10 @@ import com.google.android.material.tabs.TabLayout
|
||||
import com.mogo.commons.mvp.BaseFragment
|
||||
import com.mogo.eagle.core.utilcode.mogo.logger.CallerLogger.d
|
||||
import com.mogo.eagle.core.utilcode.mogo.logger.scene.SceneConstant
|
||||
import com.mogo.och.common.module.voice.VoiceNotice
|
||||
import com.mogo.och.common.module.utils.FlowBus
|
||||
import com.mogo.och.taxi.R
|
||||
import com.mogo.och.taxi.bean.QueryCurrentTaskRespBean
|
||||
import com.mogo.och.taxi.constant.TaskStatusEnum
|
||||
import com.mogo.och.taxi.constant.TaskTypeEnum
|
||||
import com.mogo.och.taxi.constant.TaxiDriverEventConst
|
||||
import com.mogo.och.taxi.ui.base.TaxiFragment
|
||||
import kotlinx.android.synthetic.main.taxi_server_orders_panel.module_och_taxi_tab
|
||||
import kotlinx.android.synthetic.main.taxi_server_orders_panel.module_och_taxi_view_pager
|
||||
@@ -69,6 +68,7 @@ class TaxiTaskTabFragment : BaseFragment() {
|
||||
|
||||
override fun initViews() {
|
||||
initTaskTab()
|
||||
initEventBus()
|
||||
}
|
||||
|
||||
private fun initTaskTab() {
|
||||
@@ -183,25 +183,22 @@ class TaxiTaskTabFragment : BaseFragment() {
|
||||
}
|
||||
}
|
||||
|
||||
private fun initEventBus() {
|
||||
FlowBus.with<Boolean>(TaxiDriverEventConst.EVENT_TYPE_TAB_FRAGMENT_SHOW_RED_POINT)
|
||||
.register(this) { show ->
|
||||
wait_order_num.visibility = if (show) View.VISIBLE else View.GONE
|
||||
}
|
||||
FlowBus.with<QueryCurrentTaskRespBean.Result?>(TaxiDriverEventConst.EVENT_TYPE_TAB_FRAGMENT_TASK_WITH_ORDER_CHANGED)
|
||||
.register(this) { taskWithOrder ->
|
||||
nextTaskFragment?.onTaskDataChanged(taskWithOrder)
|
||||
}
|
||||
}
|
||||
|
||||
fun onNaviToEndStationByAMap(isShow: Boolean) {
|
||||
if (null == currentTaskFragment) return
|
||||
currentTaskFragment!!.onNaviToEndStationByAmap(isShow)
|
||||
currentTaskFragment?.onNaviToEndStationByAmap(isShow)
|
||||
}
|
||||
|
||||
fun onCarTakeOrderStatusChanged() {
|
||||
if (null == currentTaskFragment) return
|
||||
currentTaskFragment!!.onCarTakeOrderStatusChanged()
|
||||
}
|
||||
|
||||
fun onTaskDataChanged(result: QueryCurrentTaskRespBean.Result?) {
|
||||
if (null == nextTaskFragment) return
|
||||
if (result != null && result.taskType == TaskTypeEnum.VirtualTask.code
|
||||
&& result.order != null && result.currentStatus != TaskStatusEnum.CompleteTask.code) {
|
||||
VoiceNotice.showNotice("已为您提前接到下一订单,待完成当前任务后服务")
|
||||
wait_order_num.visibility = View.VISIBLE
|
||||
} else {
|
||||
wait_order_num.visibility = View.GONE
|
||||
}
|
||||
nextTaskFragment!!.onTaskDataChanged(result)
|
||||
currentTaskFragment?.onCarTakeOrderStatusChanged()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user