feat(notifications): Swipe to delete support
This commit is contained in:
parent
d302927de0
commit
d741655292
|
@ -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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
}
|
}
|
Loading…
Reference in a new issue