feat(notifications): Show notification creation time

This commit is contained in:
Peter Vacho 2025-01-04 12:47:10 +01:00
parent 4ca096df1c
commit ac36100c74
Signed by: school
GPG key ID: 8CFC3837052871B4
2 changed files with 43 additions and 2 deletions

View file

@ -9,6 +9,9 @@ import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.p_vacho.neat_calendar.R import com.p_vacho.neat_calendar.R
import com.p_vacho.neat_calendar.api.models.NotificationResponse import com.p_vacho.neat_calendar.api.models.NotificationResponse
import java.time.Duration
import java.time.OffsetDateTime
import java.time.format.DateTimeFormatter
class NotificationAdapter( class NotificationAdapter(
private val notifications: MutableList<NotificationResponse>, private val notifications: MutableList<NotificationResponse>,
@ -22,6 +25,7 @@ class NotificationAdapter(
inner class NotificationViewHolder(view: View) : RecyclerView.ViewHolder(view) { inner class NotificationViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val message: TextView = view.findViewById(R.id.notificationMessage) val message: TextView = view.findViewById(R.id.notificationMessage)
val notificationTime: TextView = view.findViewById(R.id.notificationTime)
val acceptButton: ImageButton = view.findViewById(R.id.acceptButton) val acceptButton: ImageButton = view.findViewById(R.id.acceptButton)
val declineButton: ImageButton = view.findViewById(R.id.declineButton) val declineButton: ImageButton = view.findViewById(R.id.declineButton)
val viewEventButton: ImageButton = view.findViewById(R.id.viewEventButton) val viewEventButton: ImageButton = view.findViewById(R.id.viewEventButton)
@ -40,6 +44,10 @@ class NotificationAdapter(
holder.message.text = notification.message holder.message.text = notification.message
// Format and set the creation time
val formattedTime = formatNotificationTime(notification.created_at)
holder.notificationTime.text = formattedTime
// Set visibility based on read/unread status // Set visibility based on read/unread status
holder.unreadIndicator.visibility = if (notification.read) View.GONE else View.VISIBLE holder.unreadIndicator.visibility = if (notification.read) View.GONE else View.VISIBLE
@ -76,4 +84,26 @@ class NotificationAdapter(
} }
override fun getItemCount(): Int = notifications.size override fun getItemCount(): Int = notifications.size
/**
* Format the time at which a notification was received.
*
* If the notification is recent (setn within the last 24h), show the time
* in a format of (x hours/minutes/seconds ago), otherwise, use yyyy-MM-dd HH:mm
*/
private fun formatNotificationTime(createdAt: OffsetDateTime): String {
val now = OffsetDateTime.now()
val duration = Duration.between(createdAt, now)
return when {
duration.seconds < 60 -> "${duration.seconds}s ago"
duration.toMinutes() < 60 -> "${duration.toMinutes()}m ago"
duration.toHours() < 24 -> "${duration.toHours()}h ago"
else -> {
// Format as a date/time for older notifications
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")
createdAt.format(formatter)
}
}
}
} }

View file

@ -36,16 +36,27 @@
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginLeft="12dp" /> android:layout_marginLeft="12dp" />
<!-- Time Received -->
<TextView
android:id="@+id/notificationTime"
android:layout_width="0dp"
android:layout_height="wrap_content"
tools:text="2h ago"
android:textSize="12sp"
android:textColor="@android:color/darker_gray"
app:layout_constraintTop_toBottomOf="@id/notificationMessage"
app:layout_constraintStart_toStartOf="@id/notificationMessage" />
<!-- Buttons for invitations --> <!-- Buttons for invitations -->
<LinearLayout <LinearLayout
android:id="@+id/invitationActions" android:id="@+id/invitationActions"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="8dp" android:layout_marginTop="4dp"
android:visibility="gone" android:visibility="gone"
tools:visibility="visible" tools:visibility="visible"
android:orientation="horizontal" android:orientation="horizontal"
app:layout_constraintTop_toBottomOf="@id/notificationMessage" app:layout_constraintTop_toBottomOf="@id/notificationTime"
app:layout_constraintEnd_toEndOf="parent"> app:layout_constraintEnd_toEndOf="parent">
<ImageButton <ImageButton