fix(notifications): Proper reindex on notif remove

This commit is contained in:
Peter Vacho 2025-01-04 20:28:37 +01:00
parent 307afd1f0c
commit 01f1b003c9
Signed by: school
GPG key ID: 8CFC3837052871B4

View file

@ -150,7 +150,7 @@ class NotificationsActivity : AppCompatActivity() {
val ret = invitations[invitationId] val ret = invitations[invitationId]
if (ret == null) { if (ret == null) {
Log.w("NotificationsActivity", "NotificationAdapter requested unknown invitation: $invitationId") Log.w("NotificationsActivity", "NotificationAdapter requested unknown invitation: $invitationId")
Log.w("NotificationsActivity", "Known invitations: $invitations") Log.w("NotificationsActivity", "Known invitations (${invitations.size}): $invitations")
} }
return ret return ret
} }
@ -159,7 +159,7 @@ class NotificationsActivity : AppCompatActivity() {
val ret = events[eventId] val ret = events[eventId]
if (ret == null) { if (ret == null) {
Log.w("NotificationsActivity", "NotificationAdapter requested unknown event: $eventId") Log.w("NotificationsActivity", "NotificationAdapter requested unknown event: $eventId")
Log.w("NotificationsActivity", "Known events: $events") Log.w("NotificationsActivity", "Known events (${events.size}): $events")
} }
return ret return ret
} }
@ -278,22 +278,21 @@ class NotificationsActivity : AppCompatActivity() {
// Call the deletion method // Call the deletion method
lifecycleScope.launch(Dispatchers.IO) { lifecycleScope.launch(Dispatchers.IO) {
try {
RetrofitClient.notificationsService.deleteNotification(notification.id) RetrofitClient.notificationsService.deleteNotification(notification.id)
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
// Remove the notification from the list and notify the adapter // Remove the notification & notify the adapter about it
notifications.removeAt(position) notifications.removeAt(position)
adapter.notifyItemRemoved(position) adapter.notifyItemRemoved(position)
// Annoyingly, we can't just use notifyItemRemoved for the single removed item,
// as all the items below it would now be using the wrong position that was
// already bounded to the callbacks from the click listeners, so we need to refresh
// all of the notifications below this one as well.
adapter.notifyItemRangeChanged(position, notifications.size - position)
Toast.makeText(this@NotificationsActivity, "Notification deleted", Toast.LENGTH_SHORT).show() Toast.makeText(this@NotificationsActivity, "Notification deleted", Toast.LENGTH_SHORT).show()
updateEmptyState() updateEmptyState()
} }
} catch (e: Exception) {
withContext(Dispatchers.Main) {
Toast.makeText(this@NotificationsActivity, "Failed to delete notification", Toast.LENGTH_SHORT).show()
// Reset swipe if deletion fails
adapter.notifyItemChanged(position)
}
}
} }
} }
}) })