mirror of
https://github.com/kalarina-bit/Sparkledns.git
synced 2026-11-22 07:36:09 +00:00
Stop UI updates while the app is in the background
Home and Monitor polled the query log every second and Home queried ConnectivityManager every 3 s, and kept doing so while the activity was stopped. The log now signals changes (still at most once a second), Private DNS comes from a network callback, and all flows are collected with collectAsStateWithLifecycle.
This commit is contained in:
@@ -1,5 +1,10 @@
|
||||
package cc.skysparkle.sparkledns
|
||||
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.transform
|
||||
|
||||
enum class QueryStatus { OK, CACHED, BLOCKED, FAILED }
|
||||
|
||||
class QueryEntry(
|
||||
@@ -31,6 +36,14 @@ object QueryLog {
|
||||
private var failed = 0L
|
||||
private var latencySum = 0L
|
||||
private var latencyCount = 0L
|
||||
private val version = MutableStateFlow(0L)
|
||||
|
||||
// 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
|
||||
fun changes(): Flow<Long> = version.transform {
|
||||
emit(it)
|
||||
delay(1000)
|
||||
}
|
||||
|
||||
fun add(question: DnsMessage.Question?, status: QueryStatus, latencyMs: Int) {
|
||||
if (question == null) return
|
||||
@@ -46,6 +59,7 @@ object QueryLog {
|
||||
QueryStatus.BLOCKED -> blocked++
|
||||
QueryStatus.FAILED -> failed++
|
||||
}
|
||||
version.value++
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,5 +81,6 @@ object QueryLog {
|
||||
fun clear() {
|
||||
entries.clear()
|
||||
total = 0; cached = 0; blocked = 0; failed = 0; latencySum = 0; latencyCount = 0
|
||||
version.value++
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user