chat页显示数据
This commit is contained in:
parent
1e5d8ab709
commit
ae61b72fc3
@ -65,7 +65,7 @@ interface MsgDao {
|
||||
fun getMsgByPage(form: String?, pageSize: Int, page: Int): List<MsgBean?>?
|
||||
|
||||
/**
|
||||
* 查询与单人的聊天记录
|
||||
* 查询与单人的聊天记录-分页
|
||||
*/
|
||||
@Query("SELECT * FROM db_msg t1 WHERE t1.id IN (SELECT id FROM db_msg WHERE `senderId`=(:form) AND `receiverId`=(:to) OR `senderId`=(:to) AND `receiverId`=(:form) ORDER BY timestamp DESC LIMIT (((:page)-1)*(:pageSize)),(:pageSize)) ORDER by t1.timestamp ASC")
|
||||
fun getMsgByFromOrToPage(
|
||||
@ -75,6 +75,15 @@ interface MsgDao {
|
||||
page: Int
|
||||
): List<MsgBean?>?
|
||||
|
||||
/**
|
||||
* 查询与单人的聊天记录-全部
|
||||
*/
|
||||
@Query("SELECT * FROM db_msg t1 WHERE t1.id IN (SELECT id FROM db_msg WHERE senderId=:form AND receiverId=:to OR senderId=:to AND receiverId=:form ORDER BY timestamp DESC) ORDER by t1.timestamp ASC")
|
||||
fun getMsgByFromOrToPage(
|
||||
form: String,
|
||||
to: String,
|
||||
): List<MsgBean>
|
||||
|
||||
/**
|
||||
* 清空与某个人的聊天记录
|
||||
*/
|
||||
|
@ -1,5 +1,6 @@
|
||||
package com.tenlionsoft.aimz_k.page.activity
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
@ -8,6 +9,7 @@ import android.util.Log
|
||||
import android.view.WindowManager
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.databinding.DataBindingUtil
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import com.atwa.filepicker.core.FilePicker
|
||||
import com.tenlionsoft.aimz_k.R
|
||||
@ -16,6 +18,7 @@ import com.tenlionsoft.aimz_k.model.PickerType
|
||||
import com.tenlionsoft.aimz_k.viewmodel.ChatPageViewModel
|
||||
import com.tenlionsoft.baselib.base.BaseActivity
|
||||
import com.tenlionsoft.baselib.contacts.ProjectConfig
|
||||
import com.tenlionsoft.baselib.utils.SpUtils
|
||||
import com.tenlionsoft.baselib.widget.wheel.WheelView
|
||||
|
||||
/**
|
||||
@ -23,17 +26,23 @@ import com.tenlionsoft.baselib.widget.wheel.WheelView
|
||||
*/
|
||||
class ChatActivity : BaseActivity() {
|
||||
private lateinit var mBinding: ActivityChatBinding
|
||||
private val chatPageViewModel: ChatPageViewModel by lazy {
|
||||
ViewModelProvider(this)[ChatPageViewModel::class.java]
|
||||
}
|
||||
private lateinit var mLocalReceiver: LocalReceiver
|
||||
|
||||
private lateinit var mLocalReceiver: LocalReceiver
|
||||
var chatPageViewModel: ChatPageViewModel? = null
|
||||
private val filePicker = FilePicker.getInstance(this)
|
||||
override fun bindView() {
|
||||
mBinding = DataBindingUtil.setContentView(this, R.layout.activity_chat);
|
||||
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
|
||||
window.statusBarColor =
|
||||
ContextCompat.getColor(this, com.tenlionsoft.baselib.R.color.chat_page_bg)
|
||||
val fromId = intent.getStringExtra("fromId")
|
||||
chatPageViewModel = ViewModelProvider(this, object : ViewModelProvider.Factory {
|
||||
override fun <T : ViewModel> create(modelClass: Class<T>): T {
|
||||
return ChatPageViewModel(fromId!!, SpUtils.getId()) as T
|
||||
}
|
||||
})[ChatPageViewModel::class.java]
|
||||
|
||||
|
||||
mBinding.ivBack.setOnClickListener { finish(); }
|
||||
mBinding.srlChats.setEnableRefresh(false)
|
||||
mBinding.srlChats.setEnableLoadMore(false)
|
||||
@ -44,20 +53,20 @@ class ChatActivity : BaseActivity() {
|
||||
mBinding.etMsg.append(it.emoji)
|
||||
}
|
||||
//显示/隐藏软键盘
|
||||
chatPageViewModel.showSoftKeyboard.observe(this) {
|
||||
chatPageViewModel!!.showSoftKeyboard.observe(this) {
|
||||
if (it) {
|
||||
showSoftKeyBoard(mBinding.etMsg)
|
||||
chatPageViewModel.showChooseLayout.value = false
|
||||
chatPageViewModel.showEmojiLayout.value = false
|
||||
chatPageViewModel.showReplyLayout.value = false
|
||||
chatPageViewModel!!.showChooseLayout.value = false
|
||||
chatPageViewModel!!.showEmojiLayout.value = false
|
||||
chatPageViewModel!!.showReplyLayout.value = false
|
||||
} else {
|
||||
hideSoftKeyboard()
|
||||
}
|
||||
}
|
||||
mBinding.rlContent.setOnClickListener {
|
||||
chatPageViewModel.showChooseLayout.value = false
|
||||
chatPageViewModel.showEmojiLayout.value = false
|
||||
chatPageViewModel.showReplyLayout.value = false
|
||||
chatPageViewModel!!.showChooseLayout.value = false
|
||||
chatPageViewModel!!.showEmojiLayout.value = false
|
||||
chatPageViewModel!!.showReplyLayout.value = false
|
||||
hideSoftKeyboard()
|
||||
}
|
||||
mBinding.wvView.setTextSize(14F, isSp = true)
|
||||
@ -67,13 +76,12 @@ class ChatActivity : BaseActivity() {
|
||||
mBinding.etMsg.setText(data.toString())
|
||||
}
|
||||
})
|
||||
chatPageViewModel.chooseType.observe(this) {
|
||||
chatPageViewModel!!.chooseType.observe(this) {
|
||||
when (it) {
|
||||
PickerType.TYPE_PIC -> {
|
||||
filePicker
|
||||
.pickMultiImage { list ->
|
||||
filePicker.pickMultiImage { list ->
|
||||
if (list.isNotEmpty()) {
|
||||
chatPageViewModel.uploadImages(list)
|
||||
chatPageViewModel!!.uploadImages(list)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -81,7 +89,7 @@ class ChatActivity : BaseActivity() {
|
||||
PickerType.TYPE_VIDEO -> {
|
||||
filePicker.pickVideo { mate ->
|
||||
if (mate != null) {
|
||||
chatPageViewModel.uploadVideo(mate)
|
||||
chatPageViewModel!!.uploadVideo(mate)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -89,7 +97,7 @@ class ChatActivity : BaseActivity() {
|
||||
PickerType.TYPE_FILE -> {
|
||||
filePicker.pickFile { meta ->
|
||||
if (meta != null) {
|
||||
chatPageViewModel.uploadFiles(meta)
|
||||
chatPageViewModel!!.uploadFiles(meta)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -97,10 +105,17 @@ class ChatActivity : BaseActivity() {
|
||||
else -> {}
|
||||
}
|
||||
}
|
||||
chatPageViewModel!!.scrollListToBottom.observe(this) {
|
||||
if (it) {
|
||||
mBinding.rlvChats.scrollToPosition(chatPageViewModel!!.adapter.list.size - 1)//滚动到底部
|
||||
chatPageViewModel!!.scrollListToBottom.value = false
|
||||
}
|
||||
}
|
||||
registerLocalReceiver()
|
||||
}
|
||||
|
||||
|
||||
@SuppressLint("UnspecifiedRegisterReceiverFlag")
|
||||
private fun registerLocalReceiver() {
|
||||
mLocalReceiver = LocalReceiver()
|
||||
val intentFilter = IntentFilter()
|
||||
@ -123,7 +138,7 @@ class ChatActivity : BaseActivity() {
|
||||
ProjectConfig.A_S_MSG_RECEIVER -> {
|
||||
val msg = intent.getStringExtra("msg")
|
||||
Log.e("LocalReceiver", "onReceive: ${msg}")
|
||||
chatPageViewModel.receiveMsg(msg)
|
||||
chatPageViewModel!!.receiveMsg(msg)
|
||||
}//接收到信息
|
||||
}
|
||||
}
|
||||
|
@ -27,19 +27,33 @@ import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
class ChatPageViewModel : BaseViewModel() {
|
||||
class ChatPageViewModel(private val fromId: String, private val toId: String) : BaseViewModel() {
|
||||
val txtMsg = MutableLiveData<String>("")
|
||||
val showSendBtn = MutableLiveData(false)//显示/隐藏发送按钮
|
||||
val showEmojiLayout = MutableLiveData(false)//显示/隐藏emoji
|
||||
val showReplyLayout = MutableLiveData(false)//显示/隐藏快速回复
|
||||
val showChooseLayout = MutableLiveData(false)//显示/隐藏选择
|
||||
val chooseType = MutableLiveData<PickerType>()//选择文件类型
|
||||
val scrollListToBottom = MutableLiveData<Boolean>(false)
|
||||
private val _msgList = MutableLiveData<List<MsgBean>>()
|
||||
var adapter: ChatMsgAdapter = ChatMsgAdapter(_msgList.value ?: emptyList(), this)
|
||||
private val mGson: Gson = Gson()
|
||||
|
||||
init {
|
||||
Log.e("ChatPageViewModel", "Init: ${showSendBtn.value}")
|
||||
viewModelScope.launch {
|
||||
val list = getList()
|
||||
adapter.setData(list)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//获取列表
|
||||
private suspend fun getList(): List<MsgBean> {
|
||||
return withContext(Dispatchers.IO) {
|
||||
val msgDao = DbManager.db.msgDao()
|
||||
return@withContext msgDao.getMsgByFromOrToPage(fromId, toId)
|
||||
}
|
||||
}
|
||||
|
||||
fun onTxtChange(s: CharSequence, start: Int, before: Int, count: Int) {
|
||||
@ -179,7 +193,7 @@ class ChatPageViewModel : BaseViewModel() {
|
||||
}
|
||||
}
|
||||
//刷新列表
|
||||
|
||||
scrollListToBottom.value = true
|
||||
Log.e("ChatPageViewModel", "receiveMsg接收到消息: ${bean.msgBean}")
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user