feat(notifications): Swipe to delete support

This commit is contained in:
Peter Vacho 2025-01-04 18:28:32 +01:00
parent d302927de0
commit d741655292
Signed by: school
GPG key ID: 8CFC3837052871B4
2 changed files with 51 additions and 1 deletions

View file

@ -9,6 +9,7 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.p_vacho.neat_calendar.MyApplication import com.p_vacho.neat_calendar.MyApplication
@ -66,7 +67,7 @@ class NotificationsActivity : AppCompatActivity() {
invitations = invitationsDeferred.await().toMutableMap() invitations = invitationsDeferred.await().toMutableMap()
events = eventsDeferred.await().toMutableMap() events = eventsDeferred.await().toMutableMap()
rvNotifications.adapter = NotificationAdapter( val adapter = NotificationAdapter(
notifications, notifications,
::handleNotificationAction, ::handleNotificationAction,
::handleNotificationClick, ::handleNotificationClick,
@ -74,6 +75,9 @@ class NotificationsActivity : AppCompatActivity() {
::getUserData, ::getUserData,
::getEventData, ::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)
}
} }

View file

@ -1,6 +1,7 @@
package com.p_vacho.neat_calendar.api.services package com.p_vacho.neat_calendar.api.services
import com.p_vacho.neat_calendar.api.models.NotificationResponse import com.p_vacho.neat_calendar.api.models.NotificationResponse
import retrofit2.http.DELETE
import retrofit2.http.GET import retrofit2.http.GET
import retrofit2.http.POST import retrofit2.http.POST
import retrofit2.http.Path import retrofit2.http.Path
@ -14,4 +15,10 @@ interface NotificationService {
@POST("notifications/{notification_id}/read") @POST("notifications/{notification_id}/read")
suspend fun markNotificationRead(@Path("notification_id") notificationId: String): NotificationResponse 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
} }