Forráskód Böngészése

1、修复聊天消息列表没有显示本人发送的消息;

yzs 4 éve
szülő
commit
3dd6d1be01

+ 22 - 11
uchat_im/src/main/java/com/shanp/youqi/im/fragment/MessageListOnlyUserFragment.kt

@@ -11,6 +11,7 @@ import com.shanp.youqi.common.vo.im.ConversationListVo
 import com.shanp.youqi.common.widget.EmptyView
 import com.shanp.youqi.core.event.IMReadMsgEvent
 import com.shanp.youqi.core.event.IMReceivedMessageEvent
+import com.shanp.youqi.core.event.IMSendMessageEvent
 import com.shanp.youqi.core.memory.AppManager
 import com.shanp.youqi.im.R
 import com.shanp.youqi.im.adapter.MessageListOnlyUserAdapter
@@ -18,6 +19,7 @@ import com.shanp.youqi.im.databinding.ImFragmentMessageListOnlyUserBinding
 import com.shanp.youqi.im.dialog.MessageListDialog
 import io.rong.imlib.RongIMClient
 import io.rong.imlib.model.Conversation
+import io.rong.imlib.model.Message
 import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.GlobalScope
 import kotlinx.coroutines.launch
@@ -45,7 +47,7 @@ class MessageListOnlyUserFragment : UChatFragment() {
     override fun initEventAndData(view: View) {
         binding = ImFragmentMessageListOnlyUserBinding.bind(view)
         initTitleBar(view).apply {
-            titleView.textSize =  17f
+            titleView.textSize = 17f
         }
         initRecyclerView()
         initListener()
@@ -75,19 +77,18 @@ class MessageListOnlyUserFragment : UChatFragment() {
             override fun onReceive(event: IMReceivedMessageEvent) {
                 if (!AppManager.get().isConnectRongCloud) return
                 event.message?.let {
-                    val conversationType = it.conversationType
-                    if (conversationType == Conversation.ConversationType.PRIVATE) {
-
-                        GlobalScope.launch {
-                            withContext(Dispatchers.IO) {
-                                disposePrivateType(it.targetId)
-                            }
-                        }
-                    }
+                    disposeMessage(it)
                 }
             }
         })
 
+        register(RxBus.get().toFlowable(IMSendMessageEvent::class.java), object : EventSubscriber<IMSendMessageEvent>() {
+            override fun onReceive(event: IMSendMessageEvent) {
+                super.onReceive(event)
+                event.message?.let { disposeMessage(it) }
+            }
+        })
+
         binding.srl.setOnLoadMoreListener {
             GlobalScope.launch(Dispatchers.Main) {
                 withContext(Dispatchers.IO) {
@@ -101,7 +102,7 @@ class MessageListOnlyUserFragment : UChatFragment() {
                 it.unreadMessageCount = 0
                 adapter.notifyItemChanged(position, MessageListOnlyUserAdapter.REFRESH_UNREAD_COUNT)
                 clearMessagesUnreadStatus(it.conversationType, it.targetId)
-                messageListDialog.addConversation(it.targetId,it.senderUserName,it.portraitUrl)
+                messageListDialog.addConversation(it.targetId, it.senderUserName, it.portraitUrl)
 //                val conversationDialog = ConversationDialog()
 //                conversationDialog.targetId = it.targetId
 //                conversationDialog.targetUserName = it.senderUserName
@@ -118,6 +119,16 @@ class MessageListOnlyUserFragment : UChatFragment() {
         }
     }
 
+    private fun disposeMessage(message: Message) {
+        if (message.conversationType == Conversation.ConversationType.PRIVATE) {
+            GlobalScope.launch {
+                withContext(Dispatchers.IO) {
+                    disposePrivateType(message.targetId)
+                }
+            }
+        }
+    }
+
     private fun loadPrivateConversation() {
         RongIMClient.getInstance().getConversationListByPage(loadPrivateConversation, timeStamp, count, Conversation.ConversationType.PRIVATE)
     }

+ 18 - 14
uchat_im/src/main/java/com/shanp/youqi/im/fragment/UChatConversationOnlyTextFragment.kt

@@ -135,20 +135,7 @@ class UChatConversationOnlyTextFragment : UChatFragment() {
             override fun onReceive(event: IMReceivedMessageEvent) {
                 event.message?.let { message ->
                     if (message.senderUserId == targetId) {
-                        if (message.content is TextMessage) {
-                            val vo = ConversationItemVo()
-                            vo.msgType = MsgType.TYPE_TEXT
-                            vo.message = message
-                            message.messageDirection = Message.MessageDirection.RECEIVE
-                            val isReceive = message.messageDirection == Message.MessageDirection.RECEIVE
-                            vo.avatarUrl = if (isReceive) targetHeadImg else selfHeadImg
-                            adapter.addData(vo)
-                            val lastVisibleItemPosition: Int = layoutManager.findLastVisibleItemPosition()
-                            val size: Int = adapter.data.size
-                            if (lastVisibleItemPosition > size - Constant.conversation.LOAD_HISTORY_MESSAGE_COUNT) {
-                                layoutManager.scrollToPositionWithOffset(adapter.data.lastIndex, Int.MIN_VALUE)
-                            }
-                        }
+                        disposeMessage(message)
                     }
                 }
             }
@@ -185,6 +172,23 @@ class UChatConversationOnlyTextFragment : UChatFragment() {
         })
     }
 
+    fun disposeMessage(message: Message) {
+        if (message.content is TextMessage) {
+            val vo = ConversationItemVo()
+            vo.msgType = MsgType.TYPE_TEXT
+            vo.message = message
+            message.messageDirection = Message.MessageDirection.RECEIVE
+            val isReceive = message.messageDirection == Message.MessageDirection.RECEIVE
+            vo.avatarUrl = if (isReceive) targetHeadImg else selfHeadImg
+            adapter.addData(vo)
+            val lastVisibleItemPosition: Int = layoutManager.findLastVisibleItemPosition()
+            val size: Int = adapter.data.size
+            if (lastVisibleItemPosition > size - Constant.conversation.LOAD_HISTORY_MESSAGE_COUNT) {
+                layoutManager.scrollToPositionWithOffset(adapter.data.lastIndex, Int.MIN_VALUE)
+            }
+        }
+    }
+
     private fun initRecyclerView() {
         binding.rcv.layoutManager = layoutManager
         binding.rcv.addItemDecoration(object : RecyclerView.ItemDecoration() {