修正聊天时间显示问题
This commit is contained in:
87
app/src/main/java/com/aiosman/riderpro/entity/Chat.kt
Normal file
87
app/src/main/java/com/aiosman/riderpro/entity/Chat.kt
Normal file
@@ -0,0 +1,87 @@
|
|||||||
|
package com.aiosman.riderpro.entity
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.icu.util.Calendar
|
||||||
|
import com.aiosman.riderpro.exp.formatChatTime
|
||||||
|
import com.tencent.imsdk.v2.V2TIMImageElem
|
||||||
|
import com.tencent.imsdk.v2.V2TIMMessage
|
||||||
|
|
||||||
|
data class ChatItem(
|
||||||
|
val message: String,
|
||||||
|
val avatar: String,
|
||||||
|
val time: String,
|
||||||
|
val userId: String,
|
||||||
|
val nickname: String,
|
||||||
|
val timeCategory: String = "",
|
||||||
|
val timestamp: Long = 0,
|
||||||
|
val imageList: MutableList<V2TIMImageElem.V2TIMImage> = emptyList<V2TIMImageElem.V2TIMImage>().toMutableList(),
|
||||||
|
val messageType : Int = 0,
|
||||||
|
val textDisplay : String = "",
|
||||||
|
val msgId: String, // Add this property
|
||||||
|
var showTimestamp: Boolean = false,
|
||||||
|
var showTimeDivider:Boolean = false
|
||||||
|
) {
|
||||||
|
companion object {
|
||||||
|
fun convertToChatItem(message: V2TIMMessage, context: Context): ChatItem? {
|
||||||
|
// val avatar = if (message.sender == userProfile?.trtcUserId) {
|
||||||
|
// userProfile?.avatar ?: ""
|
||||||
|
// } else {
|
||||||
|
// myProfile?.avatar ?: ""
|
||||||
|
// }
|
||||||
|
// val nickname = if (message.sender == userProfile?.trtcUserId) {
|
||||||
|
// userProfile?.nickName ?: ""
|
||||||
|
// } else {
|
||||||
|
// myProfile?.nickName ?: ""
|
||||||
|
// }
|
||||||
|
val timestamp = message.timestamp
|
||||||
|
val calendar = Calendar.getInstance()
|
||||||
|
calendar.timeInMillis = timestamp * 1000
|
||||||
|
val imageElm = message.imageElem?.imageList
|
||||||
|
when (message.elemType) {
|
||||||
|
V2TIMMessage.V2TIM_ELEM_TYPE_IMAGE -> {
|
||||||
|
val imageElm = message.imageElem?.imageList?.all {
|
||||||
|
it.size == 0
|
||||||
|
}
|
||||||
|
if (imageElm != true) {
|
||||||
|
return ChatItem(
|
||||||
|
message = "Image",
|
||||||
|
avatar = message.faceUrl,
|
||||||
|
time = calendar.time.formatChatTime(context),
|
||||||
|
userId = message.sender,
|
||||||
|
nickname = message.nickName,
|
||||||
|
timestamp = timestamp * 1000,
|
||||||
|
imageList = message.imageElem?.imageList
|
||||||
|
?: emptyList<V2TIMImageElem.V2TIMImage>().toMutableList(),
|
||||||
|
messageType = V2TIMMessage.V2TIM_ELEM_TYPE_IMAGE,
|
||||||
|
textDisplay = "Image",
|
||||||
|
msgId = message.msgID // Add this line to include msgId
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
V2TIMMessage.V2TIM_ELEM_TYPE_TEXT -> {
|
||||||
|
return ChatItem(
|
||||||
|
message = message.textElem?.text ?: "Unsupported message type",
|
||||||
|
avatar = message.faceUrl,
|
||||||
|
time = calendar.time.formatChatTime(context),
|
||||||
|
userId = message.sender,
|
||||||
|
nickname = message.nickName,
|
||||||
|
timestamp = timestamp * 1000,
|
||||||
|
imageList = imageElm?.toMutableList()
|
||||||
|
?: emptyList<V2TIMImageElem.V2TIMImage>().toMutableList(),
|
||||||
|
messageType = V2TIMMessage.V2TIM_ELEM_TYPE_TEXT,
|
||||||
|
textDisplay = message.textElem?.text ?: "Unsupported message type",
|
||||||
|
msgId = message.msgID // Add this line to include msgId
|
||||||
|
)
|
||||||
|
|
||||||
|
}
|
||||||
|
else -> {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -3,7 +3,6 @@ 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
|
||||||
@@ -35,7 +34,6 @@ import androidx.compose.foundation.shape.RoundedCornerShape
|
|||||||
import androidx.compose.foundation.text.BasicTextField
|
import androidx.compose.foundation.text.BasicTextField
|
||||||
import androidx.compose.foundation.text.KeyboardActions
|
import androidx.compose.foundation.text.KeyboardActions
|
||||||
import androidx.compose.foundation.text.KeyboardOptions
|
import androidx.compose.foundation.text.KeyboardOptions
|
||||||
import androidx.compose.material.CircularProgressIndicator
|
|
||||||
import androidx.compose.material.Icon
|
import androidx.compose.material.Icon
|
||||||
import androidx.compose.material.Scaffold
|
import androidx.compose.material.Scaffold
|
||||||
import androidx.compose.material.Text
|
import androidx.compose.material.Text
|
||||||
@@ -43,7 +41,6 @@ import androidx.compose.runtime.Composable
|
|||||||
import androidx.compose.runtime.DisposableEffect
|
import androidx.compose.runtime.DisposableEffect
|
||||||
import androidx.compose.runtime.LaunchedEffect
|
import androidx.compose.runtime.LaunchedEffect
|
||||||
import androidx.compose.runtime.getValue
|
import androidx.compose.runtime.getValue
|
||||||
import androidx.compose.runtime.key
|
|
||||||
import androidx.compose.runtime.mutableStateOf
|
import androidx.compose.runtime.mutableStateOf
|
||||||
import androidx.compose.runtime.remember
|
import androidx.compose.runtime.remember
|
||||||
import androidx.compose.runtime.rememberCoroutineScope
|
import androidx.compose.runtime.rememberCoroutineScope
|
||||||
@@ -54,7 +51,6 @@ import androidx.compose.ui.Modifier
|
|||||||
import androidx.compose.ui.draw.clip
|
import androidx.compose.ui.draw.clip
|
||||||
import androidx.compose.ui.focus.onFocusChanged
|
import androidx.compose.ui.focus.onFocusChanged
|
||||||
import androidx.compose.ui.graphics.Color
|
import androidx.compose.ui.graphics.Color
|
||||||
import androidx.compose.ui.graphics.ColorFilter
|
|
||||||
import androidx.compose.ui.input.pointer.pointerInput
|
import androidx.compose.ui.input.pointer.pointerInput
|
||||||
import androidx.compose.ui.platform.LocalContext
|
import androidx.compose.ui.platform.LocalContext
|
||||||
import androidx.compose.ui.platform.LocalDensity
|
import androidx.compose.ui.platform.LocalDensity
|
||||||
@@ -72,6 +68,7 @@ import androidx.lifecycle.ViewModelProvider
|
|||||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||||
import com.aiosman.riderpro.LocalNavController
|
import com.aiosman.riderpro.LocalNavController
|
||||||
import com.aiosman.riderpro.R
|
import com.aiosman.riderpro.R
|
||||||
|
import com.aiosman.riderpro.entity.ChatItem
|
||||||
import com.aiosman.riderpro.exp.formatChatTime
|
import com.aiosman.riderpro.exp.formatChatTime
|
||||||
import com.aiosman.riderpro.ui.composables.CustomAsyncImage
|
import com.aiosman.riderpro.ui.composables.CustomAsyncImage
|
||||||
import com.aiosman.riderpro.ui.composables.StatusBarSpacer
|
import com.aiosman.riderpro.ui.composables.StatusBarSpacer
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ import com.aiosman.riderpro.data.AccountServiceImpl
|
|||||||
import com.aiosman.riderpro.data.UserService
|
import com.aiosman.riderpro.data.UserService
|
||||||
import com.aiosman.riderpro.data.UserServiceImpl
|
import com.aiosman.riderpro.data.UserServiceImpl
|
||||||
import com.aiosman.riderpro.entity.AccountProfileEntity
|
import com.aiosman.riderpro.entity.AccountProfileEntity
|
||||||
|
import com.aiosman.riderpro.entity.ChatItem
|
||||||
import com.aiosman.riderpro.exp.formatChatTime
|
import com.aiosman.riderpro.exp.formatChatTime
|
||||||
import com.tencent.imsdk.v2.V2TIMAdvancedMsgListener
|
import com.tencent.imsdk.v2.V2TIMAdvancedMsgListener
|
||||||
import com.tencent.imsdk.v2.V2TIMCallback
|
import com.tencent.imsdk.v2.V2TIMCallback
|
||||||
@@ -30,22 +31,6 @@ import java.io.FileOutputStream
|
|||||||
import java.io.InputStream
|
import java.io.InputStream
|
||||||
|
|
||||||
|
|
||||||
data class ChatItem(
|
|
||||||
val message: String,
|
|
||||||
val avatar: String,
|
|
||||||
val time: String,
|
|
||||||
val userId: String,
|
|
||||||
val nickname: String,
|
|
||||||
val timeCategory: String = "",
|
|
||||||
val timestamp: Long = 0,
|
|
||||||
val imageList: MutableList<V2TIMImageElem.V2TIMImage> = emptyList<V2TIMImageElem.V2TIMImage>().toMutableList(),
|
|
||||||
val messageType : Int = 0,
|
|
||||||
val textDisplay : String = "",
|
|
||||||
val msgId: String, // Add this property
|
|
||||||
var showTimestamp: Boolean = false,
|
|
||||||
var showTimeDivider:Boolean = false
|
|
||||||
)
|
|
||||||
|
|
||||||
class ChatViewModel(
|
class ChatViewModel(
|
||||||
val userId: String,
|
val userId: String,
|
||||||
) : ViewModel() {
|
) : ViewModel() {
|
||||||
@@ -77,7 +62,7 @@ class ChatViewModel(
|
|||||||
override fun onRecvNewMessage(msg: V2TIMMessage?) {
|
override fun onRecvNewMessage(msg: V2TIMMessage?) {
|
||||||
super.onRecvNewMessage(msg)
|
super.onRecvNewMessage(msg)
|
||||||
msg?.let {
|
msg?.let {
|
||||||
val chatItem = convertToChatItem(msg, context)
|
val chatItem = ChatItem.convertToChatItem(msg, context)
|
||||||
chatItem?.let {
|
chatItem?.let {
|
||||||
chatData = listOf(it) + chatData
|
chatData = listOf(it) + chatData
|
||||||
}
|
}
|
||||||
@@ -105,66 +90,7 @@ class ChatViewModel(
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fun convertToChatItem(message: V2TIMMessage, context: Context): ChatItem? {
|
|
||||||
val avatar = if (message.sender == userProfile?.trtcUserId) {
|
|
||||||
userProfile?.avatar ?: ""
|
|
||||||
} else {
|
|
||||||
myProfile?.avatar ?: ""
|
|
||||||
}
|
|
||||||
val nickname = if (message.sender == userProfile?.trtcUserId) {
|
|
||||||
userProfile?.nickName ?: ""
|
|
||||||
} else {
|
|
||||||
myProfile?.nickName ?: ""
|
|
||||||
}
|
|
||||||
val timestamp = message.timestamp
|
|
||||||
val calendar = Calendar.getInstance()
|
|
||||||
calendar.timeInMillis = timestamp * 1000
|
|
||||||
val imageElm = message.imageElem?.imageList
|
|
||||||
when (message.elemType) {
|
|
||||||
V2TIMMessage.V2TIM_ELEM_TYPE_IMAGE -> {
|
|
||||||
val imageElm = message.imageElem?.imageList?.all {
|
|
||||||
it.size == 0
|
|
||||||
}
|
|
||||||
if (imageElm != true) {
|
|
||||||
return ChatItem(
|
|
||||||
message = "Image",
|
|
||||||
avatar = avatar,
|
|
||||||
time = calendar.time.formatChatTime(context),
|
|
||||||
userId = message.sender,
|
|
||||||
nickname = nickname,
|
|
||||||
timestamp = timestamp * 1000,
|
|
||||||
imageList = message.imageElem?.imageList
|
|
||||||
?: emptyList<V2TIMImageElem.V2TIMImage>().toMutableList(),
|
|
||||||
messageType = V2TIMMessage.V2TIM_ELEM_TYPE_IMAGE,
|
|
||||||
textDisplay = "Image",
|
|
||||||
msgId = message.msgID // Add this line to include msgId
|
|
||||||
)
|
|
||||||
}
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
V2TIMMessage.V2TIM_ELEM_TYPE_TEXT -> {
|
|
||||||
return ChatItem(
|
|
||||||
message = message.textElem?.text ?: "Unsupported message type",
|
|
||||||
avatar = avatar,
|
|
||||||
time = calendar.time.formatChatTime(context),
|
|
||||||
userId = message.sender,
|
|
||||||
nickname = nickname,
|
|
||||||
timestamp = timestamp * 1000,
|
|
||||||
imageList = imageElm?.toMutableList()
|
|
||||||
?: emptyList<V2TIMImageElem.V2TIMImage>().toMutableList(),
|
|
||||||
messageType = V2TIMMessage.V2TIM_ELEM_TYPE_TEXT,
|
|
||||||
textDisplay = message.textElem?.text ?: "Unsupported message type",
|
|
||||||
msgId = message.msgID // Add this line to include msgId
|
|
||||||
)
|
|
||||||
|
|
||||||
}
|
|
||||||
else -> {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun onLoadMore(context: Context) {
|
fun onLoadMore(context: Context) {
|
||||||
if (!hasMore || isLoading) {
|
if (!hasMore || isLoading) {
|
||||||
@@ -179,7 +105,7 @@ class ChatViewModel(
|
|||||||
object : V2TIMValueCallback<List<V2TIMMessage>> {
|
object : V2TIMValueCallback<List<V2TIMMessage>> {
|
||||||
override fun onSuccess(p0: List<V2TIMMessage>?) {
|
override fun onSuccess(p0: List<V2TIMMessage>?) {
|
||||||
chatData = chatData + (p0 ?: emptyList()).map {
|
chatData = chatData + (p0 ?: emptyList()).map {
|
||||||
convertToChatItem(it, context)
|
ChatItem.convertToChatItem(it, context)
|
||||||
}.filterNotNull()
|
}.filterNotNull()
|
||||||
if ((p0?.size ?: 0) < 20) {
|
if ((p0?.size ?: 0) < 20) {
|
||||||
hasMore = false
|
hasMore = false
|
||||||
@@ -213,7 +139,7 @@ class ChatViewModel(
|
|||||||
|
|
||||||
override fun onSuccess(p0: V2TIMMessage?) {
|
override fun onSuccess(p0: V2TIMMessage?) {
|
||||||
Log.d("ChatViewModel", "send message success")
|
Log.d("ChatViewModel", "send message success")
|
||||||
val chatItem = convertToChatItem(p0!!, context)
|
val chatItem = ChatItem.convertToChatItem(p0!!, context)
|
||||||
chatItem?.let {
|
chatItem?.let {
|
||||||
chatData = listOf(it) + chatData
|
chatData = listOf(it) + chatData
|
||||||
}
|
}
|
||||||
@@ -245,7 +171,7 @@ class ChatViewModel(
|
|||||||
|
|
||||||
override fun onSuccess(p0: V2TIMMessage?) {
|
override fun onSuccess(p0: V2TIMMessage?) {
|
||||||
Log.d("ChatViewModel", "send image message success")
|
Log.d("ChatViewModel", "send image message success")
|
||||||
val chatItem = convertToChatItem(p0!!, context)
|
val chatItem = ChatItem.convertToChatItem(p0!!, context)
|
||||||
chatItem?.let {
|
chatItem?.let {
|
||||||
chatData = listOf(it) + chatData
|
chatData = listOf(it) + chatData
|
||||||
}
|
}
|
||||||
@@ -293,7 +219,7 @@ class ChatViewModel(
|
|||||||
object : V2TIMValueCallback<List<V2TIMMessage>> {
|
object : V2TIMValueCallback<List<V2TIMMessage>> {
|
||||||
override fun onSuccess(p0: List<V2TIMMessage>?) {
|
override fun onSuccess(p0: List<V2TIMMessage>?) {
|
||||||
chatData = (p0 ?: emptyList()).mapNotNull {
|
chatData = (p0 ?: emptyList()).mapNotNull {
|
||||||
convertToChatItem(it, context)
|
ChatItem.convertToChatItem(it, context)
|
||||||
}
|
}
|
||||||
if ((p0?.size ?: 0) < 20) {
|
if ((p0?.size ?: 0) < 20) {
|
||||||
hasMore = false
|
hasMore = false
|
||||||
|
|||||||
Reference in New Issue
Block a user