fix: 用通知前快照精确显示红点(区分首页/非首页场景)

问题:非首页返回时,onViewCreated已覆盖lastStats,
导致diffStats对比不出差异,红点要么全显要么不显。

修复:
1. NotificationManager新增preNotificationStats
   - 首条通知到达时保存当前lastStats快照
   - consumeAll时清除
   - 不会被onViewCreated的fetchStatistics覆盖

2. HomeFragment.onResume用preNotificationStats做精确diffStats
   - 有快照→fetchStatisticsForDots(baseline, acknowledged)
   - 只给变化且未查看的分类显示红点
   - 无快照→降级为全显(未查看的)

场景验证:
- 首页收到通知→当场diffStats→红点精确 
- 首页点红点→对应消除,其他保留 
- 首页点通知→consumeAll→返回后全部消除 
- 非首页返回→preNotificationStats对比→红点精确 

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
dongliang
2026-04-29 16:59:56 +09:30
parent a00d5abd51
commit 58ef336df4
2 changed files with 58 additions and 8 deletions

View File

@@ -66,6 +66,10 @@ class NotificationManager @Inject constructor(
/** 上次统计数据(对比红点用) */
var lastStats: TaskStatistics? = null
/** 通知到达前的统计快照(用于非首页返回后精确 diffStats */
var preNotificationStats: TaskStatistics? = null
private set
/** 协程作用域 */
private val scope = CoroutineScope(Dispatchers.Main + SupervisorJob())
@@ -111,6 +115,11 @@ class NotificationManager @Inject constructor(
val taskIds = parseTaskIds(rawJson)
if (taskIds.isEmpty()) return
// 保存通知到达前的统计快照(只在首次通知时保存,后续累积不覆盖)
if (preNotificationStats == null) {
preNotificationStats = lastStats?.copy()
}
addTaskIds(taskIds)
// 震动 + 亮屏(只在首条时触发,暂存的合并后不重复震动)
@@ -152,6 +161,7 @@ class NotificationManager @Inject constructor(
_pendingTaskIds.clear()
acknowledgedCards.clear()
_cardIncrements.clear()
preNotificationStats = null
Timber.d("通知: 已清空全部未读")
}