修正聊天时间显示问题
This commit is contained in:
@@ -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<ChatItem>, 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<ChatItem>, viewModel: ChatViewModel): List<ChatItem> {
|
||||
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
|
||||
}
|
||||
@@ -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<List<V2TIMMessage>> {
|
||||
override fun onSuccess(p0: List<V2TIMMessage>?) {
|
||||
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
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user