Retain scroll position when deleting messages

Removed the call to `refreshMessages()` as it's not really needed here and also because it'll reset the scroll position in some cases
This commit is contained in:
Naveen 2022-11-19 22:46:18 +05:30
parent c6d453f5ab
commit 398c2237bc
2 changed files with 5 additions and 3 deletions

View File

@ -408,6 +408,7 @@ class ThreadActivity : SimpleActivity() {
} }
private fun deleteMessages(messagesToRemove: List<Message>) { private fun deleteMessages(messagesToRemove: List<Message>) {
val deletePosition = threadItems.indexOf(messagesToRemove.first())
messages.removeAll(messagesToRemove.toSet()) messages.removeAll(messagesToRemove.toSet())
threadItems = getThreadItems() threadItems = getThreadItems()
@ -416,7 +417,7 @@ class ThreadActivity : SimpleActivity() {
finish() finish()
} else { } else {
getOrCreateThreadAdapter().apply { getOrCreateThreadAdapter().apply {
updateMessages(threadItems) updateMessages(threadItems, scrollPosition = deletePosition)
finishActMode() finishActMode()
} }
} }
@ -441,7 +442,6 @@ class ThreadActivity : SimpleActivity() {
updateScheduledMessagesThreadId(messages, fakeThreadId) updateScheduledMessagesThreadId(messages, fakeThreadId)
threadId = fakeThreadId threadId = fakeThreadId
} }
refreshMessages()
} }
private fun fetchNextMessages() { private fun fetchNextMessages() {

View File

@ -230,7 +230,9 @@ class ThreadAdapter(
fun updateMessages(newMessages: ArrayList<ThreadItem>, scrollPosition: Int = newMessages.lastIndex) { fun updateMessages(newMessages: ArrayList<ThreadItem>, scrollPosition: Int = newMessages.lastIndex) {
val latestMessages = newMessages.toMutableList() val latestMessages = newMessages.toMutableList()
submitList(latestMessages) { submitList(latestMessages) {
recyclerView.scrollToPosition(scrollPosition) if (scrollPosition != -1) {
recyclerView.scrollToPosition(scrollPosition)
}
} }
} }