From f9c029274584cdd01e7281627eb8cbfa90e82eee Mon Sep 17 00:00:00 2001 From: Nico Alt <nicoalt@posteo.org> Date: Fri, 1 Oct 2021 17:28:56 +0200 Subject: [PATCH] Display correct timestamp in messages and contact list Got some inspiration from briar-headless for the contact list: https://code.briarproject.org/briar/briar/-/blob/release-1.3.8/briar-headless/src/main/java/org/briarproject/briar/headless/contact/ContactControllerImpl.kt#L81 Closes #53. --- .../briarproject/briar/desktop/chat/Chat.kt | 22 +++------------- .../desktop/paul/views/PrivateMessageView.kt | 25 +++++++++++++++++-- 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/src/main/kotlin/org/briarproject/briar/desktop/chat/Chat.kt b/src/main/kotlin/org/briarproject/briar/desktop/chat/Chat.kt index 883a6e84c7..d59cdeda91 100644 --- a/src/main/kotlin/org/briarproject/briar/desktop/chat/Chat.kt +++ b/src/main/kotlin/org/briarproject/briar/desktop/chat/Chat.kt @@ -1,28 +1,14 @@ package org.briarproject.briar.desktop.chat -import java.time.Instant -import java.time.LocalDateTime -import java.time.ZoneId -import java.time.format.DateTimeFormatter +import org.briarproject.briar.desktop.paul.views.getFormattedTimestamp class Chat { - companion object { - private val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm") - } - val messages: MutableList<SimpleMessage> = ArrayList() - fun add(message: SimpleMessage) { - messages.add(message) - } - fun appendMessage(local: Boolean, timestamp: Long, messageText: String?) { - val name = if (local) "You" else "Other" - val dateTime = LocalDateTime.ofInstant( - Instant.ofEpochMilli(timestamp), ZoneId.systemDefault() - ) - val author = String.format("%s (%s): ", name, formatter.format(dateTime)) - messages.add(SimpleMessage(local, author, messageText!!, "time", true)) + val author = if (local) "You" else "Other" + val formattedTimestamp = getFormattedTimestamp(timestamp) + messages.add(SimpleMessage(local, author, messageText!!, formattedTimestamp, true)) } } diff --git a/src/main/kotlin/org/briarproject/briar/desktop/paul/views/PrivateMessageView.kt b/src/main/kotlin/org/briarproject/briar/desktop/paul/views/PrivateMessageView.kt index 093f3625fc..384020624f 100644 --- a/src/main/kotlin/org/briarproject/briar/desktop/paul/views/PrivateMessageView.kt +++ b/src/main/kotlin/org/briarproject/briar/desktop/paul/views/PrivateMessageView.kt @@ -93,6 +93,12 @@ import org.briarproject.briar.desktop.paul.theme.briarGreen import org.briarproject.briar.desktop.paul.theme.darkGray import org.briarproject.briar.desktop.paul.theme.divider import org.briarproject.briar.desktop.paul.theme.lightGray +import java.time.Instant +import java.time.LocalDateTime +import java.time.ZoneId +import java.time.format.DateTimeFormatter +import java.time.format.FormatStyle +import java.time.temporal.ChronoUnit import java.util.Collections val HEADER_SIZE = 56.dp @@ -271,9 +277,9 @@ fun ContactCard( color = Color.White, modifier = Modifier.align(Alignment.Start).padding(bottom = 2.dp) ) - // TODO add proper last message time + val latestMsgTime = CM.current.getGroupCount(contact.id).latestMsgTime Text( - "10 min ago", + getFormattedTimestamp(latestMsgTime), fontSize = 10.sp, color = Color.LightGray, modifier = Modifier.align(Alignment.Start) @@ -297,6 +303,21 @@ fun ContactCard( Divider(color = divider, thickness = 1.dp, modifier = Modifier.fillMaxWidth()) } +fun getFormattedTimestamp(timestamp: Long): String { + val messageTime = LocalDateTime.ofInstant( + Instant.ofEpochMilli(timestamp), ZoneId.systemDefault() + ) + val currentTime = LocalDateTime.now() + val difference = ChronoUnit.MINUTES.between(messageTime, currentTime) + + val formatter = if (difference < 1440) { // = 1 day + DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT) + } else { + DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT) + } + return messageTime.format(formatter) +} + @Composable fun ContactList( contact: Contact, -- GitLab