fix: 修复聊天界面发送消息时消息无法正确显示在屏幕上

This commit is contained in:
糕小菜 2024-12-18 22:45:24 +08:00
parent 0785dc60d5
commit c5b16664bc

View File

@ -114,13 +114,14 @@ class ChatActivity : BaseActivity<ActivityChatBinding>(), OnItemClickListener,
super.onCreate(savedInstanceState)
enableEdgeToEdge()
EventBus.getDefault().register(this)
observeLiveData()
firstLoadData()
initView()
setListener()
bindWebSocketService()
setPanelChange()
getKeyBoardVisibility()
observeViewModel()
observeStateFlow()
if (isSearchHistory) {
val size = MessagesManager.queryHistory(msgLocalId)
binding.recycleChatList.smoothScrollToPosition(size - 1)
@ -134,10 +135,13 @@ class ChatActivity : BaseActivity<ActivityChatBinding>(), OnItemClickListener,
}
}
private fun observeViewModel() {
private fun observeStateFlow() {
lifecycleScope.launch {
MessagesManager.messages.collect {
chatAdapter?.submitList(it)
binding.recycleChatList.post {
binding.recycleChatList.smoothScrollToPosition(0)
}
}
}
}
@ -147,7 +151,6 @@ class ChatActivity : BaseActivity<ActivityChatBinding>(), OnItemClickListener,
val binder = service as LocalBinder
webSocketService = binder.getService()
bound = true
observeLiveData()
}
override fun onServiceDisconnected(arg0: ComponentName) {
@ -455,7 +458,6 @@ class ChatActivity : BaseActivity<ActivityChatBinding>(), OnItemClickListener,
webSocketService!!.storeOwnerMsg(messages)
MessagesManager.sendMessages(messages)
binding.recycleChatList.smoothScrollToPosition(0)
binding.etInput.setText("")
}