Remove only pending messages instead of reloading the whole history when sending pending messages

This commit is contained in:
Matéo Duparc 2021-08-02 22:03:37 +02:00
parent 5fc3f7d0cf
commit b20fded45b
Signed by: hardcoresushi
GPG Key ID: 007F84120107191E
2 changed files with 22 additions and 18 deletions

View File

@ -44,16 +44,16 @@ class ChatActivity : ServiceBoundActivity() {
override fun onConnectFailed(ip: String, errorMsg: String?) {}
override fun onNewSession(sessionId: Int, ip: String) {
if (this@ChatActivity.sessionId == sessionId) {
val contact = airaService.contacts[sessionId]
val hasPendingMsg = airaService.pendingMsgs[sessionId]?.size ?: 0 > 0
runOnUiThread {
val contact = airaService.contacts[sessionId]
if (contact == null) {
binding.bottomPanel.visibility = View.VISIBLE
} else {
binding.offlineWarning.visibility = View.GONE
if (airaService.pendingMsgs[sessionId]!!.size > 0) {
if (hasPendingMsg) {
binding.sendingPendingMsgsIndicator.visibility = View.VISIBLE
//remove pending messages
reloadHistory(contact)
chatAdapter.removePendingMessages()
scrollToBottom()
}
}
@ -213,7 +213,16 @@ class ChatActivity : ServiceBoundActivity() {
binding.toolbar.avatar.setImageAvatar(image)
}
}
reloadHistory(contact)
chatAdapter.clear()
lastLoadedMessageOffset = 0
if (contact != null) {
loadMsgs(contact.uuid)
}
airaService.savedMsgs[sessionId]?.let {
for (msg in it.asReversed()) {
chatAdapter.newLoadedMessage(ChatItem(msg.outgoing, msg.timestamp, msg.data))
}
}
airaService.pendingMsgs[sessionId]?.let {
for (msg in it) {
if (msg[0] == Protocol.MESSAGE ||msg[0] == Protocol.FILE) {
@ -289,19 +298,6 @@ class ChatActivity : ServiceBoundActivity() {
}
}
private fun reloadHistory(contact: Contact?) {
chatAdapter.clear()
lastLoadedMessageOffset = 0
if (contact != null) {
loadMsgs(contact.uuid)
}
airaService.savedMsgs[sessionId]?.let {
for (msg in it.asReversed()) {
chatAdapter.newLoadedMessage(ChatItem(msg.outgoing, msg.timestamp, msg.data))
}
}
}
private fun scrollToBottom() {
binding.recyclerChat.smoothScrollToPosition(chatAdapter.itemCount-1)
}

View File

@ -51,6 +51,14 @@ class ChatAdapter(
notifyItemChanged(1)
}
fun removePendingMessages() {
val oldSize = chatItems.size
chatItems.removeAll {
it.timestamp == 0L
}
notifyItemRangeRemoved(chatItems.size, oldSize-chatItems.size)
}
fun clear() {
chatItems.clear()
notifyDataSetChanged()