Trim Compose work in lists and frequent recompositions

contentType for lazy list items, a set for the own-domain lookup per
Monitor row, the group chevron animated in the draw phase, the custom
URL check remembered and the number format reused.
This commit is contained in:
2026-10-25 12:27:50 +02:00
parent f236136919
commit 04ef7adb44
4 changed files with 20 additions and 9 deletions
@@ -48,9 +48,9 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.rotate
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
@@ -188,7 +188,7 @@ fun AppsScreen(modifier: Modifier = Modifier) {
groups.forEach { (group, items) ->
val expanded = searching || group !in collapsed
item(key = "group:${group.name}") {
item(key = "group:${group.name}", contentType = "group") {
GroupHeader(
modifier = Modifier.animateItem(),
group = group,
@@ -205,7 +205,7 @@ fun AppsScreen(modifier: Modifier = Modifier) {
)
}
if (expanded) {
itemsIndexed(items, key = { _, app -> app.packageName }) { index, app ->
itemsIndexed(items, key = { _, app -> app.packageName }, contentType = { _, _ -> "app" }) { index, app ->
AppRow(
modifier = Modifier.animateItem(),
app = app,
@@ -237,7 +237,8 @@ private fun GroupHeader(
else -> ToggleableState.Indeterminate
}
val chevron by animateFloatAsState(if (expanded) 180f else 0f, Motion.rotation, label = "chevron")
// Read only in the draw phase, so the animation does not recompose the header every frame
val chevron = animateFloatAsState(if (expanded) 180f else 0f, Motion.rotation, label = "chevron")
Row(
modifier = modifier
@@ -267,7 +268,7 @@ private fun GroupHeader(
modifier = Modifier
.padding(end = 8.dp)
.size(22.dp)
.rotate(chevron),
.graphicsLayer { rotationZ = chevron.value },
)
}
}
@@ -145,7 +145,7 @@ fun HomeScreen(modifier: Modifier = Modifier) {
var crashVisible by remember { mutableStateOf(crashReport != null) }
val isCustom = selected == Providers.CUSTOM_ID
val customValid = customUrl.trim().toHttpUrlOrNull()?.isHttps == true
val customValid = remember(customUrl) { customUrl.trim().toHttpUrlOrNull()?.isHttps == true }
val canConnect = !isCustom || customValid
val selectedProvider = remember(selected, customUrl, customBootstrap, customHttp3, servers) { prefs.resolve() }
val customName = stringResource(R.string.custom)
@@ -60,6 +60,7 @@ fun MonitorScreen(modifier: Modifier = Modifier) {
val context = LocalContext.current
val scope = rememberCoroutineScope()
val custom by Blocklist.custom.collectAsStateWithLifecycle()
val customSet = remember(custom) { custom.toHashSet() }
var entries by remember { mutableStateOf(QueryLog.snapshot()) }
var stats by remember { mutableStateOf(QueryLog.stats()) }
@@ -197,8 +198,8 @@ fun MonitorScreen(modifier: Modifier = Modifier) {
}
}
itemsIndexed(visible, key = { _, e -> e.id }) { index, entry ->
val inCustom = entry.domain in custom
itemsIndexed(visible, key = { _, e -> e.id }, contentType = { _, _ -> "query" }) { index, entry ->
val inCustom = entry.domain in customSet
QueryRow(
modifier = Modifier.animateItem(),
entry = entry,
@@ -47,6 +47,7 @@ import androidx.lifecycle.Lifecycle
import androidx.lifecycle.compose.LocalLifecycleOwner
import androidx.lifecycle.repeatOnLifecycle
import java.text.NumberFormat
import java.util.Locale
val CachedColor = Color(0xFF7FD1AE)
val WarningColor = Color(0xFFFFB86B)
@@ -214,7 +215,15 @@ fun rememberLiveStats(): QueryStats {
return stats
}
fun formatCount(value: Long): String = NumberFormat.getIntegerInstance().format(value)
// Called from composition only, i.e. on the main thread; rebuilt when the locale changes
private var countFormat: Pair<Locale, NumberFormat>? = null
fun formatCount(value: Long): String {
val locale = Locale.getDefault()
val format = countFormat?.takeIf { it.first == locale }?.second
?: NumberFormat.getIntegerInstance(locale).also { countFormat = locale to it }
return format.format(value)
}
fun cachePercent(stats: QueryStats): String =
"${if (stats.total == 0L) 0 else stats.cached * 100 / stats.total}%"