From c312c27dd35b4cb6c381e1f1f65d290a6a0fab87 Mon Sep 17 00:00:00 2001 From: Al Lansley Date: Fri, 17 May 2024 10:08:47 +1000 Subject: [PATCH] Reduce frequency of calls to find last sent message --- .../conversation/v2/ConversationActivityV2.kt | 10 ++++++++++ .../securesms/conversation/v2/ConversationAdapter.kt | 8 ++------ .../conversation/v2/messages/VisibleMessageView.kt | 3 ++- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt index 508aebdef2..c4e1b59d01 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt @@ -372,6 +372,7 @@ class ConversationActivityV2 : PassphraseRequiredActionBarActivity(), InputBarDe private var currentLastVisibleRecyclerViewIndex: Int = RecyclerView.NO_POSITION private var recyclerScrollState: Int = RecyclerView.SCROLL_STATE_IDLE + // region Settings companion object { // Extras @@ -387,6 +388,7 @@ class ConversationActivityV2 : PassphraseRequiredActionBarActivity(), InputBarDe const val PICK_FROM_LIBRARY = 12 const val INVITE_CONTACTS = 124 + var lastSentMessageId = -1L; } // endregion @@ -513,6 +515,9 @@ class ConversationActivityV2 : PassphraseRequiredActionBarActivity(), InputBarDe viewModel.run { binding?.toolbarContent?.update(recipient ?: return, openGroup, expirationConfiguration) } + + // Update our last sent message Id on startup / resume (resume is called after onCreate) + lastSentMessageId = mmsSmsDb.getLastOutgoingMessage(viewModel.threadId) } override fun onPause() { @@ -2221,6 +2226,11 @@ class ConversationActivityV2 : PassphraseRequiredActionBarActivity(), InputBarDe // to the bottom of long messages as required by Jira SES-789 / GitHub 1364). recyclerView.scrollToPosition(adapter.itemCount) } + + // Update our cached last sent message to ensure we have accurate details. + // Note: This `onChanged` method is not triggered when scrolling so should minimally + // affect performance. + lastSentMessageId = mmsSmsDb.getLastOutgoingMessage(viewModel.threadId) } } diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationAdapter.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationAdapter.kt index 267a71c0b3..e4c5848229 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationAdapter.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationAdapter.kt @@ -59,7 +59,8 @@ class ConversationAdapter( private val contactCache = SparseArray(100) private val contactLoadedCache = SparseBooleanArray(100) private val lastSeen = AtomicLong(originalLastSeen) - private var lastSentMessageId: Long = -1L + + //private var lastSentMessageId: Long = -1L init { lifecycleCoroutineScope.launch(IO) { @@ -241,11 +242,6 @@ class ConversationAdapter( toDeselect.iterator().forEach { (pos, record) -> onDeselect(record, pos) } - - // This value gets updated here ONLY when the cursor changes, and the value is then passed - // through to `VisibleMessageView.bind` each time we bind via `onBindItemViewHolder`, above. - // If there are no messages then lastSentMessageId is assigned the value -1L. - if (cursor != null) { lastSentMessageId = getLastSentMessageId(cursor) } } fun findLastSeenItemPosition(lastSeenTimestamp: Long): Int? { diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VisibleMessageView.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VisibleMessageView.kt index 5a0da5265c..fd6e9200b0 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VisibleMessageView.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VisibleMessageView.kt @@ -302,7 +302,8 @@ class VisibleMessageView : LinearLayout { // --- If we got here then we know the message is outgoing --- - val lastSentMessageId = mmsSmsDb.getLastOutgoingMessage(message.threadId) + //val lastSentMessageId = mmsSmsDb.getLastOutgoingMessage(message.threadId) + val lastSentMessageId = ConversationActivityV2.lastSentMessageId; val isLastSentMessage = lastSentMessageId == message.id // ----- Case ii.) Message is outgoing but NOT scheduled to disappear -----