Skip to content

Commit 418ac4c

Browse files
authored
Merge pull request #6019 from element-hq/feature/bma/fixCrashOnLongStrings
Ensure that room with long names are rendered correctly in the room list.
2 parents ba65823 + 51a92eb commit 418ac4c

File tree

3 files changed

+8
-9
lines changed

3 files changed

+8
-9
lines changed

features/home/impl/src/main/kotlin/io/element/android/features/home/impl/components/RoomSummaryRow.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import io.element.android.features.home.impl.model.RoomListRoomSummaryProvider
4646
import io.element.android.features.home.impl.model.RoomSummaryDisplayType
4747
import io.element.android.features.home.impl.roomlist.RoomListEvents
4848
import io.element.android.libraries.core.extensions.orEmpty
49+
import io.element.android.libraries.core.extensions.toSafeLength
4950
import io.element.android.libraries.designsystem.atomic.atoms.UnreadIndicatorAtom
5051
import io.element.android.libraries.designsystem.atomic.molecules.InviteButtonsRowMolecule
5152
import io.element.android.libraries.designsystem.components.avatar.Avatar
@@ -227,7 +228,7 @@ private fun NameAndTimestampRow(
227228
// Name
228229
Text(
229230
style = ElementTheme.typography.fontBodyLgMedium,
230-
text = name ?: stringResource(id = CommonStrings.common_no_room_name),
231+
text = name?.toSafeLength(ellipsize = true) ?: stringResource(id = CommonStrings.common_no_room_name),
231232
fontStyle = FontStyle.Italic.takeIf { name == null },
232233
color = ElementTheme.colors.roomListRoomName,
233234
maxLines = 1,
@@ -380,7 +381,7 @@ private fun InviteNameAndIndicatorRow(
380381
Text(
381382
modifier = Modifier.weight(1f),
382383
style = ElementTheme.typography.fontBodyLgMedium,
383-
text = name ?: stringResource(id = CommonStrings.common_no_room_name),
384+
text = name?.toSafeLength(ellipsize = true) ?: stringResource(id = CommonStrings.common_no_room_name),
384385
fontStyle = FontStyle.Italic.takeIf { name == null },
385386
color = ElementTheme.colors.roomListRoomName,
386387
maxLines = 1,

libraries/core/src/main/kotlin/io/element/android/libraries/core/extensions/BasicExtensions.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,16 @@ fun String.containsRtLOverride() = contains(RTL_OVERRIDE_CHAR)
100100

101101
fun String.filterDirectionOverrides() = filterNot { it == RTL_OVERRIDE_CHAR || it == LTR_OVERRIDE_CHAR }
102102

103+
const val DEFAULT_SAFE_LENGTH = 500
104+
103105
/**
104106
* This works around https://github.com/element-hq/element-x-android/issues/2105.
105107
* @param maxLength Max characters to retrieve. Defaults to `500`.
106108
* @param ellipsize Whether to add an ellipsis (`…`) char at the end or not. Defaults to `false`.
107109
* @return The string truncated to [maxLength] characters, with an optional ellipsis if larger.
108110
*/
109111
fun String.toSafeLength(
110-
maxLength: Int = 500,
112+
maxLength: Int = DEFAULT_SAFE_LENGTH,
111113
ellipsize: Boolean = false,
112114
): String {
113115
return if (ellipsize) {

libraries/eventformatter/impl/src/main/kotlin/io/element/android/libraries/eventformatter/impl/DefaultRoomLatestEventFormatter.kt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
package io.element.android.libraries.eventformatter.impl
1010

1111
import dev.zacsweers.metro.ContributesBinding
12+
import io.element.android.libraries.core.extensions.DEFAULT_SAFE_LENGTH
1213
import io.element.android.libraries.di.SessionScope
1314
import io.element.android.libraries.eventformatter.api.RoomLatestEventFormatter
1415
import io.element.android.libraries.eventformatter.impl.mode.RenderingMode
@@ -54,11 +55,6 @@ class DefaultRoomLatestEventFormatter(
5455
private val stateContentFormatter: StateContentFormatter,
5556
private val permalinkParser: PermalinkParser,
5657
) : RoomLatestEventFormatter {
57-
companion object {
58-
// Max characters to display in the last message. This works around https://github.com/element-hq/element-x-android/issues/2105
59-
private const val MAX_SAFE_LENGTH = 500
60-
}
61-
6258
override fun format(
6359
latestEvent: LatestEventValue.Local,
6460
isDmRoom: Boolean,
@@ -121,7 +117,7 @@ class DefaultRoomLatestEventFormatter(
121117
}
122118
is LegacyCallInviteContent -> sp.getString(CommonStrings.common_unsupported_call)
123119
is CallNotifyContent -> sp.getString(CommonStrings.common_call_started)
124-
}?.take(MAX_SAFE_LENGTH)
120+
}?.take(DEFAULT_SAFE_LENGTH)
125121
}
126122

127123
private fun MessageContent.process(

0 commit comments

Comments
 (0)