fix: 红点不显示——SharedFlow(replay=0)事件竞争

根因:EventBus用MutableSharedFlow(replay=0,buffer=0),
MainActivity和HomeFragment同时collect,NewTaskArrived
中间事件在emit时挂起(无缓冲),HomeFragment收不到。

修复:去掉NewTaskArrived中间事件,HomeFragment直接在
MqttMessageReceived type=1中处理:
1. 调 notificationManager.onNewTaskMessage()
2. 直接调 activity.showNotificationBanner() 显示横幅
3. fetchStatistics(checkDots=true) 刷新红点

架构简化:
- MainActivity不再监听MQTT type=1(去掉observeMqttMessages)
- NotificationManager不再emit事件(去掉EventBus依赖)
- 去抖合并后通过回调onPendingCountChanged通知UI

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
dongliang
2026-04-29 18:48:05 +09:30
parent 73af1cbfa9
commit 0f6385a9fe
3 changed files with 36 additions and 60 deletions

View File

@@ -4,8 +4,6 @@ import com.xiaoqu.watch.data.task.TaskStatistics
import com.xiaoqu.watch.device.screen.ScreenController
import com.xiaoqu.watch.device.sensor.VibrationController
import com.xiaoqu.watch.device.sensor.VibrationDefaults
import com.xiaoqu.watch.event.AppEvent
import com.xiaoqu.watch.event.EventBus
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
@@ -27,8 +25,7 @@ import javax.inject.Singleton
@Singleton
class NotificationManager @Inject constructor(
private val vibrationController: VibrationController,
private val screenController: ScreenController,
private val eventBus: EventBus
private val screenController: ScreenController
) {
companion object {
/** 去抖间隔(毫秒) */
@@ -73,6 +70,9 @@ class NotificationManager @Inject constructor(
/** 协程作用域 */
private val scope = CoroutineScope(Dispatchers.Main + SupervisorJob())
/** 去抖合并后的回调(通知 UI 更新横幅数字) */
var onPendingCountChanged: ((Int) -> Unit)? = null
/**
* 处理 MQTT type=1 新任务消息
* 去抖策略1s 窗口内的消息暂存,窗口结束后合并处理(不丢弃)
@@ -104,8 +104,8 @@ class NotificationManager @Inject constructor(
for (json in jsons) {
processMessageSilent(json) // 只加 ID不重复震动
}
// 合并后统一发一次事件
notifyUi()
// 合并后通知 UI 更新数字
onPendingCountChanged?.invoke(pendingCount)
}
}
}
@@ -128,8 +128,7 @@ class NotificationManager @Inject constructor(
vibrationController.executePattern(pattern)
}
screenController.turnOn()
notifyUi()
// UI 更新(横幅+红点)由调用方处理,不再通过 EventBus
}
/** 静默处理(只加 ID不震动不亮屏用于合并暂存消息 */
@@ -149,12 +148,7 @@ class NotificationManager @Inject constructor(
Timber.d("通知: 当前未读 ${_pendingTaskIds.size}")
}
/** 通知 UI 更新(横幅+红点) */
private fun notifyUi() {
scope.launch {
eventBus.emit(AppEvent.NewTaskArrived(_pendingTaskIds.toList(), _pendingTaskIds.size))
}
}
// notifyUi 已移除UI 更新改由调用方HomeFragment直接处理
/** 消费所有未读消息(用户点击提示条/横幅查看全部) */
fun consumeAll() {