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

@@ -7,16 +7,9 @@ import android.view.View
import androidx.activity.OnBackPressedCallback
import androidx.appcompat.app.AppCompatActivity
import com.xiaoqu.watch.databinding.ActivityMainBinding
import com.xiaoqu.watch.event.AppEvent
import com.xiaoqu.watch.event.EventBus
import com.xiaoqu.watch.service.manager.NotificationManager
import com.xiaoqu.watch.service.manager.SystemStateMonitor
import com.xiaoqu.watch.ui.widget.NotificationBannerView
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch
import timber.log.Timber
import javax.inject.Inject
import kotlin.math.abs
@@ -32,17 +25,9 @@ class MainActivity : AppCompatActivity() {
/** 系统状态监听器(电量、蓝牙状态) */
@Inject lateinit var systemStateMonitor: SystemStateMonitor
/** 消息通知管理器 */
@Inject lateinit var notificationManager: NotificationManager
/** 事件总线 */
@Inject lateinit var eventBus: EventBus
/** 通知横幅HomeFragment 需访问设置点击回调) */
/** 通知横幅HomeFragment 需访问) */
lateinit var notificationBanner: NotificationBannerView
/** Activity 协程作用域 */
private val activityScope = CoroutineScope(Dispatchers.Main + SupervisorJob())
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@@ -68,9 +53,6 @@ class MainActivity : AppCompatActivity() {
// 初始化通知横幅
notificationBanner = binding.notificationBanner
// 监听 MQTT 新任务消息,显示横幅
observeMqttMessages()
Timber.d("MainActivity created")
}
@@ -80,27 +62,12 @@ class MainActivity : AppCompatActivity() {
notificationBanner.destroy()
}
// ===== MQTT 新任务 → 通知横幅 =====
// ===== 通知横幅(由 HomeFragment 调用显示) =====
/** 监听 MQTT 消息和通知事件 */
private fun observeMqttMessages() {
activityScope.launch {
eventBus.events.collect { event ->
when (event) {
is AppEvent.MqttMessageReceived -> {
if (event.type == 1) {
// 交给 NotificationManager 处理(去抖+合并+震动+亮屏)
notificationManager.onNewTaskMessage(event.rawJson)
}
}
// NotificationManager 处理完后发出事件 → 显示/更新横幅
is AppEvent.NewTaskArrived -> {
notificationBanner.show(event.count)
}
else -> {}
}
}
}
/** 显示通知横幅(供 HomeFragment 调用) */
fun showNotificationBanner(count: Int) {
notificationBanner.show(count)
}
}
// ===== 下拉手势检测Activity 级别,不可能被子 View 拦截) =====