Skip to content

Commit ae58355

Browse files
authored
Fix issue with target override application (#685)
1 parent e2898de commit ae58355

File tree

2 files changed

+14
-36
lines changed

2 files changed

+14
-36
lines changed

Loop/Managers/LoopDataManager+CarbAbsorption.swift

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -50,41 +50,31 @@ extension LoopDataManager {
5050

5151
let sensitivity = try await settingsProvider.getInsulinSensitivityHistory(startDate: sensitivityStart, endDate: end)
5252

53-
var overrides = temporaryPresetsManager.overrideHistory.getOverrideHistory(startDate: sensitivityStart, endDate: end)
53+
let overrides = temporaryPresetsManager.overrideHistory.getOverrideHistory(startDate: sensitivityStart, endDate: end)
5454

5555
guard !sensitivity.isEmpty else {
5656
throw LoopError.configurationError(.insulinSensitivitySchedule)
5757
}
5858

59-
let sensitivityWithOverrides = overrides.apply(over: sensitivity) { (quantity, override) in
60-
let value = quantity.doubleValue(for: .milligramsPerDeciliter)
61-
return HKQuantity(
62-
unit: .milligramsPerDeciliter,
63-
doubleValue: value / override.settings.effectiveInsulinNeedsScaleFactor
64-
)
65-
}
59+
let sensitivityWithOverrides = overrides.applySensitivity(over: sensitivity)
6660

6761
guard !basal.isEmpty else {
6862
throw LoopError.configurationError(.basalRateSchedule)
6963
}
70-
let basalWithOverrides = overrides.apply(over: basal) { (value, override) in
71-
value * override.settings.effectiveInsulinNeedsScaleFactor
72-
}
64+
let basalWithOverrides = overrides.applyBasal(over: basal)
7365

7466
guard !carbRatio.isEmpty else {
7567
throw LoopError.configurationError(.carbRatioSchedule)
7668
}
77-
let carbRatioWithOverrides = overrides.apply(over: carbRatio) { (value, override) in
78-
value * override.settings.effectiveInsulinNeedsScaleFactor
79-
}
69+
let carbRatioWithOverrides = overrides.applyCarbRatio(over: carbRatio)
8070

8171
let carbModel: CarbAbsorptionModel = FeatureFlags.nonlinearCarbModelEnabled ? .piecewiseLinear : .linear
8272

8373
// Overlay basal history on basal doses, splitting doses to get amount delivered relative to basal
84-
let annotatedDoses = doses.annotated(with: basal)
74+
let annotatedDoses = doses.annotated(with: basalWithOverrides)
8575

8676
let insulinEffects = annotatedDoses.glucoseEffects(
87-
insulinSensitivityHistory: sensitivity,
77+
insulinSensitivityHistory: sensitivityWithOverrides,
8878
from: start.addingTimeInterval(-CarbMath.maximumAbsorptionTimeInterval).dateFlooredToTimeInterval(GlucoseMath.defaultDelta),
8979
to: nil)
9080

@@ -94,15 +84,15 @@ extension LoopDataManager {
9484
// Carb Effects
9585
let carbStatus = carbEntries.map(
9686
to: insulinCounteractionEffects,
97-
carbRatio: carbRatio,
98-
insulinSensitivity: sensitivity
87+
carbRatio: carbRatioWithOverrides,
88+
insulinSensitivity: sensitivityWithOverrides
9989
)
10090

10191
let carbEffects = carbStatus.dynamicGlucoseEffects(
10292
from: end,
10393
to: end.addingTimeInterval(InsulinMath.defaultInsulinActivityDuration),
104-
carbRatios: carbRatio,
105-
insulinSensitivities: sensitivity,
94+
carbRatios: carbRatioWithOverrides,
95+
insulinSensitivities: sensitivityWithOverrides,
10696
absorptionModel: carbModel.model
10797
)
10898

Loop/Managers/LoopDataManager.swift

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -349,34 +349,22 @@ final class LoopDataManager: ObservableObject {
349349
throw LoopError.configurationError(.insulinSensitivitySchedule)
350350
}
351351

352-
let sensitivityWithOverrides = overrides.apply(over: sensitivity) { (quantity, override) in
353-
let value = quantity.doubleValue(for: .milligramsPerDeciliter)
354-
return HKQuantity(
355-
unit: .milligramsPerDeciliter,
356-
doubleValue: value / override.settings.effectiveInsulinNeedsScaleFactor
357-
)
358-
}
352+
let sensitivityWithOverrides = overrides.applySensitivity(over: sensitivity)
359353

360354
guard !basal.isEmpty else {
361355
throw LoopError.configurationError(.basalRateSchedule)
362356
}
363-
let basalWithOverrides = overrides.apply(over: basal) { (value, override) in
364-
value * override.settings.effectiveInsulinNeedsScaleFactor
365-
}
357+
let basalWithOverrides = overrides.applyBasal(over: basal)
366358

367359
guard !carbRatio.isEmpty else {
368360
throw LoopError.configurationError(.carbRatioSchedule)
369361
}
370-
let carbRatioWithOverrides = overrides.apply(over: carbRatio) { (value, override) in
371-
value * override.settings.effectiveInsulinNeedsScaleFactor
372-
}
362+
let carbRatioWithOverrides = overrides.applyCarbRatio(over: carbRatio)
373363

374364
guard !target.isEmpty else {
375365
throw LoopError.configurationError(.glucoseTargetRangeSchedule)
376366
}
377-
let targetWithOverrides = overrides.apply(over: target) { (range, override) in
378-
override.settings.targetRange ?? range
379-
}
367+
let targetWithOverrides = overrides.applyTarget(over: target, at: baseTime)
380368

381369
// Create dosing strategy based on user setting
382370
let applicationFactorStrategy: ApplicationFactorStrategy = UserDefaults.standard.glucoseBasedApplicationFactorEnabled

0 commit comments

Comments
 (0)