Skip to content

Commit 58eb254

Browse files
committed
Added annotated string, highlight error keywords
1 parent 7124aa7 commit 58eb254

1 file changed

Lines changed: 43 additions & 3 deletions

File tree

app/src/main/java/com/acutecoder/crashhandler/CustomCrashHandlerActivity.kt

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,18 @@ import androidx.compose.runtime.setValue
4848
import androidx.compose.ui.Alignment
4949
import androidx.compose.ui.Modifier
5050
import androidx.compose.ui.draw.clip
51+
import androidx.compose.ui.graphics.Color
5152
import androidx.compose.ui.platform.LocalContext
5253
import androidx.compose.ui.res.painterResource
54+
import androidx.compose.ui.text.SpanStyle
55+
import androidx.compose.ui.text.buildAnnotatedString
56+
import androidx.compose.ui.text.withStyle
5357
import androidx.compose.ui.unit.dp
5458
import androidx.core.content.FileProvider
5559
import com.acutecoder.crashhandler.core.ErrorLog
5660
import com.acutecoder.crashhandler.ui.theme.CrashHandlerTheme
5761
import com.acutecoder.crashhandler.util.crashHandler
5862
import kotlinx.coroutines.Dispatchers
59-
import kotlinx.coroutines.delay
6063
import kotlinx.coroutines.withContext
6164
import java.io.File
6265

@@ -81,7 +84,6 @@ class CustomCrashHandlerActivity : ComponentActivity() {
8184

8285
LaunchedEffect(Unit) {
8386
withContext(Dispatchers.IO) {
84-
delay(4000)
8587
errorLog = context.crashHandler.loadErrorLog()
8688
}
8789
}
@@ -123,7 +125,12 @@ private fun ErrorBox(modifier: Modifier, errorLog: ErrorLog?) {
123125
errorLog?.let {
124126
SelectionContainer {
125127
Text(
126-
text = it.simplifiedLog(),
128+
text = it.simplifiedLog()
129+
.buildAnnotation(
130+
"\\w+(\\.\\w+)+:\\s.*\n".toRegex() to MaterialTheme.colorScheme.error,
131+
"\\([\\w.]+:\\d+\\)".toRegex() to MaterialTheme.colorScheme.primary,
132+
"and \\d+ more errors?".toRegex() to MaterialTheme.colorScheme.primary,
133+
),
127134
modifier = Modifier.padding(12.dp),
128135
color = MaterialTheme.colorScheme.onErrorContainer,
129136
)
@@ -254,3 +261,36 @@ private fun Context.shareLog(file: File) {
254261
startActivity(Intent.createChooser(intent, "Share log file"))
255262
} else Toast.makeText(this, "Crash file not found!", Toast.LENGTH_SHORT).show()
256263
}
264+
265+
private fun String.buildAnnotation(vararg regexPairs: Pair<Regex, Color>) =
266+
buildAnnotatedString {
267+
var currentIndex = 0
268+
269+
val matches = mutableListOf<Triple<Int, Int, Color>>()
270+
271+
regexPairs.forEach { (regex, highlightColor) ->
272+
regex.findAll(this@buildAnnotation).forEach { matchResult ->
273+
val startIndex = matchResult.range.first
274+
val endIndex = matchResult.range.last + 1
275+
matches.add(Triple(startIndex, endIndex, highlightColor))
276+
}
277+
}
278+
279+
matches.sortBy { it.first }
280+
281+
matches.forEach { (startIndex, endIndex, highlightColor) ->
282+
if (currentIndex <= startIndex) {
283+
append(this@buildAnnotation.substring(currentIndex, startIndex))
284+
285+
withStyle(style = SpanStyle(color = highlightColor)) {
286+
append(this@buildAnnotation.substring(startIndex, endIndex))
287+
}
288+
289+
currentIndex = endIndex
290+
}
291+
}
292+
293+
if (currentIndex < this@buildAnnotation.length) {
294+
append(this@buildAnnotation.substring(currentIndex))
295+
}
296+
}

0 commit comments

Comments
 (0)