clean up mib notifications

This commit is contained in:
2026-06-10 01:23:46 +05:00
parent 71002ed70c
commit d0f46e2118
@@ -172,21 +172,29 @@ class NotificationsSheetFragment : BottomSheetDialogFragment() {
val mibClient = MibActivityHistoryClient() val mibClient = MibActivityHistoryClient()
mibSessions.forEach { (loginId, session) -> mibSessions.forEach { (loginId, session) ->
val cachedIds = allNotifications
.filter { it.bank == "MIB" && it.loginId == loginId }
.map { it.id }.toSet()
val result = withContext(Dispatchers.IO) { val result = withContext(Dispatchers.IO) {
mibClient.fetchUntilEnough(session, loginId) mibClient.fetchActivity(session, loginId, 1, 100)
} }
if (result.items.isNotEmpty() && isAdded) { if (isAdded) {
val readIds = NotificationsCache.getMibReadIds(requireContext()) val readIds = NotificationsCache.getMibReadIds(requireContext())
val resolved = result.items.map { it.copy(isRead = it.id in readIds) } val hasOverlap = cachedIds.isNotEmpty() && result.items.any { it.id in cachedIds }
allNotifications.removeAll { it.bank == "MIB" && it.loginId == loginId } val newItems = result.items
allNotifications.addAll(resolved) .filter { it.id !in cachedIds }
.map { it.copy(isRead = it.id in readIds) }
if (newItems.isNotEmpty()) {
allNotifications.addAll(newItems)
allNotifications.sortByDescending { it.timestampMs } allNotifications.sortByDescending { it.timestampMs }
mibNextStart[loginId] = result.nextStart val allForLogin = allNotifications.filter { it.bank == "MIB" && it.loginId == loginId }
mibDone[loginId] = result.nextStart > result.totalCount NotificationsCache.saveMib(requireContext(), loginId, allForLogin)
NotificationsCache.saveMib(requireContext(), loginId, result.items)
refreshAdapters() refreshAdapters()
broadcastUnread() broadcastUnread()
} }
mibNextStart[loginId] = result.nextStart
mibDone[loginId] = hasOverlap || result.nextStart > result.totalCount
}
} }
} }
} }
@@ -221,20 +229,29 @@ class NotificationsSheetFragment : BottomSheetDialogFragment() {
val mibClient = MibActivityHistoryClient() val mibClient = MibActivityHistoryClient()
mibSessions.forEach { (loginId, session) -> mibSessions.forEach { (loginId, session) ->
if (mibDone[loginId] == true) return@forEach if (mibDone[loginId] == true) return@forEach
val start = mibNextStart[loginId] ?: 1 while (mibDone[loginId] != true && isAdded) {
val start = mibNextStart[loginId] ?: 101
val cachedIds = allNotifications
.filter { it.bank == "MIB" && it.loginId == loginId }
.map { it.id }.toSet()
val result = withContext(Dispatchers.IO) { val result = withContext(Dispatchers.IO) {
mibClient.fetchActivity(session, loginId, start, start + 99) mibClient.fetchActivity(session, loginId, start, start + 99)
} }
if (result.items.isNotEmpty() && isAdded) { if (result.rawCount == 0) break
val readIds = NotificationsCache.getMibReadIds(requireContext()) val readIds = NotificationsCache.getMibReadIds(requireContext())
val resolved = result.items.map { it.copy(isRead = it.id in readIds) } val newItems = result.items
allNotifications.addAll(resolved.filter { n -> allNotifications.none { it.id == n.id } }) .filter { it.id !in cachedIds }
.map { it.copy(isRead = it.id in readIds) }
if (newItems.isNotEmpty()) {
allNotifications.addAll(newItems)
allNotifications.sortByDescending { it.timestampMs } allNotifications.sortByDescending { it.timestampMs }
mibNextStart[loginId] = result.nextStart
mibDone[loginId] = result.nextStart > result.totalCount
val allForLogin = allNotifications.filter { it.bank == "MIB" && it.loginId == loginId } val allForLogin = allNotifications.filter { it.bank == "MIB" && it.loginId == loginId }
NotificationsCache.saveMib(requireContext(), loginId, allForLogin) NotificationsCache.saveMib(requireContext(), loginId, allForLogin)
} }
mibNextStart[loginId] = result.nextStart
mibDone[loginId] = result.nextStart > result.totalCount
if (newItems.isNotEmpty()) break
}
} }
isLoadingMore = false isLoadingMore = false