Limit the text length in the 'in reply to' preview (#4491)
* Limit the text length in the 'in reply to' preview Otherwise, very long texts with no line breaks can cause a Compose crash
This commit is contained in:
parent
338267d4e3
commit
f59b26eb10
3 changed files with 28 additions and 8 deletions
|
|
@ -98,3 +98,22 @@ fun String.ensureEndsLeftToRight() = if (containsRtLOverride()) "$this$LTR_OVERR
|
|||
fun String.containsRtLOverride() = contains(RTL_OVERRIDE_CHAR)
|
||||
|
||||
fun String.filterDirectionOverrides() = filterNot { it == RTL_OVERRIDE_CHAR || it == LTR_OVERRIDE_CHAR }
|
||||
|
||||
/**
|
||||
* This works around https://github.com/element-hq/element-x-android/issues/2105.
|
||||
* @param maxLength Max characters to retrieve. Defaults to `500`.
|
||||
* @param ellipsize Whether to add an ellipsis (`…`) char at the end or not. Defaults to `false`.
|
||||
* @return The string truncated to [maxLength] characters, with an optional ellipsis if larger.
|
||||
*/
|
||||
fun String.toSafeLength(
|
||||
maxLength: Int = 500,
|
||||
ellipsize: Boolean = false,
|
||||
): String {
|
||||
return if (ellipsize) {
|
||||
ellipsize(maxLength)
|
||||
} else if (length > maxLength) {
|
||||
take(maxLength)
|
||||
} else {
|
||||
this
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,6 +31,7 @@ import androidx.compose.ui.text.style.TextOverflow
|
|||
import androidx.compose.ui.tooling.preview.PreviewParameter
|
||||
import androidx.compose.ui.unit.dp
|
||||
import io.element.android.compound.theme.ElementTheme
|
||||
import io.element.android.libraries.core.extensions.toSafeLength
|
||||
import io.element.android.libraries.designsystem.atomic.atoms.PlaceholderAtom
|
||||
import io.element.android.libraries.designsystem.icons.CompoundDrawables
|
||||
import io.element.android.libraries.designsystem.preview.ElementPreview
|
||||
|
|
@ -152,8 +153,10 @@ private fun ReplyToContentText(metadata: InReplyToMetadata?) {
|
|||
val text = when (metadata) {
|
||||
InReplyToMetadata.Redacted -> stringResource(id = CommonStrings.common_message_removed)
|
||||
InReplyToMetadata.UnableToDecrypt -> stringResource(id = CommonStrings.common_waiting_for_decryption_key)
|
||||
is InReplyToMetadata.Text -> metadata.text
|
||||
is InReplyToMetadata.Thumbnail -> metadata.text
|
||||
// Add a limit to the text length to avoid a crash in Compose
|
||||
is InReplyToMetadata.Text -> metadata.text.toSafeLength()
|
||||
// Add a limit to the text length to avoid a crash in Compose
|
||||
is InReplyToMetadata.Thumbnail -> metadata.text.toSafeLength()
|
||||
null -> ""
|
||||
}
|
||||
val iconResourceId = when (metadata) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue