From 8c57542934bfc1a5de14e5f96e642d657bafa62a Mon Sep 17 00:00:00 2001 From: Peter Vacho Date: Sat, 4 Jan 2025 14:28:41 +0100 Subject: [PATCH] feat(notifications): Add accept/decline support --- .../activities/NotificationsActivity.kt | 31 ++++++++++++++++--- 1 file changed, 27 insertions(+), 4 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 8826e32..8f1e6e6 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 @@ -131,10 +131,30 @@ class NotificationsActivity : AppCompatActivity() { private fun handleNotificationAction(notification: NotificationResponse, action: NotificationAdapter.Action, position: Int) { when (action) { NotificationAdapter.Action.ACCEPT -> { - // TODO: Handle accept action + lifecycleScope.launch(Dispatchers.IO) { + val invitationId = notification.data + RetrofitClient.invitationService.acceptInvitation(invitationId) + + withContext(Dispatchers.Main) { + Toast.makeText(this@NotificationsActivity, "Invitation accepted", Toast.LENGTH_SHORT).show() + + // Also mark the notification as read after the interaction + if (!notification.read) handleNotificationClick(notification, position, false) + } + } } NotificationAdapter.Action.DECLINE -> { - // TODO: Handle decline action + lifecycleScope.launch(Dispatchers.IO) { + val invitationId = notification.data + RetrofitClient.invitationService.declineInvitation(invitationId) + + withContext(Dispatchers.Main) { + Toast.makeText(this@NotificationsActivity, "Invitation declined", Toast.LENGTH_SHORT).show() + + // Also mark the notification as read after the interaction + if (!notification.read) handleNotificationClick(notification, position, false) + } + } } NotificationAdapter.Action.VIEW_EVENT -> { // TODO: Handle viewing the event @@ -142,7 +162,7 @@ class NotificationsActivity : AppCompatActivity() { } } - private fun handleNotificationClick(notification: NotificationResponse, position: Int) { + private fun handleNotificationClick(notification: NotificationResponse, position: Int, sendToast: Boolean = true) { lifecycleScope.launch { val updatedNotification = RetrofitClient.notificationsService.markNotificationRead(notification.id) @@ -151,7 +171,10 @@ class NotificationsActivity : AppCompatActivity() { val adapter = rvNotifications.adapter as NotificationAdapter adapter.notifyItemChanged(position) - Toast.makeText(this@NotificationsActivity, "Marked as read", Toast.LENGTH_SHORT).show() + if (sendToast) { + Toast.makeText(this@NotificationsActivity, "Marked as read", Toast.LENGTH_SHORT) + .show() + } } } }