mirror of
https://github.com/kalarina-bit/Sparkledns.git
synced 2026-11-22 15:46:09 +00:00
Rebuild the Activity log only when it changed
This commit is contained in:
@@ -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,18 +101,23 @@ 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
|
||||||
val term = search.trim()
|
// updated only when the result differs: new entries that the filter hides change nothing
|
||||||
entries.filter { e ->
|
val visible by remember {
|
||||||
val matchesFilter = when (filter) {
|
derivedStateOf(structuralEqualityPolicy()) {
|
||||||
ActivityFilter.ALL -> true
|
val term = search.trim()
|
||||||
ActivityFilter.BLOCKED -> e.status == QueryStatus.BLOCKED
|
entries.filter { e ->
|
||||||
ActivityFilter.CACHED -> e.status == QueryStatus.CACHED
|
val matchesFilter = when (filter) {
|
||||||
ActivityFilter.ERRORS -> e.status == QueryStatus.FAILED
|
ActivityFilter.ALL -> true
|
||||||
|
ActivityFilter.BLOCKED -> e.status == QueryStatus.BLOCKED
|
||||||
|
ActivityFilter.CACHED -> e.status == QueryStatus.CACHED
|
||||||
|
ActivityFilter.ERRORS -> e.status == QueryStatus.FAILED
|
||||||
|
}
|
||||||
|
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() {
|
||||||
|
|||||||
Reference in New Issue
Block a user