Compare commits

..

1 commit

Author SHA1 Message Date
Peter Vacho 73d24ffdfc
feat(notifications): Handle some edge cases 2025-01-04 16:36:28 +01:00
3 changed files with 31 additions and 47 deletions

View file

@ -19,15 +19,12 @@ import com.p_vacho.neat_calendar.api.models.InvitationResponse
import com.p_vacho.neat_calendar.api.models.NotificationResponse
import com.p_vacho.neat_calendar.api.models.UserResponse
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.json.JSONException
import org.json.JSONObject
import retrofit2.HttpException
// TODO: Localize all strings
class NotificationsActivity : AppCompatActivity() {
private lateinit var rvNotifications: RecyclerView
private lateinit var btnBack: ImageButton
@ -86,15 +83,9 @@ class NotificationsActivity : AppCompatActivity() {
return emptyMap()
}
// Fetch the lists in parallel
val fetchedInvitations = withContext(Dispatchers.IO) {
val incomingDeferred = async { RetrofitClient.invitationService.getIncomingInvitations(userId) }
val ownedDeferred = async { RetrofitClient.invitationService.getInvitations(userId) }
val incomingInvitations = incomingDeferred.await()
val ownedInvitations = ownedDeferred.await()
(incomingInvitations + ownedInvitations).distinctBy { it.id }
RetrofitClient.invitationService.getIncomingInvitations(userId)
}
return fetchedInvitations.associateBy { it.id }
}
@ -102,7 +93,6 @@ class NotificationsActivity : AppCompatActivity() {
val ret = invitations[invitationId]
if (ret == null) {
Log.w("NotificationsActivity", "NotificationAdapter requested unknown invitation: $invitationId")
Log.w("NotificationsActivity", "Known invitations: $invitations")
}
return ret
}
@ -124,14 +114,15 @@ class NotificationsActivity : AppCompatActivity() {
}
} catch (e: HttpException) {
if (e.code() != 404) { throw e }
val errorBody = e.response()?.errorBody()?.string() ?: throw e
val errorBody = e.response()?.errorBody()?.string()
if (errorBody == null) { throw e }
try {
val jsonObj = JSONObject(errorBody)
val errDetail = jsonObj.optString("detail")
if (errDetail != "No such user") { throw e }
} catch (jsonParseExc: JSONException) { throw e }
Log.e("NotificationsActivity", "Failed to fetch user: $userId")
Log.e("NotificationsActivity", "Failed to fetch user: $userId", e)
users[userId] = null // Cache null for non-existing users
// No need for an adapter update here, null is already the default
@ -146,46 +137,31 @@ class NotificationsActivity : AppCompatActivity() {
NotificationAdapter.Action.ACCEPT -> {
lifecycleScope.launch(Dispatchers.IO) {
val invitationId = notification.data
invitations[invitationId] = RetrofitClient.invitationService.acceptInvitation(invitationId)
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)
} else {
// If the notification was unread, handleNotificationClick will have triggered this
// but otherwise, we'll need to trigger an item update ourselves, to make sure that
// the accept/decline buttons are removed
rvNotifications.adapter!!.notifyItemChanged(position)
}
if (!notification.read) handleNotificationClick(notification, position, false)
}
}
}
NotificationAdapter.Action.DECLINE -> {
lifecycleScope.launch(Dispatchers.IO) {
val invitationId = notification.data
invitations[invitationId] = RetrofitClient.invitationService.declineInvitation(invitationId)
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)
} else {
// If the notification was unread, handleNotificationClick will have triggered this
// but otherwise, we'll need to trigger an item update ourselves, to make sure that
// the accept/decline buttons are removed
rvNotifications.adapter!!.notifyItemChanged(position)
}
if (!notification.read) handleNotificationClick(notification, position, false)
}
}
}
NotificationAdapter.Action.VIEW_EVENT -> {
// TODO: Handle viewing the event
if (!notification.read) handleNotificationClick(notification, position, false)
}
}
}
@ -196,7 +172,8 @@ class NotificationsActivity : AppCompatActivity() {
RetrofitClient.notificationsService.markNotificationRead(notification.id)
notifications[position] = updatedNotification
rvNotifications.adapter!!.notifyItemChanged(position)
val adapter = rvNotifications.adapter as NotificationAdapter
adapter.notifyItemChanged(position)
if (sendToast) {
Toast.makeText(this@NotificationsActivity, "Marked as read", Toast.LENGTH_SHORT)

View file

@ -14,8 +14,6 @@ import java.time.Duration
import java.time.OffsetDateTime
import java.time.format.DateTimeFormatter
// TODO: Localize all strings
class NotificationAdapter(
private val notifications: MutableList<NotificationResponse>,
private val onActionClick: (NotificationResponse, Action, Int) -> Unit,
@ -90,15 +88,17 @@ class NotificationAdapter(
val user = getUserData(usernameId, position)
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) {
"new-invitation" -> when (invitation.status) {
"accepted" -> "You have received an event invitation from @$username [already accepted]"
"declined" -> "You have received an event invitation from @$username [already declined]"
"pending" -> "You have received an event invitation from @$username"
else -> throw IllegalStateException("Unexpected invitation status: ${invitation.status} for invitation ID: ${invitation.id}")
}
"invitation-accepted" -> "@$username has accepted your event invitation"
"invitation-declined" -> "@$username has declined your event invitation"
"new-invitation" -> "You have received an event invitation from @$username$statusSuffix"
"invitation-accepted" -> "@$username has accepted your event invitation$statusSuffix"
"invitation-declined" -> "@$username has declined your event invitation$statusSuffix"
else -> throw IllegalArgumentException("Unexpected notification message: ${notification.message}")
}
}
@ -112,8 +112,15 @@ class NotificationAdapter(
if (notification.event_type == "invitation") {
val invitation = getInvitationData(notification.data, position)
if (invitation == null) {
// No buttons for deleted invitations
holder.invitationActions.visibility = View.GONE
// 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
}

View file

@ -21,9 +21,9 @@ interface InvitationsService {
suspend fun getIncomingInvitations(@Path("user_id") userId: String): List<InvitationResponse>
@POST("invitations/{invitation_id}/accept")
suspend fun acceptInvitation(@Path("invitation_id") invitationId: String): InvitationResponse
suspend fun acceptInvitation(@Path("invitation_id") invitationId: String): Unit
@POST("invitations/{invitation_id}/decline")
suspend fun declineInvitation(@Path("invitation_id") invitationId: String): InvitationResponse
suspend fun declineInvitation(@Path("invitation_id") invitationId: String): Unit
}