diff --git a/app/src/main/java/com/aiosman/riderpro/ui/chat/ChatScreen.kt b/app/src/main/java/com/aiosman/riderpro/ui/chat/ChatScreen.kt index b19b3b9..7b50bd7 100644 --- a/app/src/main/java/com/aiosman/riderpro/ui/chat/ChatScreen.kt +++ b/app/src/main/java/com/aiosman/riderpro/ui/chat/ChatScreen.kt @@ -3,6 +3,7 @@ package com.aiosman.riderpro.ui.chat import android.app.Activity import android.content.Intent import android.net.Uri +import android.util.Log import androidx.activity.compose.rememberLauncherForActivityResult import androidx.activity.result.contract.ActivityResultContracts import androidx.compose.animation.Crossfade @@ -188,14 +189,25 @@ fun ChatScreen(userId: String) { verticalArrangement = Arrangement.Top ) { val chatList = viewModel.getDisplayChatList() + groupMessagesByTime(chatList, viewModel) // Pass viewModel to the function items(chatList.size) { index -> - Box( - modifier = Modifier - .fillMaxWidth() - .padding(8.dp) - ) { - ChatItem(item = chatList[index], viewModel.myProfile?.trtcUserId!!) + val item = chatList[index] + if (item.showTimestamp) { + val calendar = java.util.Calendar.getInstance() + calendar.timeInMillis = item.timestamp + Text( + text = calendar.time.formatChatTime(context), // Format the timestamp + style = TextStyle( + color = Color.Gray, + fontSize = 14.sp, + textAlign = TextAlign.Center + ), + modifier = Modifier + .fillMaxWidth() + .padding(vertical = 8.dp) + ) } + ChatItem(item = item, viewModel.myProfile?.trtcUserId!!) } } } @@ -216,26 +228,6 @@ fun ChatSelfItem(item: ChatItem) { Column( horizontalAlignment = androidx.compose.ui.Alignment.End, ) { - Row() { - val calendar = java.util.Calendar.getInstance() - calendar.timeInMillis = item.timestamp - Text( - text = calendar.time.formatChatTime(context), - style = TextStyle( - color = Color.Gray, - fontSize = 14.sp - ) - ) - Spacer(modifier = Modifier.width(8.dp)) - Text( - text = item.nickname, - style = TextStyle( - color = Color.Black, - fontSize = 14.sp - ) - ) - } - Spacer(modifier = Modifier.height(4.dp)) Box( modifier = Modifier .widthIn(min = 20.dp, max = (if (item.messageType == V2TIMMessage.V2TIM_ELEM_TYPE_TEXT) 250.dp else 150.dp)) @@ -314,24 +306,6 @@ fun ChatOtherItem(item: ChatItem) { } Spacer(modifier = Modifier.width(12.dp)) Column { - Row() { - Text( - text = item.nickname, - style = TextStyle( - color = Color.Black, - fontSize = 14.sp - ) - ) - Spacer(modifier = Modifier.width(8.dp)) - Text( - text = item.time, - style = TextStyle( - color = Color.Gray, - fontSize = 14.sp - ) - ) - } - Spacer(modifier = Modifier.height(4.dp)) Box( modifier = Modifier .widthIn(min = 20.dp, max = (if (item.messageType == V2TIMMessage.V2TIM_ELEM_TYPE_TEXT) 250.dp else 150.dp)) @@ -512,4 +486,22 @@ fun ChatInput( ) } } +} + +fun groupMessagesByTime(chatList: List, viewModel: ChatViewModel) { + if (chatList.isEmpty()) return + + var lastTimestamp = chatList[0].timestamp // Use the first message's timestamp + viewModel.showTimestampMap[chatList.last().msgId] = true + Log.d("TimestampDebug", "Setting showTimestamp for ${chatList.last().msgId} to true") + + for (i in 1 until chatList.size) { // Iterate in normal order + val currentMessage = chatList[i] + val timeDiff = currentMessage.timestamp - lastTimestamp + + if (timeDiff > 60 * 1000) { + viewModel.showTimestampMap[currentMessage.msgId] = true + lastTimestamp = currentMessage.timestamp + } + } } \ No newline at end of file diff --git a/app/src/main/java/com/aiosman/riderpro/ui/chat/ChatViewModel.kt b/app/src/main/java/com/aiosman/riderpro/ui/chat/ChatViewModel.kt index b9c6797..47b4eea 100644 --- a/app/src/main/java/com/aiosman/riderpro/ui/chat/ChatViewModel.kt +++ b/app/src/main/java/com/aiosman/riderpro/ui/chat/ChatViewModel.kt @@ -40,7 +40,9 @@ data class ChatItem( val timestamp: Long = 0, val imageList: MutableList = emptyList().toMutableList(), val messageType : Int = 0, - val textDisplay : String = "" + val textDisplay : String = "", + val msgId: String, // Add this property + var showTimestamp: Boolean = false ) class ChatViewModel( @@ -55,6 +57,7 @@ class ChatViewModel( var hasMore by mutableStateOf(true) var isLoading by mutableStateOf(false) var lastMessage: V2TIMMessage? = null + val showTimestampMap = mutableMapOf() // Add this map fun init(context: Context) { // 获取用户信息 viewModelScope.launch { @@ -67,6 +70,7 @@ class ChatViewModel( } } + fun RegistListener(context: Context) { textMessageListener = object : V2TIMAdvancedMsgListener() { override fun onRecvNewMessage(msg: V2TIMMessage?) { @@ -131,7 +135,8 @@ class ChatViewModel( imageList = message.imageElem?.imageList ?: emptyList().toMutableList(), messageType = V2TIMMessage.V2TIM_ELEM_TYPE_IMAGE, - textDisplay = "Image" + textDisplay = "Image", + msgId = message.msgID // Add this line to include msgId ) } return null @@ -149,7 +154,8 @@ class ChatViewModel( imageList = imageElm?.toMutableList() ?: emptyList().toMutableList(), messageType = V2TIMMessage.V2TIM_ELEM_TYPE_TEXT, - textDisplay = message.textElem?.text ?: "Unsupported message type" + textDisplay = message.textElem?.text ?: "Unsupported message type", + msgId = message.msgID // Add this line to include msgId ) } @@ -304,7 +310,12 @@ class ChatViewModel( } fun getDisplayChatList(): List { - return chatData + val list = chatData + // Update showTimestamp for each message + for (item in list) { + item.showTimestamp = showTimestampMap.getOrDefault(item.msgId, false) + } + return list } } \ No newline at end of file