新消息时自动刷新
This commit is contained in:
@@ -5,8 +5,10 @@ import android.content.Intent
|
|||||||
import android.icu.util.Calendar
|
import android.icu.util.Calendar
|
||||||
import android.icu.util.TimeZone
|
import android.icu.util.TimeZone
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
|
import com.aiosman.riderpro.data.AccountProfile
|
||||||
import com.aiosman.riderpro.data.AccountService
|
import com.aiosman.riderpro.data.AccountService
|
||||||
import com.aiosman.riderpro.data.AccountServiceImpl
|
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.FavouriteListViewModel
|
||||||
import com.aiosman.riderpro.ui.favourite.FavouriteNoticeViewModel
|
import com.aiosman.riderpro.ui.favourite.FavouriteNoticeViewModel
|
||||||
import com.aiosman.riderpro.ui.follower.FollowerNoticeViewModel
|
import com.aiosman.riderpro.ui.follower.FollowerNoticeViewModel
|
||||||
@@ -30,6 +32,7 @@ import kotlin.coroutines.suspendCoroutine
|
|||||||
|
|
||||||
object AppState {
|
object AppState {
|
||||||
var UserId: Int? = null
|
var UserId: Int? = null
|
||||||
|
var profile :AccountProfileEntity? = null
|
||||||
|
|
||||||
suspend fun initWithAccount(scope: CoroutineScope, context: Context) {
|
suspend fun initWithAccount(scope: CoroutineScope, context: Context) {
|
||||||
val accountService: AccountService = AccountServiceImpl()
|
val accountService: AccountService = AccountServiceImpl()
|
||||||
@@ -47,6 +50,9 @@ object AppState {
|
|||||||
)
|
)
|
||||||
// 设置当前登录用户 ID
|
// 设置当前登录用户 ID
|
||||||
UserId = resp.id
|
UserId = resp.id
|
||||||
|
var profileResult = accountService.getMyAccountProfile()
|
||||||
|
profile = profileResult
|
||||||
|
// 获取当前用户资料
|
||||||
|
|
||||||
// 注册 JPush
|
// 注册 JPush
|
||||||
Messaging.registerDevice(scope, context)
|
Messaging.registerDevice(scope, context)
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import androidx.core.app.ActivityCompat
|
|||||||
import androidx.core.app.NotificationCompat
|
import androidx.core.app.NotificationCompat
|
||||||
import androidx.core.app.NotificationManagerCompat
|
import androidx.core.app.NotificationManagerCompat
|
||||||
import com.aiosman.riderpro.entity.ChatItem
|
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.V2TIMAdvancedMsgListener
|
||||||
import com.tencent.imsdk.v2.V2TIMManager
|
import com.tencent.imsdk.v2.V2TIMManager
|
||||||
import com.tencent.imsdk.v2.V2TIMMessage
|
import com.tencent.imsdk.v2.V2TIMMessage
|
||||||
@@ -53,6 +54,7 @@ class TrtcService : Service() {
|
|||||||
override fun onRecvNewMessage(msg: V2TIMMessage?) {
|
override fun onRecvNewMessage(msg: V2TIMMessage?) {
|
||||||
super.onRecvNewMessage(msg)
|
super.onRecvNewMessage(msg)
|
||||||
msg?.let {
|
msg?.let {
|
||||||
|
MessageListViewModel.refreshConversation(context, it.sender)
|
||||||
if (MainActivityLifecycle.isForeground) {
|
if (MainActivityLifecycle.isForeground) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import androidx.navigation.NavController
|
|||||||
import androidx.navigation.NavHostController
|
import androidx.navigation.NavHostController
|
||||||
import androidx.paging.PagingData
|
import androidx.paging.PagingData
|
||||||
import androidx.paging.map
|
import androidx.paging.map
|
||||||
|
import com.aiosman.riderpro.AppState
|
||||||
import com.aiosman.riderpro.data.AccountNotice
|
import com.aiosman.riderpro.data.AccountNotice
|
||||||
import com.aiosman.riderpro.data.AccountService
|
import com.aiosman.riderpro.data.AccountService
|
||||||
import com.aiosman.riderpro.entity.CommentEntity
|
import com.aiosman.riderpro.entity.CommentEntity
|
||||||
@@ -32,6 +33,7 @@ import kotlinx.coroutines.launch
|
|||||||
import kotlin.coroutines.suspendCoroutine
|
import kotlin.coroutines.suspendCoroutine
|
||||||
|
|
||||||
data class Conversation(
|
data class Conversation(
|
||||||
|
val id: String,
|
||||||
val trtcUserId: String,
|
val trtcUserId: String,
|
||||||
val nickname: String,
|
val nickname: String,
|
||||||
val lastMessage: String,
|
val lastMessage: String,
|
||||||
@@ -40,7 +42,37 @@ data class Conversation(
|
|||||||
val unreadCount: Int = 0,
|
val unreadCount: Int = 0,
|
||||||
val displayText: String,
|
val displayText: String,
|
||||||
val isSelf: Boolean
|
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() {
|
object MessageListViewModel : ViewModel() {
|
||||||
val accountService: AccountService = AccountServiceImpl()
|
val accountService: AccountService = AccountServiceImpl()
|
||||||
@@ -133,29 +165,7 @@ object MessageListViewModel : ViewModel() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
chatList = result?.conversationList?.map { msg: V2TIMConversation ->
|
chatList = result?.conversationList?.map { msg: V2TIMConversation ->
|
||||||
val lastMessage = Calendar.getInstance().apply {
|
Conversation.convertToConversation(msg, context)
|
||||||
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
|
|
||||||
)
|
|
||||||
} ?: emptyList()
|
} ?: emptyList()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -184,8 +194,20 @@ object MessageListViewModel : ViewModel() {
|
|||||||
) {
|
) {
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
val profile = userService.getUserProfileByTrtcUserId(conversation.trtcUserId)
|
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