Rebuild the Activity log only when it changed

This commit is contained in:
2026-10-31 11:50:14 +02:00
parent d58e9cb5f9
commit e3f5906924
2 changed files with 30 additions and 12 deletions
@@ -35,12 +35,14 @@ import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue import androidx.compose.runtime.setValue
import androidx.compose.runtime.structuralEqualityPolicy
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip import androidx.compose.ui.draw.clip
@@ -99,7 +101,10 @@ fun ActivityScreen(bottomPadding: Dp, modifier: Modifier = Modifier) {
val statsVersion by StatsStore.version.collectAsStateWithLifecycle() val statsVersion by StatsStore.version.collectAsStateWithLifecycle()
LaunchedEffect(period, statsVersion) { summary = StatsStore.summary(period.hours) } LaunchedEffect(period, statsVersion) { summary = StatsStore.summary(period.hours) }
val visible = remember(entries, filter, search) { // Filtered again only when the log, the filter or the search changes, and the list below is
// updated only when the result differs: new entries that the filter hides change nothing
val visible by remember {
derivedStateOf(structuralEqualityPolicy()) {
val term = search.trim() val term = search.trim()
entries.filter { e -> entries.filter { e ->
val matchesFilter = when (filter) { val matchesFilter = when (filter) {
@@ -111,6 +116,8 @@ fun ActivityScreen(bottomPadding: Dp, modifier: Modifier = Modifier) {
matchesFilter && (term.isEmpty() || e.domain.contains(term, ignoreCase = true)) matchesFilter && (term.isEmpty() || e.domain.contains(term, ignoreCase = true))
} }
} }
}
val canClear by remember { derivedStateOf { entries.isNotEmpty() } }
fun toggleBlock(domain: String, unblock: Boolean) { fun toggleBlock(domain: String, unblock: Boolean) {
scope.launch { scope.launch {
@@ -144,7 +151,6 @@ fun ActivityScreen(bottomPadding: Dp, modifier: Modifier = Modifier) {
val wide = windowWidth() >= WideLayoutWidth val wide = windowWidth() >= WideLayoutWidth
val side = sidePadding(windowWidth(), if (wide) MaxWideContentWidth else MaxContentWidth) val side = sidePadding(windowWidth(), if (wide) MaxWideContentWidth else MaxContentWidth)
val canClear = entries.isNotEmpty()
val clear = { val clear = {
QueryLog.clear() QueryLog.clear()
entries = emptyList() entries = emptyList()
@@ -25,6 +25,10 @@ object QueryLog {
private var nextId = 0L private var nextId = 0L
private val version = MutableStateFlow(0L) private val version = MutableStateFlow(0L)
// The last snapshot, handed out again while the log has not changed
private var lastSnapshot: List<QueryEntry> = emptyList()
private var snapshotVersion = 0L
// Fires right away, then at most once a second while queries keep coming: a busy log refreshes // Fires right away, then at most once a second while queries keep coming: a busy log refreshes
// the screen no more often than the old one-second polling did, and an idle one not at all // the screen no more often than the old one-second polling did, and an idle one not at all
fun changes(): Flow<Long> = version.transform { fun changes(): Flow<Long> = version.transform {
@@ -45,9 +49,17 @@ object QueryLog {
} }
// Newest first. Builds a real copy: on API 35+ List.reversed() resolves to a live JDK view, // Newest first. Builds a real copy: on API 35+ List.reversed() resolves to a live JDK view,
// and on older devices that method does not exist at all // and on older devices that method does not exist at all. While the log does not change the
// same list comes back, so a screen refreshed with it has nothing to update.
@Synchronized @Synchronized
fun snapshot(): List<QueryEntry> = List(entries.size) { entries[entries.size - 1 - it] } fun snapshot(): List<QueryEntry> {
val current = version.value
if (snapshotVersion != current) {
lastSnapshot = List(entries.size) { entries[entries.size - 1 - it] }
snapshotVersion = current
}
return lastSnapshot
}
@Synchronized @Synchronized
fun clear() { fun clear() {