From d741655292f2dc2d44908551bc4ffb280485d094 Mon Sep 17 00:00:00 2001 From: Peter Vacho Date: Sat, 4 Jan 2025 18:28:32 +0100 Subject: [PATCH] feat(notifications): Swipe to delete support --- .../activities/NotificationsActivity.kt | 45 ++++++++++++++++++- .../api/services/NotificationService.kt | 7 +++ 2 files changed, 51 insertions(+), 1 deletion(-) 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 80cb277..c22c06b 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 @@ -9,6 +9,7 @@ import androidx.appcompat.app.AppCompatActivity import androidx.core.view.ViewCompat import androidx.core.view.WindowInsetsCompat import androidx.lifecycle.lifecycleScope +import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import com.p_vacho.neat_calendar.MyApplication @@ -66,7 +67,7 @@ class NotificationsActivity : AppCompatActivity() { invitations = invitationsDeferred.await().toMutableMap() events = eventsDeferred.await().toMutableMap() - rvNotifications.adapter = NotificationAdapter( + val adapter = NotificationAdapter( notifications, ::handleNotificationAction, ::handleNotificationClick, @@ -74,6 +75,9 @@ class NotificationsActivity : AppCompatActivity() { ::getUserData, ::getEventData, ) + + rvNotifications.adapter = adapter + setupSwipeToDelete(adapter) } } @@ -250,4 +254,43 @@ class NotificationsActivity : AppCompatActivity() { } } } + + private fun setupSwipeToDelete(adapter: NotificationAdapter) { + val itemTouchHelper = ItemTouchHelper(object : ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.RIGHT) { + override fun onMove( + recyclerView: RecyclerView, + viewHolder: RecyclerView.ViewHolder, + target: RecyclerView.ViewHolder + ): Boolean { + return false // We don't support moving items + } + + override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) { + val position = viewHolder.adapterPosition + val notification = notifications[position] + + // 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() + } + } 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) + } + } + } + } + }) + + // Attach ItemTouchHelper to the RecyclerView + itemTouchHelper.attachToRecyclerView(rvNotifications) + } } diff --git a/app/src/main/java/com/p_vacho/neat_calendar/api/services/NotificationService.kt b/app/src/main/java/com/p_vacho/neat_calendar/api/services/NotificationService.kt index 635dfcf..4e022f9 100644 --- a/app/src/main/java/com/p_vacho/neat_calendar/api/services/NotificationService.kt +++ b/app/src/main/java/com/p_vacho/neat_calendar/api/services/NotificationService.kt @@ -1,6 +1,7 @@ package com.p_vacho.neat_calendar.api.services import com.p_vacho.neat_calendar.api.models.NotificationResponse +import retrofit2.http.DELETE import retrofit2.http.GET import retrofit2.http.POST import retrofit2.http.Path @@ -14,4 +15,10 @@ interface NotificationService { @POST("notifications/{notification_id}/read") suspend fun markNotificationRead(@Path("notification_id") notificationId: String): NotificationResponse + + @POST("notifications/{notification_id}/unread") + suspend fun markNotificationUnread(@Path("notification_id") notificationId: String): NotificationResponse + + @DELETE("notifications/{notification_id}") + suspend fun deleteNotification(@Path("notification_id") notificationId: String): Unit } \ No newline at end of file