Skip to content

Commit

Permalink
fix: limit quick chat append, prevent duplicates
Browse files Browse the repository at this point in the history
Limits quick chat append to 200 characters and prevents duplicate appends.
Also sets a constant for the max message size.

fixes meshtastic#1511
  • Loading branch information
jamesarich committed Jan 3, 2025
1 parent 70a08c9 commit cb0e926
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions app/src/main/java/com/geeksville/mesh/ui/message/Message.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -422,7 +427,7 @@ private fun TextInput(
enabled: Boolean,
message: MutableState<TextFieldValue>,
modifier: Modifier = Modifier,
maxSize: Int = 200,
maxSize: Int = MESSAGE_CHARACTER_LIMIT,
onClick: (String) -> Unit = {}
) = Column(modifier) {
val focusManager = LocalFocusManager.current
Expand Down

0 comments on commit cb0e926

Please sign in to comment.