Skip to content

Commit c4e87a0

Browse files
Refactor: Drastically simplify HighlightedText with fallback for crash avoidance
This commit significantly simplifies the `HighlightedText` composable to prioritize crash avoidance and minimize code, based on your feedback. The new logic is as follows: 1. If the `pattern` is empty, the original `text` is displayed without highlighting. 2. If the `pattern` matches the entire `text` (case-insensitively), the original `text` is displayed without any highlighting. This serves as a simple fallback to prevent a suspected `IndexOutOfBoundsException` that might occur when applying styles to the entire string length in the specific Compose version used. 3. For all other cases (i.e., `pattern` is not empty and does not match the entire `text`), the original, compact `text.split()`-based highlighting logic is used. This method highlights occurrences of the pattern but uses the casing of the input `pattern` string for the styled segments, not necessarily the casing of the matched text. This change rolls back more complex highlighting logic (like span splitting for full matches and case-preserving partial match highlighting via Regex/while loops) in favor of utmost simplicity and robustness against the reported crash.
1 parent 6b7a5eb commit c4e87a0

1 file changed

Lines changed: 13 additions & 5 deletions

File tree

  • android/src/main/java/com/inspiredandroid/linuxcommandbibliotheca/ui/composables

android/src/main/java/com/inspiredandroid/linuxcommandbibliotheca/ui/composables/HighlightedText.kt

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,19 @@ fun HighlightedText(
3434
) {
3535
if (pattern.isEmpty()) {
3636
Text(
37-
text,
37+
text = text, // Explicitly naming arguments for clarity
3838
maxLines = maxLines,
39-
overflow = TextOverflow.Ellipsis,
39+
overflow = TextOverflow.Ellipsis
40+
)
41+
} else if (text.equals(pattern, ignoreCase = true)) {
42+
// Fallback for full-text match to avoid potential crash
43+
Text(
44+
text = text,
45+
maxLines = maxLines,
46+
overflow = TextOverflow.Ellipsis
4047
)
4148
} else {
49+
// Original compact highlighting logic for partial matches
4250
val highlightColor = MaterialTheme.colors.primary
4351
val annotatedString = remember(text, pattern, highlightColor) {
4452
buildAnnotatedString {
@@ -47,7 +55,7 @@ fun HighlightedText(
4755
append(s)
4856
if (index != splitText.size - 1) {
4957
withStyle(style = SpanStyle(color = highlightColor)) {
50-
append(pattern)
58+
append(pattern) // Appends the pattern string directly
5159
}
5260
}
5361
}
@@ -56,7 +64,7 @@ fun HighlightedText(
5664
Text(
5765
text = annotatedString,
5866
maxLines = maxLines,
59-
overflow = TextOverflow.Ellipsis,
67+
overflow = TextOverflow.Ellipsis
6068
)
6169
}
6270
}
@@ -67,7 +75,7 @@ fun HighlightedTextPreview() {
6775
LinuxTheme {
6876
HighlightedText(
6977
text = "pacman",
70-
pattern = "cm",
78+
pattern = "cm"
7179
)
7280
}
7381
}

0 commit comments

Comments
 (0)