diff --git a/OCH/mogo-och-common-module/src/main/java/com/mogo/och/common/module/utils/FlowBus.kt b/OCH/mogo-och-common-module/src/main/java/com/mogo/och/common/module/utils/FlowBus.kt new file mode 100644 index 0000000000..7bdc1dcc2a --- /dev/null +++ b/OCH/mogo-och-common-module/src/main/java/com/mogo/och/common/module/utils/FlowBus.kt @@ -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>() + private val busStickMap = mutableMapOf>() + + @Synchronized + fun with(key: String): EventBus { + var eventBus = busMap[key] + if (eventBus == null) { + eventBus = EventBus(key) + busMap[key] = eventBus + } + return eventBus as EventBus + } + + @Synchronized + fun withStick(key: String): StickEventBus { + var eventBus = busStickMap[key] + if (eventBus == null) { + eventBus = StickEventBus(key) + busStickMap[key] = eventBus + } + return eventBus as StickEventBus + } + + //真正实现类 + open class EventBus(private val key: String) : LifecycleObserver { + + //私有对象用于发送消息 + private val _events: MutableSharedFlow by lazy { + obtainEvent() + } + + //暴露的公有对象用于接收消息 + private val events = _events.asSharedFlow() + + open fun obtainEvent(): MutableSharedFlow = + 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(key: String) : EventBus(key) { + override fun obtainEvent(): MutableSharedFlow = + MutableSharedFlow(1, 1, BufferOverflow.DROP_OLDEST) + } +} diff --git a/OCH/taxi/unmanned-driver/src/main/java/com/mogo/och/taxi/constant/TaxiDriverEventConst.kt b/OCH/taxi/unmanned-driver/src/main/java/com/mogo/och/taxi/constant/TaxiDriverEventConst.kt new file mode 100644 index 0000000000..11e87f2668 --- /dev/null +++ b/OCH/taxi/unmanned-driver/src/main/java/com/mogo/och/taxi/constant/TaxiDriverEventConst.kt @@ -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" +} \ No newline at end of file diff --git a/OCH/taxi/unmanned-driver/src/main/java/com/mogo/och/taxi/ui/base/TaxiFragment.kt b/OCH/taxi/unmanned-driver/src/main/java/com/mogo/och/taxi/ui/base/TaxiFragment.kt index 0769a74235..2e1fc7ed65 100644 --- a/OCH/taxi/unmanned-driver/src/main/java/com/mogo/och/taxi/ui/base/TaxiFragment.kt +++ b/OCH/taxi/unmanned-driver/src/main/java/com/mogo/och/taxi/ui/base/TaxiFragment.kt @@ -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(), 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 diff --git a/OCH/taxi/unmanned-driver/src/main/java/com/mogo/och/taxi/ui/task/TaxiCurrentTaskFragment.kt b/OCH/taxi/unmanned-driver/src/main/java/com/mogo/och/taxi/ui/task/TaxiCurrentTaskFragment.kt index 9f91fde18b..e51c83c2bd 100644 --- a/OCH/taxi/unmanned-driver/src/main/java/com/mogo/och/taxi/ui/task/TaxiCurrentTaskFragment.kt +++ b/OCH/taxi/unmanned-driver/src/main/java/com/mogo/och/taxi/ui/task/TaxiCurrentTaskFragment.kt @@ -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(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(TaxiDriverEventConst.EVENT_TYPE_TAB_FRAGMENT_SHOW_RED_POINT) + .post(this.lifecycleScope, true) + } else { + FlowBus.with(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() } diff --git a/OCH/taxi/unmanned-driver/src/main/java/com/mogo/och/taxi/ui/task/TaxiTaskTabFragment.kt b/OCH/taxi/unmanned-driver/src/main/java/com/mogo/och/taxi/ui/task/TaxiTaskTabFragment.kt index 91955bf823..0f9e29c346 100644 --- a/OCH/taxi/unmanned-driver/src/main/java/com/mogo/och/taxi/ui/task/TaxiTaskTabFragment.kt +++ b/OCH/taxi/unmanned-driver/src/main/java/com/mogo/och/taxi/ui/task/TaxiTaskTabFragment.kt @@ -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(TaxiDriverEventConst.EVENT_TYPE_TAB_FRAGMENT_SHOW_RED_POINT) + .register(this) { show -> + wait_order_num.visibility = if (show) View.VISIBLE else View.GONE + } + FlowBus.with(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() } }