diff --git a/apps/android/app/src/main/java/chat/simplex/app/model/ChatModel.kt b/apps/android/app/src/main/java/chat/simplex/app/model/ChatModel.kt index ff3ed6fd7c..2f661fa973 100644 --- a/apps/android/app/src/main/java/chat/simplex/app/model/ChatModel.kt +++ b/apps/android/app/src/main/java/chat/simplex/app/model/ChatModel.kt @@ -1,14 +1,13 @@ package chat.simplex.app.model -import android.annotation.SuppressLint import android.net.Uri import androidx.compose.runtime.mutableStateListOf import androidx.compose.runtime.mutableStateOf import chat.simplex.app.SimplexApp import kotlinx.datetime.* +import kotlinx.datetime.TimeZone import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable -import java.time.format.DateTimeFormatter class ChatModel(val controller: ChatController, val alertManager: SimplexApp.AlertManager) { var currentUser = mutableStateOf(null) @@ -495,13 +494,14 @@ class CIMeta ( } } -// TODO use old api? -// TODO date for older timestamps -@SuppressLint("NewApi") -fun getTimestampText(d: Instant): String { - val dt = d.toLocalDateTime(TimeZone.currentSystemDefault()).toJavaLocalDateTime() - val formatter = DateTimeFormatter.ofPattern("HH:mm") - return dt.format(formatter) +fun getTimestampText(t: Instant): String { + val tz = TimeZone.currentSystemDefault() + val now: LocalDateTime = Clock.System.now().toLocalDateTime(tz) + val time: LocalDateTime = t.toLocalDateTime(tz) + val recent = now.date == time.date || + (now.date.minus(time.date).days == 1 && now.hour < 12 && time.hour >= 18 ) + return if (recent) String.format("%02d:%02d", time.hour, time.minute) + else String.format("%02d/%02d", time.dayOfMonth, time.monthNumber) } @Serializable diff --git a/apps/android/app/src/main/java/chat/simplex/app/views/chatlist/ChatPreviewView.kt b/apps/android/app/src/main/java/chat/simplex/app/views/chatlist/ChatPreviewView.kt index 08fbb02511..a064d5e518 100644 --- a/apps/android/app/src/main/java/chat/simplex/app/views/chatlist/ChatPreviewView.kt +++ b/apps/android/app/src/main/java/chat/simplex/app/views/chatlist/ChatPreviewView.kt @@ -1,6 +1,5 @@ package chat.simplex.app.views.chatlist -import android.icu.text.SimpleDateFormat import androidx.compose.foundation.* import androidx.compose.foundation.layout.* import androidx.compose.foundation.shape.CircleShape @@ -14,31 +13,9 @@ import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp -import chat.simplex.app.model.Chat -import chat.simplex.app.model.ChatInfo +import chat.simplex.app.model.* import chat.simplex.app.ui.theme.HighOrLowlight import chat.simplex.app.ui.theme.SimpleXTheme -import kotlinx.datetime.Instant -import kotlinx.datetime.toJavaInstant -import java.time.LocalDateTime -import java.util.* -import java.time.Instant as JavaInstant - -fun getDisplayTime(t: Instant) : String { - val timeFormatter = SimpleDateFormat("HH:mm", Locale.getDefault()) - val dateFormatter = SimpleDateFormat("dd/MM/y", Locale.getDefault()) - val timeProvided: JavaInstant = t.toJavaInstant() - val tz = TimeZone.getDefault() - val dateProvided = LocalDateTime.ofInstant(timeProvided, tz.toZoneId()).toLocalDate() - val today = LocalDateTime.now().atZone(tz.toZoneId()).toLocalDate() - val yesterday = today.minusDays(1) - - return when (dateProvided) { - today -> timeFormatter.format(Date.from(timeProvided)) - yesterday -> "yesterday" - else -> dateFormatter.format(Date.from(timeProvided)) - } -} @Composable fun ChatPreviewView(chat: Chat, goToChat: () -> Unit) { @@ -70,9 +47,9 @@ fun ChatPreviewView(chat: Chat, goToChat: () -> Unit) { Text(chat.chatInfo.chatViewName, fontWeight = FontWeight.Bold) ( if (chat.chatItems.count() > 0) { - Text(getDisplayTime(chat.chatItems.last().meta.itemTs), color = HighOrLowlight) + Text(getTimestampText(chat.chatItems.last().meta.itemTs), color = HighOrLowlight) } - else Text(getDisplayTime(chat.chatInfo.createdAt), color = HighOrLowlight) + else Text(getTimestampText(chat.chatInfo.createdAt), color = HighOrLowlight) ) } if (chat.chatItems.count() > 0) {