Skip to content

Commit 092a2a0

Browse files
authored
Prevent repeate ICE values (#572)
1 parent 3137445 commit 092a2a0

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

Loop/Managers/LoopDataManager.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -800,14 +800,21 @@ final class LoopDataManager {
800800
insulinEffect.filterDateRange(startDate, endDate)
801801
)
802802

803+
// Ensure we're not repeating effects
804+
if let lastEffect = insulinCounteractionEffects.last {
805+
guard startDate >= lastEffect.endDate else {
806+
return
807+
}
808+
}
809+
803810
// Compare that retrospective, insulin-driven prediction to the actual glucose change to
804811
// calculate the effect of all insulin counteraction
805812
guard let lastGlucose = prediction.last else { return }
806813
let glucoseUnit = HKUnit.milligramsPerDeciliter()
807814
let velocityUnit = glucoseUnit.unitDivided(by: HKUnit.second())
808815
let discrepancy = change.end.quantity.doubleValue(for: glucoseUnit) - lastGlucose.quantity.doubleValue(for: glucoseUnit) // mg/dL
809816
let averageVelocity = HKQuantity(unit: velocityUnit, doubleValue: discrepancy / change.end.endDate.timeIntervalSince(change.start.endDate))
810-
let effect = GlucoseEffectVelocity(startDate: change.start.startDate, endDate: change.end.startDate, quantity: averageVelocity)
817+
let effect = GlucoseEffectVelocity(startDate: startDate, endDate: change.end.startDate, quantity: averageVelocity)
811818

812819
insulinCounteractionEffects.append(effect)
813820
// For now, only keep the last 24 hours of values

0 commit comments

Comments
 (0)