Compare commits

...

2 commits

3 changed files with 83 additions and 24 deletions

View file

@ -19,12 +19,15 @@ 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
import org.json.JSONObject import org.json.JSONObject
import retrofit2.HttpException import retrofit2.HttpException
// TODO: Localize all strings
class NotificationsActivity : AppCompatActivity() { class NotificationsActivity : AppCompatActivity() {
private lateinit var rvNotifications: RecyclerView private lateinit var rvNotifications: RecyclerView
private lateinit var btnBack: ImageButton private lateinit var btnBack: ImageButton
@ -83,14 +86,25 @@ 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 }
} }
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")
Log.w("NotificationsActivity", "Known invitations: $invitations")
}
return ret
} }
private fun getUserData(userId: String, rvPosition: Int?): UserResponse? { private fun getUserData(userId: String, rvPosition: Int?): UserResponse? {
@ -110,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
@ -133,31 +146,46 @@ 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()
// Also mark the notification as read after the interaction // Also mark the notification as read after the interaction
if (!notification.read) handleNotificationClick(notification, position, false) 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)
}
} }
} }
} }
NotificationAdapter.Action.DECLINE -> { NotificationAdapter.Action.DECLINE -> {
lifecycleScope.launch(Dispatchers.IO) { lifecycleScope.launch(Dispatchers.IO) {
val invitationId = notification.data val invitationId = notification.data
RetrofitClient.invitationService.declineInvitation(invitationId) invitations[invitationId] = RetrofitClient.invitationService.declineInvitation(invitationId)
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
Toast.makeText(this@NotificationsActivity, "Invitation declined", Toast.LENGTH_SHORT).show() Toast.makeText(this@NotificationsActivity, "Invitation declined", Toast.LENGTH_SHORT).show()
// Also mark the notification as read after the interaction // Also mark the notification as read after the interaction
if (!notification.read) handleNotificationClick(notification, position, false) 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)
}
} }
} }
} }
NotificationAdapter.Action.VIEW_EVENT -> { NotificationAdapter.Action.VIEW_EVENT -> {
// TODO: Handle viewing the event // TODO: Handle viewing the event
if (!notification.read) handleNotificationClick(notification, position, false)
} }
} }
} }
@ -168,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

@ -14,6 +14,8 @@ import java.time.Duration
import java.time.OffsetDateTime import java.time.OffsetDateTime
import java.time.format.DateTimeFormatter import java.time.format.DateTimeFormatter
// TODO: Localize all strings
class NotificationAdapter( class NotificationAdapter(
private val notifications: MutableList<NotificationResponse>, private val notifications: MutableList<NotificationResponse>,
private val onActionClick: (NotificationResponse, Action, Int) -> Unit, private val onActionClick: (NotificationResponse, Action, Int) -> Unit,
@ -89,32 +91,62 @@ class NotificationAdapter(
val username = user?.username ?: "Unknown User" val username = user?.username ?: "Unknown User"
return when (notification.message) { return when (notification.message) {
"new-invitation" -> "You have received an event invitation from @$username" "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-accepted" -> "@$username has accepted your event invitation"
"invitation-declined" -> "@$username has declined your event invitation" "invitation-declined" -> "@$username has declined your event invitation"
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) {
// No buttons for deleted invitations
holder.invitationActions.visibility = View.GONE
return
}
when (notification.message) { when (notification.message) {
"new-invitation" -> { "new-invitation" -> {
// Show Accept/Decline & View buttons when (invitation.status) {
holder.invitationActions.visibility = View.VISIBLE "pending" -> {
holder.acceptButton.visibility = View.VISIBLE // Show Accept/Decline & View buttons
holder.declineButton.visibility = View.VISIBLE holder.invitationActions.visibility = View.VISIBLE
holder.viewEventButton.visibility = View.VISIBLE holder.acceptButton.visibility = View.VISIBLE
holder.declineButton.visibility = View.VISIBLE
holder.viewEventButton.visibility = View.VISIBLE
holder.acceptButton.setOnClickListener { holder.acceptButton.setOnClickListener {
onActionClick(notification, Action.ACCEPT, position) onActionClick(notification, Action.ACCEPT, position)
} }
holder.declineButton.setOnClickListener { holder.declineButton.setOnClickListener {
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" -> {

View file

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