feat: don't allow sending too long messages

Signed-off-by: Infi <infi@infi.sh>
This commit is contained in:
Infi 2024-03-05 22:01:37 +01:00
parent c9e8185926
commit 72e16cf591
3 changed files with 18 additions and 4 deletions

View File

@ -158,6 +158,7 @@ fun NativeMessageField(
forceSendButton: Boolean = false,
canAttach: Boolean = true,
disabled: Boolean = false,
failedValidation: Boolean = false,
serverId: String? = null,
channelId: String? = null,
editMode: Boolean = false,
@ -176,7 +177,8 @@ fun NativeMessageField(
var requestFocus by remember { mutableStateOf({}) }
var clearFocus by remember { mutableStateOf({}) }
val sendButtonVisible = (value.isNotBlank() || forceSendButton) && !disabled
val sendButtonVisible =
(value.isNotBlank() || forceSendButton) && !disabled && !failedValidation
val density = LocalDensity.current
@ -184,6 +186,7 @@ fun NativeMessageField(
val selectionColour = MaterialTheme.colorScheme.primary.copy(alpha = 0.2f).toArgb()
val cursorColour = MaterialTheme.colorScheme.primary.toArgb()
val failedValidationColour = MaterialTheme.colorScheme.error.toArgb()
val contentColour = LocalContentColor.current.toArgb()
val placeholderColour = LocalContentColor.current.copy(alpha = 0.5f).toArgb()
@ -524,6 +527,11 @@ fun NativeMessageField(
}
it.hint = it.context.getString(placeholderResource, channelName)
it.serverId = serverId
if (failedValidation) {
it.setTextColor(failedValidationColour)
} else {
it.setTextColor(contentColour)
}
},
modifier = Modifier
.weight(1f)

View File

@ -5,7 +5,6 @@ import android.net.Uri
import android.os.Build
import android.os.Environment
import android.provider.MediaStore
import android.util.Log
import android.widget.Toast
import androidx.activity.compose.BackHandler
import androidx.activity.compose.rememberLauncherForActivityResult
@ -73,8 +72,6 @@ import chat.revolt.api.routes.channel.react
import chat.revolt.api.routes.microservices.autumn.FileArgs
import chat.revolt.api.schemas.Channel
import chat.revolt.api.schemas.ChannelType
import chat.revolt.callbacks.Action
import chat.revolt.callbacks.ActionChannel
import chat.revolt.components.chat.Message
import chat.revolt.components.chat.NativeMessageField
import chat.revolt.components.chat.SystemMessage
@ -288,6 +285,12 @@ fun ChannelScreen(
}
}
val isMessageTooLong by remember(viewModel.pendingMessageContent) {
derivedStateOf {
viewModel.pendingMessageContent.length > MAX_MESSAGE_LENGTH
}
}
LaunchedEffect(isScrolledToTop) {
snapshotFlow { isScrolledToTop.value }
.distinctUntilChanged()
@ -530,6 +533,7 @@ fun ChannelScreen(
serverId = channel?.server,
editMode = viewModel.editingMessage != null,
cancelEdit = viewModel::cancelEditingMessage,
failedValidation = isMessageTooLong,
onFocusChange = { nowFocused ->
if (nowFocused && viewModel.currentBottomPane != BottomPane.None) {
viewModel.currentBottomPane = BottomPane.None

View File

@ -59,6 +59,8 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlinx.datetime.Instant
const val MAX_MESSAGE_LENGTH = 2000
sealed class BottomPane {
data object None : BottomPane()
data object InbuiltMediaPicker : BottomPane()