forked from shihaam/thijooree
clean up mib notifications
This commit is contained in:
@@ -172,20 +172,28 @@ 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 }
|
||||||
allNotifications.sortByDescending { it.timestampMs }
|
.map { it.copy(isRead = it.id in readIds) }
|
||||||
|
if (newItems.isNotEmpty()) {
|
||||||
|
allNotifications.addAll(newItems)
|
||||||
|
allNotifications.sortByDescending { it.timestampMs }
|
||||||
|
val allForLogin = allNotifications.filter { it.bank == "MIB" && it.loginId == loginId }
|
||||||
|
NotificationsCache.saveMib(requireContext(), loginId, allForLogin)
|
||||||
|
refreshAdapters()
|
||||||
|
broadcastUnread()
|
||||||
|
}
|
||||||
mibNextStart[loginId] = result.nextStart
|
mibNextStart[loginId] = result.nextStart
|
||||||
mibDone[loginId] = result.nextStart > result.totalCount
|
mibDone[loginId] = hasOverlap || result.nextStart > result.totalCount
|
||||||
NotificationsCache.saveMib(requireContext(), loginId, result.items)
|
|
||||||
refreshAdapters()
|
|
||||||
broadcastUnread()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -221,19 +229,28 @@ 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 result = withContext(Dispatchers.IO) {
|
val start = mibNextStart[loginId] ?: 101
|
||||||
mibClient.fetchActivity(session, loginId, start, start + 99)
|
val cachedIds = allNotifications
|
||||||
}
|
.filter { it.bank == "MIB" && it.loginId == loginId }
|
||||||
if (result.items.isNotEmpty() && isAdded) {
|
.map { it.id }.toSet()
|
||||||
|
val result = withContext(Dispatchers.IO) {
|
||||||
|
mibClient.fetchActivity(session, loginId, start, start + 99)
|
||||||
|
}
|
||||||
|
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 }
|
||||||
allNotifications.sortByDescending { it.timestampMs }
|
.map { it.copy(isRead = it.id in readIds) }
|
||||||
|
if (newItems.isNotEmpty()) {
|
||||||
|
allNotifications.addAll(newItems)
|
||||||
|
allNotifications.sortByDescending { it.timestampMs }
|
||||||
|
val allForLogin = allNotifications.filter { it.bank == "MIB" && it.loginId == loginId }
|
||||||
|
NotificationsCache.saveMib(requireContext(), loginId, allForLogin)
|
||||||
|
}
|
||||||
mibNextStart[loginId] = result.nextStart
|
mibNextStart[loginId] = result.nextStart
|
||||||
mibDone[loginId] = result.nextStart > result.totalCount
|
mibDone[loginId] = result.nextStart > result.totalCount
|
||||||
val allForLogin = allNotifications.filter { it.bank == "MIB" && it.loginId == loginId }
|
if (newItems.isNotEmpty()) break
|
||||||
NotificationsCache.saveMib(requireContext(), loginId, allForLogin)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user