@@ -343,7 +343,7 @@ final class LoopDataManager: ObservableObject {
343
343
throw LoopError . configurationError ( . basalRateSchedule)
344
344
}
345
345
346
- let forecastEndTime = baseTime. addingTimeInterval ( InsulinMath . defaultInsulinActivityDuration) . dateCeiledToTimeInterval ( . minutes ( GlucoseMath . defaultDelta) )
346
+ let forecastEndTime = baseTime. addingTimeInterval ( InsulinMath . defaultInsulinActivityDuration) . dateCeiledToTimeInterval ( GlucoseMath . defaultDelta)
347
347
348
348
let carbsStart = baseTime. addingTimeInterval ( CarbMath . dateAdjustmentPast + . minutes( - 1 ) ) // additional minute to handle difference in seconds between carb entry and carb ratio
349
349
@@ -366,9 +366,24 @@ final class LoopDataManager: ObservableObject {
366
366
367
367
let glucose = try await glucoseStore. getGlucoseSamples ( start: carbsStart, end: baseTime)
368
368
369
- let sensitivityStart = min ( carbsStart , dosesStart )
369
+ let dosesWithModel = doses . map { $0 . simpleDose ( with : insulinModel ( for : $0 . insulinType ) ) }
370
370
371
- let sensitivity = try await settingsProvider. getInsulinSensitivityHistory ( startDate: sensitivityStart, endDate: forecastEndTime)
371
+ let recommendationInsulinModel = insulinModel ( for: deliveryDelegate? . pumpInsulinType ?? . novolog)
372
+
373
+ let recommendationEffectInterval = DateInterval (
374
+ start: baseTime,
375
+ duration: recommendationInsulinModel. effectDuration
376
+ )
377
+ let neededSensitivityTimeline = LoopAlgorithm . timelineIntervalForSensitivity (
378
+ doses: dosesWithModel,
379
+ glucoseHistoryStart: glucose. first? . startDate ?? baseTime,
380
+ recommendationEffectInterval: recommendationEffectInterval
381
+ )
382
+
383
+ let sensitivity = try await settingsProvider. getInsulinSensitivityHistory (
384
+ startDate: neededSensitivityTimeline. start,
385
+ endDate: neededSensitivityTimeline. end
386
+ )
372
387
373
388
let target = try await settingsProvider. getTargetRangeHistory ( startDate: baseTime, endDate: forecastEndTime)
374
389
@@ -382,7 +397,7 @@ final class LoopDataManager: ObservableObject {
382
397
throw LoopError . configurationError ( . maximumBasalRatePerHour)
383
398
}
384
399
385
- var overrides = temporaryPresetsManager. overrideHistory. getOverrideHistory ( startDate: sensitivityStart , endDate: forecastEndTime)
400
+ var overrides = temporaryPresetsManager. overrideHistory. getOverrideHistory ( startDate: neededSensitivityTimeline . start , endDate: forecastEndTime)
386
401
387
402
// Bug (https://tidepool.atlassian.net/browse/LOOP-4759) pre-meal is not recorded in override history
388
403
// So currently we handle automatic forecast by manually adding it in, and when meal bolusing, we do not do this.
@@ -433,7 +448,7 @@ final class LoopDataManager: ObservableObject {
433
448
434
449
return StoredDataAlgorithmInput (
435
450
glucoseHistory: glucose,
436
- doses: doses . map { $0 . simpleDose ( with : insulinModel ( for : $0 . insulinType ) ) } ,
451
+ doses: dosesWithModel ,
437
452
carbEntries: carbEntries,
438
453
predictionStart: baseTime,
439
454
basal: basalWithOverrides,
@@ -446,7 +461,7 @@ final class LoopDataManager: ObservableObject {
446
461
useIntegralRetrospectiveCorrection: UserDefaults . standard. integralRetrospectiveCorrectionEnabled,
447
462
includePositiveVelocityAndRC: true ,
448
463
carbAbsorptionModel: carbAbsorptionModel,
449
- recommendationInsulinModel: insulinModel ( for : deliveryDelegate ? . pumpInsulinType ?? . novolog ) ,
464
+ recommendationInsulinModel: recommendationInsulinModel ,
450
465
recommendationType: . manualBolus,
451
466
automaticBolusApplicationFactor: effectiveBolusApplicationFactor)
452
467
}
@@ -1018,6 +1033,7 @@ extension StoredDataAlgorithmInput {
1018
1033
carbRatio: carbRatio,
1019
1034
algorithmEffectsOptions: effectsOptions,
1020
1035
useIntegralRetrospectiveCorrection: self . useIntegralRetrospectiveCorrection,
1036
+ useMidAbsorptionISF: true ,
1021
1037
carbAbsorptionModel: self . carbAbsorptionModel. model
1022
1038
)
1023
1039
return prediction. glucose
0 commit comments