From 398c2237bcf374fbbad7b46b58b4c432acbe266a Mon Sep 17 00:00:00 2001 From: Naveen Date: Sat, 19 Nov 2022 22:46:18 +0530 Subject: [PATCH] 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 --- .../smsmessenger/activities/ThreadActivity.kt | 4 ++-- .../simplemobiletools/smsmessenger/adapters/ThreadAdapter.kt | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/ThreadActivity.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/ThreadActivity.kt index 1e9b9c0a..628e516d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/ThreadActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/ThreadActivity.kt @@ -408,6 +408,7 @@ class ThreadActivity : SimpleActivity() { } private fun deleteMessages(messagesToRemove: List) { + val deletePosition = threadItems.indexOf(messagesToRemove.first()) messages.removeAll(messagesToRemove.toSet()) threadItems = getThreadItems() @@ -416,7 +417,7 @@ class ThreadActivity : SimpleActivity() { finish() } else { getOrCreateThreadAdapter().apply { - updateMessages(threadItems) + updateMessages(threadItems, scrollPosition = deletePosition) finishActMode() } } @@ -441,7 +442,6 @@ class ThreadActivity : SimpleActivity() { updateScheduledMessagesThreadId(messages, fakeThreadId) threadId = fakeThreadId } - refreshMessages() } private fun fetchNextMessages() { diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/ThreadAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/ThreadAdapter.kt index 12dd23d5..0fa9e199 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/ThreadAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/ThreadAdapter.kt @@ -230,7 +230,9 @@ class ThreadAdapter( fun updateMessages(newMessages: ArrayList, scrollPosition: Int = newMessages.lastIndex) { val latestMessages = newMessages.toMutableList() submitList(latestMessages) { - recyclerView.scrollToPosition(scrollPosition) + if (scrollPosition != -1) { + recyclerView.scrollToPosition(scrollPosition) + } } }