diff --git a/app/src/main/java/com/p_vacho/neat_calendar/adapters/NotificationAdapter.kt b/app/src/main/java/com/p_vacho/neat_calendar/adapters/NotificationAdapter.kt index d1207f5..ad3284f 100644 --- a/app/src/main/java/com/p_vacho/neat_calendar/adapters/NotificationAdapter.kt +++ b/app/src/main/java/com/p_vacho/neat_calendar/adapters/NotificationAdapter.kt @@ -9,6 +9,9 @@ import android.widget.TextView import androidx.recyclerview.widget.RecyclerView import com.p_vacho.neat_calendar.R import com.p_vacho.neat_calendar.api.models.NotificationResponse +import java.time.Duration +import java.time.OffsetDateTime +import java.time.format.DateTimeFormatter class NotificationAdapter( private val notifications: MutableList, @@ -22,6 +25,7 @@ class NotificationAdapter( inner class NotificationViewHolder(view: View) : RecyclerView.ViewHolder(view) { 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 declineButton: ImageButton = view.findViewById(R.id.declineButton) val viewEventButton: ImageButton = view.findViewById(R.id.viewEventButton) @@ -40,6 +44,10 @@ class NotificationAdapter( 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 holder.unreadIndicator.visibility = if (notification.read) View.GONE else View.VISIBLE @@ -76,4 +84,26 @@ class NotificationAdapter( } 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) + } + } + } } diff --git a/app/src/main/res/layout/item_notification.xml b/app/src/main/res/layout/item_notification.xml index f126b8a..7cb8fca 100644 --- a/app/src/main/res/layout/item_notification.xml +++ b/app/src/main/res/layout/item_notification.xml @@ -36,16 +36,27 @@ app:layout_constraintEnd_toEndOf="parent" android:layout_marginLeft="12dp" /> + + +