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:
Jorge Martin Espinosa
2025-03-27 14:37:38 +01:00
committed by GitHub
parent 338267d4e3
commit f59b26eb10
3 changed files with 28 additions and 8 deletions

View File

@@ -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
}
}