fix(overview): Hide stale cached age on live cards

This commit is contained in:
darken
2026-04-14 11:28:41 +02:00
committed by Matthias Urhahn
parent 1cd8ae5ace
commit a4e1a3ba2e
4 changed files with 34 additions and 2 deletions
@@ -215,7 +215,7 @@ fun DualPodsCard(
}
// Cached battery indicator
if (device.isBatteryCached && !device.isAapReady) {
if (device.isBatteryCached && !device.isLive) {
Spacer(modifier = Modifier.height(4.dp))
Text(
text = stringResource(R.string.battery_cached_label, device.cachedBatteryFormatted(now)),
@@ -240,7 +240,7 @@ fun SinglePodsCard(
}
// Cached battery indicator
if (device.isBatteryCached && !device.isAapReady) {
if (device.isBatteryCached && !device.isLive) {
Spacer(modifier = Modifier.height(4.dp))
Text(
text = stringResource(R.string.battery_cached_label, device.cachedBatteryFormatted(now)),
@@ -58,6 +58,10 @@ fun PodDevice.toCachedState(
private fun hasStateChanged(old: CachedDeviceState, new: CachedDeviceState): Boolean {
if (Duration.between(old.lastSeenAt, new.lastSeenAt).abs() > Duration.ofMinutes(1)) return true
if (hasSlotTimestampChanged(old.left, new.left)) return true
if (hasSlotTimestampChanged(old.right, new.right)) return true
if (hasSlotTimestampChanged(old.case, new.case)) return true
if (hasSlotTimestampChanged(old.headset, new.headset)) return true
return old.left?.percent != new.left?.percent
|| old.right?.percent != new.right?.percent
|| old.case?.percent != new.case?.percent
@@ -70,3 +74,11 @@ private fun hasStateChanged(old: CachedDeviceState, new: CachedDeviceState): Boo
|| old.serialNumber != new.serialNumber
|| old.firmwareVersion != new.firmwareVersion
}
private fun hasSlotTimestampChanged(
old: CachedBatterySlot?,
new: CachedBatterySlot?,
): Boolean {
if (old == null || new == null) return false
return Duration.between(old.updatedAt, new.updatedAt).abs() > Duration.ofMinutes(1)
}
@@ -134,5 +134,25 @@ class ToCachedStateTest : BaseTest() {
)
liveDevice().toCachedState(existing, now).shouldNotBeNull()
}
@Test
fun `returns new state when a live slot refreshes an old cached timestamp`() {
val existing = CachedDeviceState(
profileId = "test-profile",
model = PodModel.AIRPODS_PRO3,
left = CachedDeviceState.CachedBatterySlot(0.8f, now.minusSeconds(10)),
right = CachedDeviceState.CachedBatterySlot(0.7f, now.minusSeconds(10)),
case = CachedDeviceState.CachedBatterySlot(0.5f, now.minusSeconds(177 * 60 * 60)),
isLeftCharging = false,
isRightCharging = false,
isCaseCharging = false,
isHeadsetCharging = false,
lastSeenAt = now.minusSeconds(10),
)
liveDevice(leftBattery = 0.8f, rightBattery = 0.7f, caseBattery = 0.5f)
.toCachedState(existing, now)
.shouldNotBeNull()
}
}
}