Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions app/src/main/java/com/pulseloop/ui/PulseLoopApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
)
}
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/java/com/pulseloop/ui/screens/GuidanceUi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,16 @@ data class GuidanceState(
@Composable
fun rememberGuidance(db: PulseLoopDatabase, key: Any?): GuidanceState? {
val state by produceState<GuidanceState?>(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(
offSignals = inputs.lastNight?.let { VitalsCheck.offSignals(it, inputs.history) } ?: emptyList(),
bedtime = BedtimeGuidance.compute(nights, inputs.sleepGoalMinutes),
sleepGoalMinutes = inputs.sleepGoalMinutes,
)
}.getOrNull()
}.getOrNull() }
}
return state
}
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/java/com/pulseloop/ui/screens/ReadinessUi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ data class ScoreLoad<T>(val loaded: Boolean, val value: T?)
@Composable
fun rememberReadinessLoad(db: PulseLoopDatabase, key: Any?): ScoreLoad<Readiness.Result> {
val result by produceState(initialValue = ScoreLoad<Readiness.Result>(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
}
Expand Down Expand Up @@ -364,7 +365,7 @@ private fun ContributorRow(c: ScoreContributor, expanded: Boolean, onTap: () ->
@Composable
fun rememberActivityScoreLoad(db: PulseLoopDatabase, key: Any?): ScoreLoad<com.pulseloop.service.ActivityScore.Result> {
val result by produceState(initialValue = ScoreLoad<com.pulseloop.service.ActivityScore.Result>(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
}
Expand Down
14 changes: 8 additions & 6 deletions app/src/main/java/com/pulseloop/ui/screens/TodayScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<com.pulseloop.service.DrinksRepository.Snapshot?>(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
}

Expand Down
34 changes: 26 additions & 8 deletions app/src/main/java/com/pulseloop/ui/viewmodels/ViewModels.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down
Loading