feat(notifications): Various improvements

This commit is contained in:
Peter Vacho 2025-01-04 17:01:20 +01:00
parent ef08d6ddd3
commit 49f7a977dd
Signed by: school
GPG key ID: 8CFC3837052871B4
2 changed files with 23 additions and 18 deletions

View file

@ -19,6 +19,7 @@ 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.NotificationResponse
import com.p_vacho.neat_calendar.api.models.UserResponse import com.p_vacho.neat_calendar.api.models.UserResponse
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import org.json.JSONException import org.json.JSONException
@ -85,9 +86,15 @@ class NotificationsActivity : AppCompatActivity() {
return emptyMap() return emptyMap()
} }
// Fetch the lists in parallel
val fetchedInvitations = withContext(Dispatchers.IO) { val fetchedInvitations = withContext(Dispatchers.IO) {
RetrofitClient.invitationService.getIncomingInvitations(userId) 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 }
} }
return fetchedInvitations.associateBy { it.id } return fetchedInvitations.associateBy { it.id }
} }
@ -95,6 +102,7 @@ class NotificationsActivity : AppCompatActivity() {
val ret = invitations[invitationId] val ret = invitations[invitationId]
if (ret == null) { if (ret == null) {
Log.w("NotificationsActivity", "NotificationAdapter requested unknown invitation: $invitationId") Log.w("NotificationsActivity", "NotificationAdapter requested unknown invitation: $invitationId")
Log.w("NotificationsActivity", "Known invitations: $invitations")
} }
return ret return ret
} }
@ -116,15 +124,14 @@ class NotificationsActivity : AppCompatActivity() {
} }
} catch (e: HttpException) { } catch (e: HttpException) {
if (e.code() != 404) { throw e } if (e.code() != 404) { throw e }
val errorBody = e.response()?.errorBody()?.string() val errorBody = e.response()?.errorBody()?.string() ?: throw e
if (errorBody == null) { throw e }
try { try {
val jsonObj = JSONObject(errorBody) val jsonObj = JSONObject(errorBody)
val errDetail = jsonObj.optString("detail") val errDetail = jsonObj.optString("detail")
if (errDetail != "No such user") { throw e } if (errDetail != "No such user") { throw e }
} catch (jsonParseExc: JSONException) { throw e } } catch (jsonParseExc: JSONException) { throw e }
Log.e("NotificationsActivity", "Failed to fetch user: $userId", e) Log.e("NotificationsActivity", "Failed to fetch user: $userId")
users[userId] = null // Cache null for non-existing users users[userId] = null // Cache null for non-existing users
// No need for an adapter update here, null is already the default // No need for an adapter update here, null is already the default
@ -139,7 +146,7 @@ class NotificationsActivity : AppCompatActivity() {
NotificationAdapter.Action.ACCEPT -> { NotificationAdapter.Action.ACCEPT -> {
lifecycleScope.launch(Dispatchers.IO) { lifecycleScope.launch(Dispatchers.IO) {
val invitationId = notification.data val invitationId = notification.data
RetrofitClient.invitationService.acceptInvitation(invitationId) invitations[invitationId] = RetrofitClient.invitationService.acceptInvitation(invitationId)
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
Toast.makeText(this@NotificationsActivity, "Invitation accepted", Toast.LENGTH_SHORT).show() Toast.makeText(this@NotificationsActivity, "Invitation accepted", Toast.LENGTH_SHORT).show()
@ -152,7 +159,8 @@ class NotificationsActivity : AppCompatActivity() {
// but otherwise, we'll need to trigger an item update ourselves, to make sure that // but otherwise, we'll need to trigger an item update ourselves, to make sure that
// the accept/decline buttons are removed // the accept/decline buttons are removed
rvNotifications.adapter!!.notifyItemChanged(position) rvNotifications.adapter!!.notifyItemChanged(position)
} } }
}
} }
} }
NotificationAdapter.Action.DECLINE -> { NotificationAdapter.Action.DECLINE -> {
@ -188,8 +196,7 @@ class NotificationsActivity : AppCompatActivity() {
RetrofitClient.notificationsService.markNotificationRead(notification.id) RetrofitClient.notificationsService.markNotificationRead(notification.id)
notifications[position] = updatedNotification notifications[position] = updatedNotification
val adapter = rvNotifications.adapter as NotificationAdapter rvNotifications.adapter!!.notifyItemChanged(position)
adapter.notifyItemChanged(position)
if (sendToast) { if (sendToast) {
Toast.makeText(this@NotificationsActivity, "Marked as read", Toast.LENGTH_SHORT) Toast.makeText(this@NotificationsActivity, "Marked as read", Toast.LENGTH_SHORT)

View file

@ -90,17 +90,15 @@ 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) { return when (notification.message) {
"accepted" -> " [already accepted]" "new-invitation" -> when (invitation.status) {
"declined" -> " [already declined]" "accepted" -> "You have received an event invitation from @$username [already accepted]"
"pending" -> "" "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}") else -> throw IllegalStateException("Unexpected invitation status: ${invitation.status} for invitation ID: ${invitation.id}")
} }
"invitation-accepted" -> "@$username has accepted your event invitation"
return when (notification.message) { "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}") else -> throw IllegalArgumentException("Unexpected notification message: ${notification.message}")
} }
} }