From 5ba14f2abadcd3e74e0e31e21a60f5d855a5e7cf Mon Sep 17 00:00:00 2001 From: Peter Vacho Date: Sat, 4 Jan 2025 22:02:18 +0100 Subject: [PATCH] feat(notifications): Add function to remove nth notification --- .../activities/NotificationsActivity.kt | 12 +++--------- .../adapters/NotificationAdapter.kt | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 9 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 3a656e1..4af0f35 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 @@ -333,15 +333,9 @@ class NotificationsActivity : AppCompatActivity() { lifecycleScope.launch(Dispatchers.IO) { 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) + // This both notifies the adapter & removes the notification from the + // notifications list + (rvNotifications.adapter as NotificationAdapter).removeNotificationAt(position) Toast.makeText(this@NotificationsActivity, "Notification deleted", Toast.LENGTH_SHORT).show() updateEmptyState() diff --git a/app/src/main/java/com/p_vacho/neat_calendar/adapters/NotificationAdapter.kt b/app/src/main/java/com/p_vacho/neat_calendar/adapters/NotificationAdapter.kt index ce55258..3a0e2a0 100644 --- a/app/src/main/java/com/p_vacho/neat_calendar/adapters/NotificationAdapter.kt +++ b/app/src/main/java/com/p_vacho/neat_calendar/adapters/NotificationAdapter.kt @@ -189,4 +189,20 @@ class NotificationAdapter( else -> DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm").format(createdAt) } } + + /** + * Remove a notification from the list and notify the adapter. + * + * Call this after the callback deletes the notification from the backend API. + */ + fun removeNotificationAt(position: Int) { + notifications.removeAt(position) + 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 bound to the callbacks from the click listeners, so we need to refresh + // all of the notifications below this one as well. + notifyItemRangeChanged(position, notifications.size - position) + } }