From f79db0b6b2f54e62e37e477785273cda5f23b264 Mon Sep 17 00:00:00 2001 From: Smarshal21 Date: Sat, 18 Nov 2023 18:23:21 +0530 Subject: [PATCH] Refactored isMessageWithSingleEmoticonOnly in TextMatchers to use regex for single emoji check Signed-off-by: Smarshal21 --- .../messages/IncomingTextMessageViewHolder.kt | 4 ++- .../OutcomingTextMessageViewHolder.kt | 4 ++- .../nextcloud/talk/utils/TextMatchers.java | 25 +++++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/com/nextcloud/talk/utils/TextMatchers.java diff --git a/app/src/main/java/com/nextcloud/talk/adapters/messages/IncomingTextMessageViewHolder.kt b/app/src/main/java/com/nextcloud/talk/adapters/messages/IncomingTextMessageViewHolder.kt index 061f8196a..17333d452 100644 --- a/app/src/main/java/com/nextcloud/talk/adapters/messages/IncomingTextMessageViewHolder.kt +++ b/app/src/main/java/com/nextcloud/talk/adapters/messages/IncomingTextMessageViewHolder.kt @@ -45,6 +45,7 @@ import com.nextcloud.talk.models.json.chat.ChatMessage import com.nextcloud.talk.ui.theme.ViewThemeUtils import com.nextcloud.talk.utils.ApiUtils import com.nextcloud.talk.utils.DateUtils +import com.nextcloud.talk.utils.TextMatchers import com.nextcloud.talk.utils.message.MessageUtils import com.nextcloud.talk.utils.preferences.AppPreferences import com.stfalcon.chatkit.messages.MessageHolders @@ -102,7 +103,8 @@ class IncomingTextMessageViewHolder(itemView: View, payload: Any) : val messageParameters = message.messageParameters if ( - (messageParameters == null || messageParameters.size <= 0) + (messageParameters == null || messageParameters.size <= 0) && + TextMatchers.isMessageWithSingleEmoticonOnly(message.text) ) { textSize = (textSize * TEXT_SIZE_MULTIPLIER).toFloat() itemView.isSelected = true diff --git a/app/src/main/java/com/nextcloud/talk/adapters/messages/OutcomingTextMessageViewHolder.kt b/app/src/main/java/com/nextcloud/talk/adapters/messages/OutcomingTextMessageViewHolder.kt index f9dfa95bc..680af44a6 100644 --- a/app/src/main/java/com/nextcloud/talk/adapters/messages/OutcomingTextMessageViewHolder.kt +++ b/app/src/main/java/com/nextcloud/talk/adapters/messages/OutcomingTextMessageViewHolder.kt @@ -41,6 +41,7 @@ import com.nextcloud.talk.models.json.chat.ReadStatus import com.nextcloud.talk.ui.theme.ViewThemeUtils import com.nextcloud.talk.utils.ApiUtils import com.nextcloud.talk.utils.DateUtils +import com.nextcloud.talk.utils.TextMatchers import com.nextcloud.talk.utils.message.MessageUtils import com.stfalcon.chatkit.messages.MessageHolders.OutcomingTextMessageViewHolder import javax.inject.Inject @@ -88,7 +89,8 @@ class OutcomingTextMessageViewHolder(itemView: View) : OutcomingTextMessageViewH val messageParameters = message.messageParameters if ( - (messageParameters == null || messageParameters.size <= 0) + (messageParameters == null || messageParameters.size <= 0) && + TextMatchers.isMessageWithSingleEmoticonOnly(message.text) ) { textSize = (textSize * TEXT_SIZE_MULTIPLIER).toFloat() layoutParams.isWrapBefore = true diff --git a/app/src/main/java/com/nextcloud/talk/utils/TextMatchers.java b/app/src/main/java/com/nextcloud/talk/utils/TextMatchers.java new file mode 100644 index 000000000..d04f657ba --- /dev/null +++ b/app/src/main/java/com/nextcloud/talk/utils/TextMatchers.java @@ -0,0 +1,25 @@ +package com.nextcloud.talk.utils; + + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import androidx.annotation.Nullable; + +public final class TextMatchers { + + public static boolean isMessageWithSingleEmoticonOnly(@Nullable final String text) { + if (text == null || text.isEmpty()) { + return false; + } + String emojiRegex = "([\\p{So}\\p{Sk}])"; + Pattern pattern = Pattern.compile(emojiRegex); + Matcher matcher = pattern.matcher(text); + + int emojiCount = 0; + while (matcher.find()) { + emojiCount++; + } + return emojiCount == 1; + } +}