From 88287b8312d54fb5cb1fc84d30060200b04fd262 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Bj=C3=B6rkert?= Date: Sun, 24 May 2026 17:09:23 +0200 Subject: [PATCH] Deduplicate Dexcom Share readings to fix delta always showing zero Dexcom Share returns each reading twice when both the iPhone Dexcom app and the Apple Watch app upload to the same account (~9-10 s apart, same SGV). Without deduplication the two most recent entries in bgData were always identical, producing delta = 0. The NS fetch path already had inline deduplication. Extract it into a shared helper (deduplicateBGReadings) and apply it to the Dexcom-only path as well. --- .../Controllers/Nightscout/BGData.swift | 30 +++++++++++-------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/LoopFollow/Controllers/Nightscout/BGData.swift b/LoopFollow/Controllers/Nightscout/BGData.swift index d97aba24c..e735c8db9 100644 --- a/LoopFollow/Controllers/Nightscout/BGData.swift +++ b/LoopFollow/Controllers/Nightscout/BGData.swift @@ -37,7 +37,7 @@ extension MainViewController { if graphHours > 24, IsNightscoutEnabled() { self.webLoadNSBGData(dexData: data) } else { - self.ProcessDexBGData(data: data, sourceName: "Dexcom") + self.ProcessDexBGData(data: self.deduplicateBGReadings(data), sourceName: "Dexcom") } } } @@ -70,18 +70,7 @@ extension MainViewController { nsData[i].date.round(FloatingPointRoundingRule.toNearestOrEven) } - var nsData2: [ShareGlucoseData] = [] - var lastAddedTime = Double.infinity - var lastAddedSGV: Int? - let minInterval: Double = 30 - - for reading in nsData { - if (lastAddedSGV == nil || lastAddedSGV != reading.sgv) || (lastAddedTime - reading.date >= minInterval) { - nsData2.append(reading) - lastAddedTime = reading.date - lastAddedSGV = reading.sgv - } - } + var nsData2 = self.deduplicateBGReadings(nsData) // merge NS and Dex data if needed; use recent Dex data and older NS data var sourceName = "Nightscout" @@ -117,6 +106,21 @@ extension MainViewController { } } + /// Removes consecutive duplicate readings (same SGV within 30 s). Expects newest-first input. + func deduplicateBGReadings(_ readings: [ShareGlucoseData]) -> [ShareGlucoseData] { + var result: [ShareGlucoseData] = [] + var lastTime = Double.infinity + var lastSGV: Int? + for reading in readings { + if lastSGV == nil || lastSGV != reading.sgv || lastTime - reading.date >= 30 { + result.append(reading) + lastTime = reading.date + lastSGV = reading.sgv + } + } + return result + } + /// Processes incoming BG data. func ProcessDexBGData(data: [ShareGlucoseData], sourceName: String) { let graphHours = 24 * Storage.shared.downloadDays.value