聊天时间

This commit is contained in:
2024-10-10 01:16:49 +08:00
parent 6daaa0758c
commit 4c5a9dfe5c
2 changed files with 51 additions and 48 deletions

View File

@@ -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(
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(8.dp)
) {
ChatItem(item = chatList[index], viewModel.myProfile?.trtcUserId!!)
.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))
@@ -513,3 +487,21 @@ 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
val currentMessage = chatList[i]
val timeDiff = currentMessage.timestamp - lastTimestamp
if (timeDiff > 60 * 1000) {
viewModel.showTimestampMap[currentMessage.msgId] = true
lastTimestamp = currentMessage.timestamp
}
}
}

View File

@@ -40,7 +40,9 @@ data class ChatItem(
val timestamp: Long = 0,
val imageList: MutableList<V2TIMImageElem.V2TIMImage> = emptyList<V2TIMImageElem.V2TIMImage>().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<String, Boolean>() // 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<V2TIMImageElem.V2TIMImage>().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<V2TIMImageElem.V2TIMImage>().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<ChatItem> {
return chatData
val list = chatData
// Update showTimestamp for each message
for (item in list) {
item.showTimestamp = showTimestampMap.getOrDefault(item.msgId, false)
}
return list
}
}