fix: 点击红点卡片后扣减通知数+不恢复已查看红点
1. NotificationManager新增: - acknowledgedCards: 记录已查看的分类 - cardIncrements: 记录每个分类的新增任务数 - acknowledgeCard(): 标记分类已查看 - pendingCount扣除已查看分类的增量 2. HomeFragment: - 点击卡片→acknowledgeCard→红点消失+提示条数量减少 - onResume只恢复未查看分类的红点 - pendingCount=0时清除所有提示 - fetchStatistics记录每个分类的增量用于扣减 3. consumeAll重置所有状态(acknowledgedCards+cardIncrements) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -41,8 +41,18 @@ class NotificationManager @Inject constructor(
|
|||||||
private val _pendingTaskIds = mutableListOf<String>()
|
private val _pendingTaskIds = mutableListOf<String>()
|
||||||
val pendingTaskIds: List<String> get() = _pendingTaskIds.toList()
|
val pendingTaskIds: List<String> get() = _pendingTaskIds.toList()
|
||||||
|
|
||||||
/** 未读任务数量 */
|
/** 未读任务数量(扣除已查看分类的增量) */
|
||||||
val pendingCount: Int get() = _pendingTaskIds.size
|
val pendingCount: Int get() = (_pendingTaskIds.size - acknowledgedCount).coerceAtLeast(0)
|
||||||
|
|
||||||
|
/** 已查看的卡片(status: 2=接单池, 3=待打卡, 4=待完成) */
|
||||||
|
val acknowledgedCards = mutableSetOf<Int>()
|
||||||
|
|
||||||
|
/** 每个分类的新增任务数(从 diffStats 记录) */
|
||||||
|
private val _cardIncrements = mutableMapOf<Int, Int>()
|
||||||
|
|
||||||
|
/** 已查看分类的任务总数 */
|
||||||
|
private val acknowledgedCount: Int
|
||||||
|
get() = acknowledgedCards.sumOf { _cardIncrements[it] ?: 0 }
|
||||||
|
|
||||||
/** 上次消息处理时间(去抖用) */
|
/** 上次消息处理时间(去抖用) */
|
||||||
private var lastMessageTime = 0L
|
private var lastMessageTime = 0L
|
||||||
@@ -137,16 +147,41 @@ class NotificationManager @Inject constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 消费所有未读消息(用户已查看列表) */
|
/** 消费所有未读消息(用户点击提示条/横幅查看全部) */
|
||||||
fun consumeAll() {
|
fun consumeAll() {
|
||||||
_pendingTaskIds.clear()
|
_pendingTaskIds.clear()
|
||||||
|
acknowledgedCards.clear()
|
||||||
|
_cardIncrements.clear()
|
||||||
Timber.d("通知: 已清空全部未读")
|
Timber.d("通知: 已清空全部未读")
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 按 ID 消费(任务完成时) */
|
/** 按 ID 消费(任务完成时) */
|
||||||
fun consumeByTaskId(taskId: String) {
|
fun consumeByTaskId(taskId: String) {
|
||||||
_pendingTaskIds.remove(taskId)
|
_pendingTaskIds.remove(taskId)
|
||||||
Timber.d("通知: 已消费任务 $taskId, 剩余 ${_pendingTaskIds.size}")
|
Timber.d("通知: 已消费任务 $taskId, 剩余 ${pendingCount}")
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 记录分类增量(从 diffStats 结果调用)
|
||||||
|
* @param status 卡片状态(2/3/4)
|
||||||
|
* @param increment 该分类新增任务数
|
||||||
|
*/
|
||||||
|
fun recordCardIncrement(status: Int, increment: Int) {
|
||||||
|
_cardIncrements[status] = increment
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 标记某个分类已查看(用户点击红点卡片)
|
||||||
|
* 不从 pendingTaskIds 中删除,而是通过 acknowledgedCount 扣减 pendingCount
|
||||||
|
*/
|
||||||
|
fun acknowledgeCard(status: Int) {
|
||||||
|
acknowledgedCards.add(status)
|
||||||
|
Timber.d("通知: 已查看分类 $status, 剩余未读 $pendingCount")
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 是否还有未查看的分类 */
|
||||||
|
fun hasUnacknowledgedCards(): Boolean {
|
||||||
|
return _cardIncrements.keys.any { it !in acknowledgedCards }
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -130,14 +130,19 @@ class HomeFragment : BaseFragment<FragmentHomeBinding>() {
|
|||||||
|
|
||||||
override fun onResume() {
|
override fun onResume() {
|
||||||
super.onResume()
|
super.onResume()
|
||||||
// 从其他页面返回时,恢复新任务提示
|
// 从其他页面返回时,只恢复未查看分类的红点
|
||||||
if (notificationManager.pendingCount > 0) {
|
if (notificationManager.pendingCount > 0) {
|
||||||
// 红点(不靠 diffStats,直接显示)
|
val ack = notificationManager.acknowledgedCards
|
||||||
dotPool.visibility = View.VISIBLE
|
if (2 !in ack) dotPool.visibility = View.VISIBLE
|
||||||
dotPunch.visibility = View.VISIBLE
|
if (3 !in ack) dotPunch.visibility = View.VISIBLE
|
||||||
dotComplete.visibility = View.VISIBLE
|
if (4 !in ack) dotComplete.visibility = View.VISIBLE
|
||||||
// 提示条
|
|
||||||
updateNewTaskHint()
|
updateNewTaskHint()
|
||||||
|
} else {
|
||||||
|
// 全部已查看 → 清除所有提示
|
||||||
|
dotPool.visibility = View.GONE
|
||||||
|
dotPunch.visibility = View.GONE
|
||||||
|
dotComplete.visibility = View.GONE
|
||||||
|
tvNewTaskHint.visibility = View.GONE
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -290,17 +295,17 @@ class HomeFragment : BaseFragment<FragmentHomeBinding>() {
|
|||||||
navigateToNewTasks()
|
navigateToNewTasks()
|
||||||
}
|
}
|
||||||
|
|
||||||
// 快捷区卡片点击 → 跳转任务列表 + 清除红点
|
// 快捷区卡片点击 → 标记已查看 + 清红点 + 更新提示条 + 跳转
|
||||||
page.findViewById<View>(R.id.cardPool)?.setOnClickListener {
|
page.findViewById<View>(R.id.cardPool)?.setOnClickListener {
|
||||||
dotPool.visibility = View.GONE
|
acknowledgeCard(2, dotPool)
|
||||||
navigateToTaskList(2)
|
navigateToTaskList(2)
|
||||||
}
|
}
|
||||||
page.findViewById<View>(R.id.cardPunch)?.setOnClickListener {
|
page.findViewById<View>(R.id.cardPunch)?.setOnClickListener {
|
||||||
dotPunch.visibility = View.GONE
|
acknowledgeCard(3, dotPunch)
|
||||||
navigateToTaskList(3)
|
navigateToTaskList(3)
|
||||||
}
|
}
|
||||||
page.findViewById<View>(R.id.cardComplete)?.setOnClickListener {
|
page.findViewById<View>(R.id.cardComplete)?.setOnClickListener {
|
||||||
dotComplete.visibility = View.GONE
|
acknowledgeCard(4, dotComplete)
|
||||||
navigateToTaskList(4)
|
navigateToTaskList(4)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -341,12 +346,22 @@ class HomeFragment : BaseFragment<FragmentHomeBinding>() {
|
|||||||
if (result is ApiResult.Success && result.data != null) {
|
if (result is ApiResult.Success && result.data != null) {
|
||||||
val data = result.data
|
val data = result.data
|
||||||
|
|
||||||
// 对比红点
|
// 对比红点 + 记录每个分类的增量
|
||||||
if (checkDots) {
|
if (checkDots) {
|
||||||
val changedCards = notificationManager.diffStats(notificationManager.lastStats, data)
|
val oldStats = notificationManager.lastStats
|
||||||
if (2 in changedCards) dotPool.visibility = View.VISIBLE
|
val changedCards = notificationManager.diffStats(oldStats, data)
|
||||||
if (3 in changedCards) dotPunch.visibility = View.VISIBLE
|
if (2 in changedCards) {
|
||||||
if (4 in changedCards) dotComplete.visibility = View.VISIBLE
|
dotPool.visibility = View.VISIBLE
|
||||||
|
notificationManager.recordCardIncrement(2, data.waitForTask - (oldStats?.waitForTask ?: 0))
|
||||||
|
}
|
||||||
|
if (3 in changedCards) {
|
||||||
|
dotPunch.visibility = View.VISIBLE
|
||||||
|
notificationManager.recordCardIncrement(3, data.treatTask - (oldStats?.treatTask ?: 0))
|
||||||
|
}
|
||||||
|
if (4 in changedCards) {
|
||||||
|
dotComplete.visibility = View.VISIBLE
|
||||||
|
notificationManager.recordCardIncrement(4, data.incompleteTask - (oldStats?.incompleteTask ?: 0))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 更新数字
|
// 更新数字
|
||||||
@@ -535,6 +550,13 @@ class HomeFragment : BaseFragment<FragmentHomeBinding>() {
|
|||||||
findNavController().navigate(R.id.action_home_to_taskList, bundle)
|
findNavController().navigate(R.id.action_home_to_taskList, bundle)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 标记某个分类已查看:清红点 + 更新通知数 + 刷新提示条 */
|
||||||
|
private fun acknowledgeCard(status: Int, dot: View) {
|
||||||
|
dot.visibility = View.GONE
|
||||||
|
notificationManager.acknowledgeCard(status)
|
||||||
|
updateNewTaskHint()
|
||||||
|
}
|
||||||
|
|
||||||
/** 更新新任务提示条 */
|
/** 更新新任务提示条 */
|
||||||
private fun updateNewTaskHint() {
|
private fun updateNewTaskHint() {
|
||||||
val count = notificationManager.pendingCount
|
val count = notificationManager.pendingCount
|
||||||
|
|||||||
Reference in New Issue
Block a user