diff --git a/app/src/main/java/com/pulseloop/ui/PulseLoopApp.kt b/app/src/main/java/com/pulseloop/ui/PulseLoopApp.kt index 01c60ff9..dd264c20 100644 --- a/app/src/main/java/com/pulseloop/ui/PulseLoopApp.kt +++ b/app/src/main/java/com/pulseloop/ui/PulseLoopApp.kt @@ -491,12 +491,17 @@ fun PulseLoopApp() { sourceLabel = hcSource?.let { com.pulseloop.health.source.HealthConnectSource.label(context, it) + tr(" · Health Connect") }, ) // Thin sync-progress accent under the greeting while history streams in. + // Its 2dp are always reserved: appearing and disappearing changed the + // header's height, so every sync (each reconnect, every half hour) + // shifted the whole tab down and back up. val syncPct = coordinator.syncProgress.collectAsState().value - if (syncPct != null) { - LinearProgressIndicator( - progress = { (syncPct.coerceIn(0, 100)) / 100f }, - modifier = Modifier.fillMaxWidth().height(2.dp), - ) + Box(Modifier.fillMaxWidth().height(2.dp)) { + if (syncPct != null) { + LinearProgressIndicator( + progress = { (syncPct.coerceIn(0, 100)) / 100f }, + modifier = Modifier.fillMaxSize(), + ) + } } } } diff --git a/app/src/main/java/com/pulseloop/ui/screens/GuidanceUi.kt b/app/src/main/java/com/pulseloop/ui/screens/GuidanceUi.kt index 948ce575..c3a93eeb 100644 --- a/app/src/main/java/com/pulseloop/ui/screens/GuidanceUi.kt +++ b/app/src/main/java/com/pulseloop/ui/screens/GuidanceUi.kt @@ -32,7 +32,8 @@ data class GuidanceState( @Composable fun rememberGuidance(db: PulseLoopDatabase, key: Any?): GuidanceState? { val state by produceState(initialValue = null, db, key) { - value = runCatching { + // Off the main thread: it reads and scores every recent night. + value = kotlinx.coroutines.withContext(kotlinx.coroutines.Dispatchers.Default) { runCatching { val inputs = ReadinessRepository.loadInputs(db) val nights = inputs.history + listOfNotNull(inputs.lastNight) GuidanceState( @@ -40,7 +41,7 @@ fun rememberGuidance(db: PulseLoopDatabase, key: Any?): GuidanceState? { bedtime = BedtimeGuidance.compute(nights, inputs.sleepGoalMinutes), sleepGoalMinutes = inputs.sleepGoalMinutes, ) - }.getOrNull() + }.getOrNull() } } return state } diff --git a/app/src/main/java/com/pulseloop/ui/screens/ReadinessUi.kt b/app/src/main/java/com/pulseloop/ui/screens/ReadinessUi.kt index a3efeb2a..f86e415b 100644 --- a/app/src/main/java/com/pulseloop/ui/screens/ReadinessUi.kt +++ b/app/src/main/java/com/pulseloop/ui/screens/ReadinessUi.kt @@ -74,7 +74,8 @@ data class ScoreLoad(val loaded: Boolean, val value: T?) @Composable fun rememberReadinessLoad(db: PulseLoopDatabase, key: Any?): ScoreLoad { val result by produceState(initialValue = ScoreLoad(false, null), db, key) { - value = ScoreLoad(true, runCatching { ReadinessRepository.compute(db) }.getOrNull()) + // Off the main thread: the computation walks the past nights. + value = ScoreLoad(true, kotlinx.coroutines.withContext(kotlinx.coroutines.Dispatchers.Default) { runCatching { ReadinessRepository.compute(db) }.getOrNull() }) } return result } @@ -364,7 +365,7 @@ private fun ContributorRow(c: ScoreContributor, expanded: Boolean, onTap: () -> @Composable fun rememberActivityScoreLoad(db: PulseLoopDatabase, key: Any?): ScoreLoad { val result by produceState(initialValue = ScoreLoad(false, null), db, key) { - value = ScoreLoad(true, runCatching { com.pulseloop.service.ActivityScore.load(db) }.getOrNull()) + value = ScoreLoad(true, kotlinx.coroutines.withContext(kotlinx.coroutines.Dispatchers.Default) { runCatching { com.pulseloop.service.ActivityScore.load(db) }.getOrNull() }) } return result } diff --git a/app/src/main/java/com/pulseloop/ui/screens/TodayScreen.kt b/app/src/main/java/com/pulseloop/ui/screens/TodayScreen.kt index 224996d9..9057aebf 100644 --- a/app/src/main/java/com/pulseloop/ui/screens/TodayScreen.kt +++ b/app/src/main/java/com/pulseloop/ui/screens/TodayScreen.kt @@ -138,18 +138,20 @@ fun TodayScreen( // An accuracy-check steps correction supersedes the phone factor: applying both would // correct the same undercount twice. val accuracySteps = com.pulseloop.health.accuracy.AccuracyCorrections.load(db).has(com.pulseloop.health.accuracy.ComparisonMetric.STEPS) - value = if (accuracySteps || !com.pulseloop.service.StepCalibration.isApplied(context)) null else runCatching { - val zone = java.time.ZoneId.systemDefault() - val ring = db.activityDailyDao().recentReal(60).associate { java.time.Instant.ofEpochMilli(it.date).atZone(zone).toLocalDate() to it.steps } - com.pulseloop.service.StepCalibration.factor(com.pulseloop.service.StepCalibration.phoneDays(context), ring, java.time.LocalDate.now())?.factor - }.getOrNull() + value = if (accuracySteps || !com.pulseloop.service.StepCalibration.isApplied(context)) null else kotlinx.coroutines.withContext(kotlinx.coroutines.Dispatchers.Default) { + runCatching { + val zone = java.time.ZoneId.systemDefault() + val ring = db.activityDailyDao().recentReal(60).associate { java.time.Instant.ofEpochMilli(it.date).atZone(zone).toLocalDate() to it.steps } + com.pulseloop.service.StepCalibration.factor(com.pulseloop.service.StepCalibration.phoneDays(context), ring, java.time.LocalDate.now())?.factor + }.getOrNull() + } } // Drinks, when the user tracks them: the tile and the morning-after line on Last night. var drinkTick by remember { mutableIntStateOf(0) } var drinksLoaded by remember { mutableStateOf(false) } val accuracyOpen by remember(db) { db.accuracyDao().openFlow() }.collectAsState(initial = null) val drinks by produceState(null, state.lastUpdated, sleep.lastNight?.id, drinkTick, logOpen) { - value = if (com.pulseloop.service.DrinkPrefs.enabled) runCatching { com.pulseloop.service.DrinksRepository.load(db) }.getOrNull() else null + value = if (com.pulseloop.service.DrinkPrefs.enabled) kotlinx.coroutines.withContext(kotlinx.coroutines.Dispatchers.Default) { runCatching { com.pulseloop.service.DrinksRepository.load(db) }.getOrNull() } else null drinksLoaded = true } diff --git a/app/src/main/java/com/pulseloop/ui/viewmodels/ViewModels.kt b/app/src/main/java/com/pulseloop/ui/viewmodels/ViewModels.kt index 2606655a..51e12ad3 100644 --- a/app/src/main/java/com/pulseloop/ui/viewmodels/ViewModels.kt +++ b/app/src/main/java/com/pulseloop/ui/viewmodels/ViewModels.kt @@ -152,6 +152,10 @@ class TodayViewModel(db: PulseLoopDatabase, private val apiKeyStore: ApiKeyStore ) } } } + // The four polls below stamp [TodayState.lastUpdated] only when their reading changed. + // Today keys its readiness, guidance, food and drinks loaders on that stamp, so stamping it + // on every pass re-ran them all about once a second while the screen sat still, and the + // redraws read as flicker under the frosted header. // Reactive HR — poll latest every 2s, resilient to DB errors. // Also derive resting HR from the last 24h of samples (low-percentile estimate). viewModelScope.launch { @@ -164,7 +168,10 @@ class TodayViewModel(db: PulseLoopDatabase, private val apiKeyStore: ApiKeyStore .range(MeasurementKind.HEART_RATE.name, nowMs - 24 * 3600_000L, nowMs) .map { corrections.heartRate(it.value) } val resting = com.pulseloop.service.HeartRateZones.restingHeartRate(samples) - _state.update { it.copy(heartRate = hr?.toInt(), restingHR = resting, lastUpdated = nowMs) } + _state.update { + if (it.heartRate == hr?.toInt() && it.restingHR == resting) it + else it.copy(heartRate = hr?.toInt(), restingHR = resting, lastUpdated = nowMs) + } } catch (_: Exception) {} kotlinx.coroutines.delay(2000) } @@ -174,7 +181,10 @@ class TodayViewModel(db: PulseLoopDatabase, private val apiKeyStore: ApiKeyStore while (true) { try { val spo2 = db.measurementDao().latest(MeasurementKind.SPO2.name)?.let(AccuracyCorrections.load(db)::spo2) - _state.update { it.copy(spo2 = spo2?.toInt(), lastUpdated = System.currentTimeMillis()) } + _state.update { + if (it.spo2 == spo2?.toInt()) it + else it.copy(spo2 = spo2?.toInt(), lastUpdated = System.currentTimeMillis()) + } } catch (_: Exception) {} kotlinx.coroutines.delay(2000) } @@ -185,11 +195,16 @@ class TodayViewModel(db: PulseLoopDatabase, private val apiKeyStore: ApiKeyStore try { val sys = db.measurementDao().latest(MeasurementKind.BLOOD_PRESSURE_SYSTOLIC.name) val dia = db.measurementDao().latest(MeasurementKind.BLOOD_PRESSURE_DIASTOLIC.name) - _state.update { it.copy( - bloodPressureSystolic = sys?.toInt()?.plus(apiKeyStore?.bpAdjustSystolic ?: 0), - bloodPressureDiastolic = dia?.toInt()?.plus(apiKeyStore?.bpAdjustDiastolic ?: 0), - lastUpdated = System.currentTimeMillis() - ) } + val systolic = sys?.toInt()?.plus(apiKeyStore?.bpAdjustSystolic ?: 0) + val diastolic = dia?.toInt()?.plus(apiKeyStore?.bpAdjustDiastolic ?: 0) + _state.update { + if (it.bloodPressureSystolic == systolic && it.bloodPressureDiastolic == diastolic) it + else it.copy( + bloodPressureSystolic = systolic, + bloodPressureDiastolic = diastolic, + lastUpdated = System.currentTimeMillis(), + ) + } } catch (_: Exception) {} kotlinx.coroutines.delay(5000) } @@ -201,7 +216,10 @@ class TodayViewModel(db: PulseLoopDatabase, private val apiKeyStore: ApiKeyStore // Raw ring glucose + app-side calibration offset (see ApiKeyStore.glucoseOffsetMgdl). val glucose = db.measurementDao().latest(MeasurementKind.BLOOD_SUGAR.name) ?.plus(apiKeyStore?.glucoseOffsetMgdl ?: 0.0) - _state.update { it.copy(bloodSugar = glucose, lastUpdated = System.currentTimeMillis()) } + _state.update { + if (it.bloodSugar == glucose) it + else it.copy(bloodSugar = glucose, lastUpdated = System.currentTimeMillis()) + } } catch (_: Exception) {} kotlinx.coroutines.delay(5000) }