From 0ac0cba4489a25159ec1fc9bd5c110491d7fa60f Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Fri, 25 Jun 2021 15:11:38 +1000 Subject: [PATCH] Insert mentions --- .../conversation/v2/ConversationActivityV2.kt | 35 ++++++++++++++----- 1 file changed, 26 insertions(+), 9 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 c3f3ee1e83..f8ad83d7f2 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 @@ -302,11 +302,9 @@ class ConversationActivityV2 : PassphraseRequiredActionBarActivity(), InputBarDe // 36 DP is the exact height of the typing indicator view. It's also exactly 18 * 2, and 18 is the large message // corner radius. This makes 36 DP look "correct" in the context of other messages on the screen. val typingIndicatorHeight = if (typingIndicatorViewContainer.isVisible) toPx(36, resources) else 0 - // We * don't * want to move the recycler view up when showing the mention candidates view - val additionalContentContainerHeight = if (isShowingMentionCandidatesView) 0 else additionalContentContainer.height // Recycler view val recyclerViewLayoutParams = conversationRecyclerView.layoutParams as RelativeLayout.LayoutParams - recyclerViewLayoutParams.bottomMargin = newValue + additionalContentContainerHeight + typingIndicatorHeight + recyclerViewLayoutParams.bottomMargin = newValue + typingIndicatorHeight conversationRecyclerView.layoutParams = recyclerViewLayoutParams // Additional content container val additionalContentContainerLayoutParams = additionalContentContainer.layoutParams as RelativeLayout.LayoutParams @@ -331,22 +329,28 @@ class ConversationActivityV2 : PassphraseRequiredActionBarActivity(), InputBarDe } private fun showOrHideMentionCandidatesIfNeeded(text: CharSequence) { - val isBackspace = (text.length < previousText.length) - if (isBackspace) { + if (text.length < previousText.length) { currentMentionStartIndex = -1 hideMentionCandidates() val mentionsToRemove = mentions.filter { !text.contains(it.displayName) } mentions.removeAll(mentionsToRemove) } if (text.isNotEmpty()) { - if (currentMentionStartIndex > text.length) { resetMentions() } // Should never occur val lastCharIndex = text.lastIndex val lastChar = text[lastCharIndex] - val secondToLastChar = if (lastCharIndex > 0) text[lastCharIndex - 1] else ' ' - if (lastChar == '@' && Character.isWhitespace(secondToLastChar)) { + // Check if there is whitespace before the '@' or the '@' is the first character + + val isCharacterBeforeLastWhiteSpaceOrStartOfLine: Boolean + if (text.length == 1) { + isCharacterBeforeLastWhiteSpaceOrStartOfLine = true // Start of line + } else { + val charBeforeLast = text[lastCharIndex - 1] + isCharacterBeforeLastWhiteSpaceOrStartOfLine = Character.isWhitespace(charBeforeLast) + } + if (lastChar == '@' && isCharacterBeforeLastWhiteSpaceOrStartOfLine) { currentMentionStartIndex = lastCharIndex showOrUpdateMentionCandidatesIfNeeded() - } else if (Character.isWhitespace(lastChar)) { + } else if (Character.isWhitespace(lastChar) || lastChar == '@') { // the lastCharacter == "@" is to check for @@ currentMentionStartIndex = -1 hideMentionCandidates() } else if (currentMentionStartIndex != -1) { @@ -362,6 +366,7 @@ class ConversationActivityV2 : PassphraseRequiredActionBarActivity(), InputBarDe additionalContentContainer.removeAllViews() val view = MentionCandidatesView(this) view.glide = glide + view.onCandidateSelected = { handleMentionSelected(it) } additionalContentContainer.addView(view) val candidates = MentionsManager.getMentionCandidates(query, threadID, thread.isOpenGroupRecipient) this.mentionCandidatesView = view @@ -617,6 +622,18 @@ class ConversationActivityV2 : PassphraseRequiredActionBarActivity(), InputBarDe // TODO: Implement } + private fun handleMentionSelected(mention: Mention) { + if (currentMentionStartIndex == -1) { return } + mentions.add(mention) + val previousText = inputBar.text + val newText = previousText.substring(0, currentMentionStartIndex) + "@" + mention.displayName + " " + inputBar.text = newText + inputBar.inputBarEditText.setSelection(newText.length) + currentMentionStartIndex = -1 + hideMentionCandidates() + this.previousText = newText + } + override fun send() { }