From cb0e926baa01cf0c972cefd42534c7da6a1975d5 Mon Sep 17 00:00:00 2001 From: jamesarich Date: Fri, 3 Jan 2025 13:18:07 -0600 Subject: [PATCH] fix: limit quick chat append, prevent duplicates Limits quick chat append to 200 characters and prevents duplicate appends. Also sets a constant for the max message size. fixes #1511 --- .../com/geeksville/mesh/ui/message/Message.kt | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/geeksville/mesh/ui/message/Message.kt b/app/src/main/java/com/geeksville/mesh/ui/message/Message.kt index 77dab99bd..ce3c1f87a 100644 --- a/app/src/main/java/com/geeksville/mesh/ui/message/Message.kt +++ b/app/src/main/java/com/geeksville/mesh/ui/message/Message.kt @@ -102,6 +102,8 @@ import com.geeksville.mesh.ui.theme.AppTheme import dagger.hilt.android.AndroidEntryPoint import kotlinx.coroutines.launch +private const val MESSAGE_CHARACTER_LIMIT = 200 + internal fun FragmentManager.navigateToMessages(contactKey: String, message: String = "") { val messagesFragment = MessagesFragment().apply { arguments = bundleOf("contactKey" to contactKey, "message" to message) @@ -255,13 +257,16 @@ internal fun MessageScreen( QuickChatRow(isConnected, quickChat) { action -> if (action.mode == QuickChatAction.Mode.Append) { val originalText = messageInput.value.text - val needsSpace = !originalText.endsWith(' ') && originalText.isNotEmpty() - val newText = buildString { - append(originalText) - if (needsSpace) append(' ') - append(action.message) + if (!originalText.contains(action.message)) { + val needsSpace = + !originalText.endsWith(' ') && originalText.isNotEmpty() + val newText = buildString { + append(originalText) + if (needsSpace) append(' ') + append(action.message) + }.take(MESSAGE_CHARACTER_LIMIT) + messageInput.value = TextFieldValue(newText, TextRange(newText.length)) } - messageInput.value = TextFieldValue(newText, TextRange(newText.length)) } else { viewModel.sendMessage(action.message, contactKey) } @@ -422,7 +427,7 @@ private fun TextInput( enabled: Boolean, message: MutableState, modifier: Modifier = Modifier, - maxSize: Int = 200, + maxSize: Int = MESSAGE_CHARACTER_LIMIT, onClick: (String) -> Unit = {} ) = Column(modifier) { val focusManager = LocalFocusManager.current