From 6c69a769c59e71bce1d71c8d2d1e7ac01978cdb9 Mon Sep 17 00:00:00 2001 From: AllenTom Date: Thu, 10 Oct 2024 15:09:22 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E8=81=8A=E5=A4=A9=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E6=98=BE=E7=A4=BA=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../aiosman/riderpro/ui/chat/ChatScreen.kt | 36 ++++++++++--------- .../aiosman/riderpro/ui/chat/ChatViewModel.kt | 9 +++-- 2 files changed, 24 insertions(+), 21 deletions(-) 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 d8be418..74b7ce0 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 @@ -193,11 +193,10 @@ fun ChatScreen(userId: String) { reverseLayout = true, verticalArrangement = Arrangement.Top ) { - val chatList = viewModel.getDisplayChatList() - groupMessagesByTime(chatList, viewModel) // Pass viewModel to the function + val chatList = groupMessagesByTime(viewModel.getDisplayChatList(), viewModel) items(chatList.size, key = { index -> chatList[index].msgId }) { index -> val item = chatList[index] - if (item.showTimestamp) { + if (item.showTimeDivider) { val calendar = java.util.Calendar.getInstance() calendar.timeInMillis = item.timestamp Text( @@ -258,6 +257,7 @@ fun ChatSelfItem(item: ChatItem) { textAlign = TextAlign.Start ) } + V2TIMMessage.V2TIM_ELEM_TYPE_IMAGE -> { CustomAsyncImage( imageUrl = item.imageList[1].url, @@ -265,6 +265,7 @@ fun ChatSelfItem(item: ChatItem) { contentDescription = "image" ) } + else -> { Text( text = "Unsupported message type", @@ -342,6 +343,7 @@ fun ChatOtherItem(item: ChatItem) { textAlign = TextAlign.Start ) } + V2TIMMessage.V2TIM_ELEM_TYPE_IMAGE -> { CustomAsyncImage( imageUrl = item.imageList[1].url, @@ -349,6 +351,7 @@ fun ChatOtherItem(item: ChatItem) { contentDescription = "image" ) } + else -> { Text( text = "Unsupported message type", @@ -485,7 +488,8 @@ fun ChatInput( tint = Color(0xffe0e0e0) ) Spacer(modifier = Modifier.width(8.dp)) - Crossfade(targetState = text.isNotEmpty(), animationSpec = tween(500), + Crossfade( + targetState = text.isNotEmpty(), animationSpec = tween(500), label = "" ) { isNotEmpty -> Icon( @@ -505,20 +509,20 @@ 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 +fun groupMessagesByTime(chatList: List, viewModel: ChatViewModel): List { + for (i in chatList.indices) { // Iterate in normal order + if (i == 0) { + viewModel.showTimestampMap[chatList[i].msgId] = true + chatList[i].showTimeDivider = true + continue + } val currentMessage = chatList[i] - val timeDiff = currentMessage.timestamp - lastTimestamp - - if (timeDiff > 60 * 1000) { + val timeDiff = currentMessage.timestamp - chatList[i - 1].timestamp + // 时间间隔大于 3 分钟,显示时间戳 + if (-timeDiff > 3 * 60 * 1000) { viewModel.showTimestampMap[currentMessage.msgId] = true - lastTimestamp = currentMessage.timestamp + currentMessage.showTimeDivider = true } } + return chatList } \ 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 47b4eea..05256f0 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 @@ -42,7 +42,8 @@ data class ChatItem( val messageType : Int = 0, val textDisplay : String = "", val msgId: String, // Add this property - var showTimestamp: Boolean = false + var showTimestamp: Boolean = false, + var showTimeDivider:Boolean = false ) class ChatViewModel( @@ -140,7 +141,6 @@ class ChatViewModel( ) } return null - } V2TIMMessage.V2TIM_ELEM_TYPE_TEXT -> { @@ -292,9 +292,9 @@ class ChatViewModel( null, object : V2TIMValueCallback> { override fun onSuccess(p0: List?) { - chatData = (p0 ?: emptyList()).map { + chatData = (p0 ?: emptyList()).mapNotNull { convertToChatItem(it, context) - }.filterNotNull() + } if ((p0?.size ?: 0) < 20) { hasMore = false } @@ -317,5 +317,4 @@ class ChatViewModel( } return list } - } \ No newline at end of file