Compare commits

...

2 commits

2 changed files with 84 additions and 18 deletions

View file

@ -90,7 +90,11 @@ class NotificationsActivity : AppCompatActivity() {
} }
private fun getInvitationData(invitationId: String, rvPosition: Int): InvitationResponse? { private fun getInvitationData(invitationId: String, rvPosition: Int): InvitationResponse? {
return invitations[invitationId] val ret = invitations[invitationId]
if (ret == null) {
Log.w("NotificationsActivity", "NotificationAdapter requested unknown invitation: $invitationId")
}
return ret
} }
private fun getUserData(userId: String, rvPosition: Int?): UserResponse? { private fun getUserData(userId: String, rvPosition: Int?): UserResponse? {
@ -131,10 +135,30 @@ class NotificationsActivity : AppCompatActivity() {
private fun handleNotificationAction(notification: NotificationResponse, action: NotificationAdapter.Action, position: Int) { private fun handleNotificationAction(notification: NotificationResponse, action: NotificationAdapter.Action, position: Int) {
when (action) { when (action) {
NotificationAdapter.Action.ACCEPT -> { 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 -> { 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 -> { NotificationAdapter.Action.VIEW_EVENT -> {
// TODO: Handle viewing the event // TODO: Handle viewing the event
@ -142,7 +166,7 @@ class NotificationsActivity : AppCompatActivity() {
} }
} }
private fun handleNotificationClick(notification: NotificationResponse, position: Int) { private fun handleNotificationClick(notification: NotificationResponse, position: Int, sendToast: Boolean = true) {
lifecycleScope.launch { lifecycleScope.launch {
val updatedNotification = val updatedNotification =
RetrofitClient.notificationsService.markNotificationRead(notification.id) RetrofitClient.notificationsService.markNotificationRead(notification.id)
@ -151,7 +175,10 @@ class NotificationsActivity : AppCompatActivity() {
val adapter = rvNotifications.adapter as NotificationAdapter val adapter = rvNotifications.adapter as NotificationAdapter
adapter.notifyItemChanged(position) 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()
}
} }
} }
} }

View file

@ -88,22 +88,47 @@ class NotificationAdapter(
val user = getUserData(usernameId, position) val user = getUserData(usernameId, position)
val username = user?.username ?: "Unknown User" val username = user?.username ?: "Unknown User"
val statusSuffix = when (invitation.status) {
"accepted" -> " [already accepted]"
"declined" -> " [already declined]"
"pending" -> ""
else -> throw IllegalStateException("Unexpected invitation status: ${invitation.status} for invitation ID: ${invitation.id}")
}
return when (notification.message) { return when (notification.message) {
"new-invitation" -> "You have received an event invitation from @$username" "new-invitation" -> "You have received an event invitation from @$username$statusSuffix"
"invitation-accepted" -> "@$username has accepted your event invitation" "invitation-accepted" -> "@$username has accepted your event invitation$statusSuffix"
"invitation-declined" -> "@$username has declined your event invitation" "invitation-declined" -> "@$username has declined your event invitation$statusSuffix"
else -> throw IllegalArgumentException("Unexpected notification message: ${notification.message}") else -> throw IllegalArgumentException("Unexpected notification message: ${notification.message}")
} }
} }
private fun handleInvitationActions( private fun handleInvitationActions(
holder: NotificationViewHolder, holder: NotificationViewHolder,
notification: NotificationResponse, notification: NotificationResponse,
position: Int position: Int
) { ) {
if (notification.event_type == "invitation") { if (notification.event_type == "invitation") {
val invitation = getInvitationData(notification.data, position)
if (invitation == null) {
// For deleted invitations, make all the buttons visible but unclickable & grayed out
holder.invitationActions.visibility = View.VISIBLE
holder.acceptButton.visibility = View.VISIBLE
holder.declineButton.visibility = View.VISIBLE
holder.viewEventButton.visibility = View.VISIBLE
holder.acceptButton.isEnabled = false
holder.declineButton.isEnabled = false
holder.viewEventButton.isEnabled = false
return
}
when (notification.message) { when (notification.message) {
"new-invitation" -> { "new-invitation" -> {
when (invitation.status) {
"pending" -> {
// Show Accept/Decline & View buttons // Show Accept/Decline & View buttons
holder.invitationActions.visibility = View.VISIBLE holder.invitationActions.visibility = View.VISIBLE
holder.acceptButton.visibility = View.VISIBLE holder.acceptButton.visibility = View.VISIBLE
@ -117,6 +142,20 @@ class NotificationAdapter(
onActionClick(notification, Action.DECLINE, position) onActionClick(notification, Action.DECLINE, position)
} }
} }
"accepted", "declined" -> {
// Show only View button
holder.invitationActions.visibility = View.VISIBLE
holder.acceptButton.visibility = View.GONE
holder.declineButton.visibility = View.GONE
holder.viewEventButton.visibility = View.VISIBLE
holder.viewEventButton.setOnClickListener {
onActionClick(notification, Action.VIEW_EVENT, position)
}
}
else -> throw IllegalStateException("Unexpected invitation status: ${invitation.status} for invite ID: ${invitation.id}")
}
}
"invitation-accepted", "invitation-declined" -> { "invitation-accepted", "invitation-declined" -> {
// Show only View button // Show only View button
holder.invitationActions.visibility = View.VISIBLE holder.invitationActions.visibility = View.VISIBLE