feat(notifications): Add function to remove nth notification
This commit is contained in:
parent
bf3b296136
commit
5ba14f2aba
|
@ -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()
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue