新消息时自动刷新
This commit is contained in:
@@ -5,8 +5,10 @@ import android.content.Intent
|
||||
import android.icu.util.Calendar
|
||||
import android.icu.util.TimeZone
|
||||
import android.util.Log
|
||||
import com.aiosman.riderpro.data.AccountProfile
|
||||
import com.aiosman.riderpro.data.AccountService
|
||||
import com.aiosman.riderpro.data.AccountServiceImpl
|
||||
import com.aiosman.riderpro.entity.AccountProfileEntity
|
||||
import com.aiosman.riderpro.ui.favourite.FavouriteListViewModel
|
||||
import com.aiosman.riderpro.ui.favourite.FavouriteNoticeViewModel
|
||||
import com.aiosman.riderpro.ui.follower.FollowerNoticeViewModel
|
||||
@@ -30,6 +32,7 @@ import kotlin.coroutines.suspendCoroutine
|
||||
|
||||
object AppState {
|
||||
var UserId: Int? = null
|
||||
var profile :AccountProfileEntity? = null
|
||||
|
||||
suspend fun initWithAccount(scope: CoroutineScope, context: Context) {
|
||||
val accountService: AccountService = AccountServiceImpl()
|
||||
@@ -47,6 +50,9 @@ object AppState {
|
||||
)
|
||||
// 设置当前登录用户 ID
|
||||
UserId = resp.id
|
||||
var profileResult = accountService.getMyAccountProfile()
|
||||
profile = profileResult
|
||||
// 获取当前用户资料
|
||||
|
||||
// 注册 JPush
|
||||
Messaging.registerDevice(scope, context)
|
||||
|
||||
@@ -15,6 +15,7 @@ import androidx.core.app.ActivityCompat
|
||||
import androidx.core.app.NotificationCompat
|
||||
import androidx.core.app.NotificationManagerCompat
|
||||
import com.aiosman.riderpro.entity.ChatItem
|
||||
import com.aiosman.riderpro.ui.index.tabs.message.MessageListViewModel
|
||||
import com.tencent.imsdk.v2.V2TIMAdvancedMsgListener
|
||||
import com.tencent.imsdk.v2.V2TIMManager
|
||||
import com.tencent.imsdk.v2.V2TIMMessage
|
||||
@@ -53,6 +54,7 @@ class TrtcService : Service() {
|
||||
override fun onRecvNewMessage(msg: V2TIMMessage?) {
|
||||
super.onRecvNewMessage(msg)
|
||||
msg?.let {
|
||||
MessageListViewModel.refreshConversation(context, it.sender)
|
||||
if (MainActivityLifecycle.isForeground) {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import androidx.navigation.NavController
|
||||
import androidx.navigation.NavHostController
|
||||
import androidx.paging.PagingData
|
||||
import androidx.paging.map
|
||||
import com.aiosman.riderpro.AppState
|
||||
import com.aiosman.riderpro.data.AccountNotice
|
||||
import com.aiosman.riderpro.data.AccountService
|
||||
import com.aiosman.riderpro.entity.CommentEntity
|
||||
@@ -32,6 +33,7 @@ import kotlinx.coroutines.launch
|
||||
import kotlin.coroutines.suspendCoroutine
|
||||
|
||||
data class Conversation(
|
||||
val id: String,
|
||||
val trtcUserId: String,
|
||||
val nickname: String,
|
||||
val lastMessage: String,
|
||||
@@ -40,7 +42,37 @@ data class Conversation(
|
||||
val unreadCount: Int = 0,
|
||||
val displayText: String,
|
||||
val isSelf: Boolean
|
||||
)
|
||||
) {
|
||||
companion object {
|
||||
fun convertToConversation(msg: V2TIMConversation, context: Context): Conversation {
|
||||
val lastMessage = Calendar.getInstance().apply {
|
||||
timeInMillis = msg.lastMessage?.timestamp ?: 0
|
||||
timeInMillis *= 1000
|
||||
}
|
||||
var displayText = ""
|
||||
when (msg.lastMessage?.elemType) {
|
||||
V2TIMMessage.V2TIM_ELEM_TYPE_TEXT -> {
|
||||
displayText = msg.lastMessage?.textElem?.text ?: ""
|
||||
}
|
||||
|
||||
V2TIMMessage.V2TIM_ELEM_TYPE_IMAGE -> {
|
||||
displayText = "[图片]"
|
||||
}
|
||||
}
|
||||
return Conversation(
|
||||
id = msg.conversationID,
|
||||
nickname = msg.showName,
|
||||
lastMessage = msg.lastMessage?.textElem?.text ?: "",
|
||||
lastMessageTime = lastMessage.time.formatChatTime(context),
|
||||
avatar = msg.faceUrl,
|
||||
unreadCount = msg.unreadCount,
|
||||
trtcUserId = msg.userID,
|
||||
displayText = displayText,
|
||||
isSelf = msg.lastMessage.sender == AppState.profile?.trtcUserId
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
object MessageListViewModel : ViewModel() {
|
||||
val accountService: AccountService = AccountServiceImpl()
|
||||
@@ -133,29 +165,7 @@ object MessageListViewModel : ViewModel() {
|
||||
}
|
||||
|
||||
chatList = result?.conversationList?.map { msg: V2TIMConversation ->
|
||||
val lastMessage = Calendar.getInstance().apply {
|
||||
timeInMillis = msg.lastMessage?.timestamp ?: 0
|
||||
timeInMillis *= 1000
|
||||
}
|
||||
var displayText = ""
|
||||
when (msg.lastMessage?.elemType) {
|
||||
V2TIMMessage.V2TIM_ELEM_TYPE_TEXT -> {
|
||||
displayText = msg.lastMessage?.textElem?.text ?: ""
|
||||
}
|
||||
V2TIMMessage.V2TIM_ELEM_TYPE_IMAGE -> {
|
||||
displayText = "[图片]"
|
||||
}
|
||||
}
|
||||
Conversation(
|
||||
nickname = msg.showName,
|
||||
lastMessage = msg.lastMessage?.textElem?.text ?: "",
|
||||
lastMessageTime = lastMessage.time.formatChatTime(context),
|
||||
avatar = msg.faceUrl,
|
||||
unreadCount = msg.unreadCount,
|
||||
trtcUserId = msg.userID,
|
||||
displayText = displayText,
|
||||
isSelf = msg.lastMessage.sender == MyProfileViewModel.profile?.trtcUserId
|
||||
)
|
||||
Conversation.convertToConversation(msg, context)
|
||||
} ?: emptyList()
|
||||
}
|
||||
|
||||
@@ -184,8 +194,20 @@ object MessageListViewModel : ViewModel() {
|
||||
) {
|
||||
viewModelScope.launch {
|
||||
val profile = userService.getUserProfileByTrtcUserId(conversation.trtcUserId)
|
||||
navController.navigate(NavigationRoute.AccountProfile.route.replace("{id}", profile.id.toString()))
|
||||
navController.navigate(
|
||||
NavigationRoute.AccountProfile.route.replace(
|
||||
"{id}",
|
||||
profile.id.toString()
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun refreshConversation(context: Context, userId: String) {
|
||||
viewModelScope.launch {
|
||||
loadChatList(context)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user