From 01f1b003c9fdcfa34c44e2e9b4a5afd72be3ca6c Mon Sep 17 00:00:00 2001 From: Peter Vacho Date: Sat, 4 Jan 2025 20:28:37 +0100 Subject: [PATCH] fix(notifications): Proper reindex on notif remove --- .../activities/NotificationsActivity.kt | 33 +++++++++---------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/com/p_vacho/neat_calendar/activities/NotificationsActivity.kt b/app/src/main/java/com/p_vacho/neat_calendar/activities/NotificationsActivity.kt index 79d3f1b..c1527b5 100644 --- a/app/src/main/java/com/p_vacho/neat_calendar/activities/NotificationsActivity.kt +++ b/app/src/main/java/com/p_vacho/neat_calendar/activities/NotificationsActivity.kt @@ -150,7 +150,7 @@ class NotificationsActivity : AppCompatActivity() { val ret = invitations[invitationId] if (ret == null) { Log.w("NotificationsActivity", "NotificationAdapter requested unknown invitation: $invitationId") - Log.w("NotificationsActivity", "Known invitations: $invitations") + Log.w("NotificationsActivity", "Known invitations (${invitations.size}): $invitations") } return ret } @@ -159,7 +159,7 @@ class NotificationsActivity : AppCompatActivity() { val ret = events[eventId] if (ret == null) { Log.w("NotificationsActivity", "NotificationAdapter requested unknown event: $eventId") - Log.w("NotificationsActivity", "Known events: $events") + Log.w("NotificationsActivity", "Known events (${events.size}): $events") } return ret } @@ -278,21 +278,20 @@ class NotificationsActivity : AppCompatActivity() { // Call the deletion method lifecycleScope.launch(Dispatchers.IO) { - try { - RetrofitClient.notificationsService.deleteNotification(notification.id) - withContext(Dispatchers.Main) { - // Remove the notification from the list and notify the adapter - notifications.removeAt(position) - adapter.notifyItemRemoved(position) - Toast.makeText(this@NotificationsActivity, "Notification deleted", Toast.LENGTH_SHORT).show() - 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) - } + RetrofitClient.notificationsService.deleteNotification(notification.id) + withContext(Dispatchers.Main) { + // Remove the notification & notify the adapter about it + notifications.removeAt(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() + updateEmptyState() } } }