From 5e5c44cacde5ddcaad8755626f888a43e9f3d37b Mon Sep 17 00:00:00 2001 From: dongliang Date: Wed, 29 Apr 2026 19:31:51 +0930 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=BA=A2=E7=82=B9=E6=94=B9=E4=B8=BA?= =?UTF-8?q?=E7=9B=B4=E6=8E=A5=E6=98=BE=E7=A4=BA=EF=BC=8C=E4=B8=8D=E4=BE=9D?= =?UTF-8?q?=E8=B5=96diffStats?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit diffStats方案因lastStats竞态反复失败。 改为:有pending通知→所有未ack的卡片直接显示红点。 点了哪个消哪个,简单可靠无竞态。 Co-Authored-By: Claude Opus 4.6 (1M context) --- .../com/xiaoqu/watch/ui/home/HomeFragment.kt | 69 ++++--------------- 1 file changed, 13 insertions(+), 56 deletions(-) diff --git a/app/src/main/java/com/xiaoqu/watch/ui/home/HomeFragment.kt b/app/src/main/java/com/xiaoqu/watch/ui/home/HomeFragment.kt index 36c6186..fb019f1 100644 --- a/app/src/main/java/com/xiaoqu/watch/ui/home/HomeFragment.kt +++ b/app/src/main/java/com/xiaoqu/watch/ui/home/HomeFragment.kt @@ -139,42 +139,17 @@ class HomeFragment : BaseFragment() { override fun onResume() { super.onResume() if (notificationManager.pendingCount <= 0) { - // 全部已查看/无通知 → 清除 activeDotCards.clear() renderDots() return } - - // 有未读通知 → 用 preNotificationStats 精确补充红点到 activeDotCards - val baseline = notificationManager.preNotificationStats - if (baseline != null && activeDotCards.isEmpty()) { - // 非首页返回:activeDotCards 为空(view 重建),需要重新计算 - viewLifecycleOwner.lifecycleScope.launch { - val result = safeApiCall { taskApi.getStatistics() } - if (result is ApiResult.Success && result.data != null) { - val data = result.data - val changed = notificationManager.diffStats(baseline, data) - val ack = notificationManager.acknowledgedCards - // 只加未查看的 - for (status in changed) { - if (status !in ack) activeDotCards.add(status) - } - // 记录增量 - if (2 in changed) notificationManager.recordCardIncrement(2, data.waitForTask - baseline.waitForTask) - if (3 in changed) notificationManager.recordCardIncrement(3, data.treatTask - baseline.treatTask) - if (4 in changed) notificationManager.recordCardIncrement(4, data.incompleteTask - baseline.incompleteTask) - // 更新数字 - tvPoolNum.text = data.waitForTask.toString() - tvPunchNum.text = data.treatTask.toString() - tvCompleteNum.text = data.incompleteTask.toString() - notificationManager.lastStats = data - renderDots() - } - } - } else { - // 首页返回(从卡片列表回来等):activeDotCards 已有值,直接渲染 - renderDots() - } + // 有未读通知 → 所有未 ack 的卡片显示红点 + val ack = notificationManager.acknowledgedCards + activeDotCards.clear() + if (2 !in ack) activeDotCards.add(2) + if (3 !in ack) activeDotCards.add(3) + if (4 !in ack) activeDotCards.add(4) + renderDots() } override fun onDestroyView() { @@ -387,30 +362,12 @@ class HomeFragment : BaseFragment() { if (result is ApiResult.Success && result.data != null) { val data = result.data - // 对比红点:累积到 activeDotCards,不覆盖已有的 - if (checkDots) { - val oldStats = notificationManager.lastStats - if (oldStats != null) { - // 有旧数据 → 精确对比 - val changedCards = notificationManager.diffStats(oldStats, data) - for (status in changedCards) { - activeDotCards.add(status) - val increment = when (status) { - 2 -> data.waitForTask - oldStats.waitForTask - 3 -> data.treatTask - oldStats.treatTask - 4 -> data.incompleteTask - oldStats.incompleteTask - else -> 0 - } - notificationManager.recordCardIncrement(status, increment) - } - } else { - // 无旧数据(首次加载 lastStats 还为 null)→ 有 pending 通知就显示所有有值的 - if (notificationManager.pendingCount > 0) { - if (data.waitForTask > 0) activeDotCards.add(2) - if (data.treatTask > 0) activeDotCards.add(3) - if (data.incompleteTask > 0) activeDotCards.add(4) - } - } + // 有 pending 通知 → 所有未 ack 的卡片显示红点(不用 diffStats,避免竞态) + if (checkDots && notificationManager.pendingCount > 0) { + val ack = notificationManager.acknowledgedCards + if (2 !in ack) activeDotCards.add(2) + if (3 !in ack) activeDotCards.add(3) + if (4 !in ack) activeDotCards.add(4) renderDots() }