聊天时间
This commit is contained in:
@@ -3,6 +3,7 @@ package com.aiosman.riderpro.ui.chat
|
|||||||
import android.app.Activity
|
import android.app.Activity
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
|
import android.util.Log
|
||||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||||
import androidx.activity.result.contract.ActivityResultContracts
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
import androidx.compose.animation.Crossfade
|
import androidx.compose.animation.Crossfade
|
||||||
@@ -188,14 +189,25 @@ fun ChatScreen(userId: String) {
|
|||||||
verticalArrangement = Arrangement.Top
|
verticalArrangement = Arrangement.Top
|
||||||
) {
|
) {
|
||||||
val chatList = viewModel.getDisplayChatList()
|
val chatList = viewModel.getDisplayChatList()
|
||||||
|
groupMessagesByTime(chatList, viewModel) // Pass viewModel to the function
|
||||||
items(chatList.size) { index ->
|
items(chatList.size) { index ->
|
||||||
Box(
|
val item = chatList[index]
|
||||||
modifier = Modifier
|
if (item.showTimestamp) {
|
||||||
.fillMaxWidth()
|
val calendar = java.util.Calendar.getInstance()
|
||||||
.padding(8.dp)
|
calendar.timeInMillis = item.timestamp
|
||||||
) {
|
Text(
|
||||||
ChatItem(item = chatList[index], viewModel.myProfile?.trtcUserId!!)
|
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(
|
Column(
|
||||||
horizontalAlignment = androidx.compose.ui.Alignment.End,
|
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(
|
Box(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.widthIn(min = 20.dp, max = (if (item.messageType == V2TIMMessage.V2TIM_ELEM_TYPE_TEXT) 250.dp else 150.dp))
|
.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))
|
Spacer(modifier = Modifier.width(12.dp))
|
||||||
Column {
|
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(
|
Box(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.widthIn(min = 20.dp, max = (if (item.messageType == V2TIMMessage.V2TIM_ELEM_TYPE_TEXT) 250.dp else 150.dp))
|
.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<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
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -40,7 +40,9 @@ data class ChatItem(
|
|||||||
val timestamp: Long = 0,
|
val timestamp: Long = 0,
|
||||||
val imageList: MutableList<V2TIMImageElem.V2TIMImage> = emptyList<V2TIMImageElem.V2TIMImage>().toMutableList(),
|
val imageList: MutableList<V2TIMImageElem.V2TIMImage> = emptyList<V2TIMImageElem.V2TIMImage>().toMutableList(),
|
||||||
val messageType : Int = 0,
|
val messageType : Int = 0,
|
||||||
val textDisplay : String = ""
|
val textDisplay : String = "",
|
||||||
|
val msgId: String, // Add this property
|
||||||
|
var showTimestamp: Boolean = false
|
||||||
)
|
)
|
||||||
|
|
||||||
class ChatViewModel(
|
class ChatViewModel(
|
||||||
@@ -55,6 +57,7 @@ class ChatViewModel(
|
|||||||
var hasMore by mutableStateOf(true)
|
var hasMore by mutableStateOf(true)
|
||||||
var isLoading by mutableStateOf(false)
|
var isLoading by mutableStateOf(false)
|
||||||
var lastMessage: V2TIMMessage? = null
|
var lastMessage: V2TIMMessage? = null
|
||||||
|
val showTimestampMap = mutableMapOf<String, Boolean>() // Add this map
|
||||||
fun init(context: Context) {
|
fun init(context: Context) {
|
||||||
// 获取用户信息
|
// 获取用户信息
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
@@ -67,6 +70,7 @@ class ChatViewModel(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fun RegistListener(context: Context) {
|
fun RegistListener(context: Context) {
|
||||||
textMessageListener = object : V2TIMAdvancedMsgListener() {
|
textMessageListener = object : V2TIMAdvancedMsgListener() {
|
||||||
override fun onRecvNewMessage(msg: V2TIMMessage?) {
|
override fun onRecvNewMessage(msg: V2TIMMessage?) {
|
||||||
@@ -131,7 +135,8 @@ class ChatViewModel(
|
|||||||
imageList = message.imageElem?.imageList
|
imageList = message.imageElem?.imageList
|
||||||
?: emptyList<V2TIMImageElem.V2TIMImage>().toMutableList(),
|
?: emptyList<V2TIMImageElem.V2TIMImage>().toMutableList(),
|
||||||
messageType = V2TIMMessage.V2TIM_ELEM_TYPE_IMAGE,
|
messageType = V2TIMMessage.V2TIM_ELEM_TYPE_IMAGE,
|
||||||
textDisplay = "Image"
|
textDisplay = "Image",
|
||||||
|
msgId = message.msgID // Add this line to include msgId
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
return null
|
return null
|
||||||
@@ -149,7 +154,8 @@ class ChatViewModel(
|
|||||||
imageList = imageElm?.toMutableList()
|
imageList = imageElm?.toMutableList()
|
||||||
?: emptyList<V2TIMImageElem.V2TIMImage>().toMutableList(),
|
?: emptyList<V2TIMImageElem.V2TIMImage>().toMutableList(),
|
||||||
messageType = V2TIMMessage.V2TIM_ELEM_TYPE_TEXT,
|
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> {
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user