修正聊天时间显示问题

This commit is contained in:
2024-10-10 15:09:22 +08:00
parent d39e758e4e
commit 6c69a769c5
2 changed files with 24 additions and 21 deletions

View File

@@ -193,11 +193,10 @@ fun ChatScreen(userId: String) {
reverseLayout = true, reverseLayout = true,
verticalArrangement = Arrangement.Top verticalArrangement = Arrangement.Top
) { ) {
val chatList = viewModel.getDisplayChatList() val chatList = groupMessagesByTime(viewModel.getDisplayChatList(), viewModel)
groupMessagesByTime(chatList, viewModel) // Pass viewModel to the function
items(chatList.size, key = { index -> chatList[index].msgId }) { index -> items(chatList.size, key = { index -> chatList[index].msgId }) { index ->
val item = chatList[index] val item = chatList[index]
if (item.showTimestamp) { if (item.showTimeDivider) {
val calendar = java.util.Calendar.getInstance() val calendar = java.util.Calendar.getInstance()
calendar.timeInMillis = item.timestamp calendar.timeInMillis = item.timestamp
Text( Text(
@@ -258,6 +257,7 @@ fun ChatSelfItem(item: ChatItem) {
textAlign = TextAlign.Start textAlign = TextAlign.Start
) )
} }
V2TIMMessage.V2TIM_ELEM_TYPE_IMAGE -> { V2TIMMessage.V2TIM_ELEM_TYPE_IMAGE -> {
CustomAsyncImage( CustomAsyncImage(
imageUrl = item.imageList[1].url, imageUrl = item.imageList[1].url,
@@ -265,6 +265,7 @@ fun ChatSelfItem(item: ChatItem) {
contentDescription = "image" contentDescription = "image"
) )
} }
else -> { else -> {
Text( Text(
text = "Unsupported message type", text = "Unsupported message type",
@@ -342,6 +343,7 @@ fun ChatOtherItem(item: ChatItem) {
textAlign = TextAlign.Start textAlign = TextAlign.Start
) )
} }
V2TIMMessage.V2TIM_ELEM_TYPE_IMAGE -> { V2TIMMessage.V2TIM_ELEM_TYPE_IMAGE -> {
CustomAsyncImage( CustomAsyncImage(
imageUrl = item.imageList[1].url, imageUrl = item.imageList[1].url,
@@ -349,6 +351,7 @@ fun ChatOtherItem(item: ChatItem) {
contentDescription = "image" contentDescription = "image"
) )
} }
else -> { else -> {
Text( Text(
text = "Unsupported message type", text = "Unsupported message type",
@@ -485,7 +488,8 @@ fun ChatInput(
tint = Color(0xffe0e0e0) tint = Color(0xffe0e0e0)
) )
Spacer(modifier = Modifier.width(8.dp)) Spacer(modifier = Modifier.width(8.dp))
Crossfade(targetState = text.isNotEmpty(), animationSpec = tween(500), Crossfade(
targetState = text.isNotEmpty(), animationSpec = tween(500),
label = "" label = ""
) { isNotEmpty -> ) { isNotEmpty ->
Icon( Icon(
@@ -505,20 +509,20 @@ fun ChatInput(
} }
} }
fun groupMessagesByTime(chatList: List<ChatItem>, viewModel: ChatViewModel) { fun groupMessagesByTime(chatList: List<ChatItem>, viewModel: ChatViewModel): List<ChatItem> {
if (chatList.isEmpty()) return for (i in chatList.indices) { // Iterate in normal order
if (i == 0) {
var lastTimestamp = chatList[0].timestamp // Use the first message's timestamp viewModel.showTimestampMap[chatList[i].msgId] = true
viewModel.showTimestampMap[chatList.last().msgId] = true chatList[i].showTimeDivider = true
Log.d("TimestampDebug", "Setting showTimestamp for ${chatList.last().msgId} to true") continue
}
for (i in 1 until chatList.size) { // Iterate in normal order
val currentMessage = chatList[i] val currentMessage = chatList[i]
val timeDiff = currentMessage.timestamp - lastTimestamp val timeDiff = currentMessage.timestamp - chatList[i - 1].timestamp
// 时间间隔大于 3 分钟,显示时间戳
if (timeDiff > 60 * 1000) { if (-timeDiff > 3 * 60 * 1000) {
viewModel.showTimestampMap[currentMessage.msgId] = true viewModel.showTimestampMap[currentMessage.msgId] = true
lastTimestamp = currentMessage.timestamp currentMessage.showTimeDivider = true
} }
} }
return chatList
} }

View File

@@ -42,7 +42,8 @@ data class ChatItem(
val messageType : Int = 0, val messageType : Int = 0,
val textDisplay : String = "", val textDisplay : String = "",
val msgId: String, // Add this property val msgId: String, // Add this property
var showTimestamp: Boolean = false var showTimestamp: Boolean = false,
var showTimeDivider:Boolean = false
) )
class ChatViewModel( class ChatViewModel(
@@ -140,7 +141,6 @@ class ChatViewModel(
) )
} }
return null return null
} }
V2TIMMessage.V2TIM_ELEM_TYPE_TEXT -> { V2TIMMessage.V2TIM_ELEM_TYPE_TEXT -> {
@@ -292,9 +292,9 @@ class ChatViewModel(
null, null,
object : V2TIMValueCallback<List<V2TIMMessage>> { object : V2TIMValueCallback<List<V2TIMMessage>> {
override fun onSuccess(p0: List<V2TIMMessage>?) { override fun onSuccess(p0: List<V2TIMMessage>?) {
chatData = (p0 ?: emptyList()).map { chatData = (p0 ?: emptyList()).mapNotNull {
convertToChatItem(it, context) convertToChatItem(it, context)
}.filterNotNull() }
if ((p0?.size ?: 0) < 20) { if ((p0?.size ?: 0) < 20) {
hasMore = false hasMore = false
} }
@@ -317,5 +317,4 @@ class ChatViewModel(
} }
return list return list
} }
} }