-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdateRequiredCarbs.patch
69 lines (66 loc) · 2.72 KB
/
updateRequiredCarbs.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
Submodule Loop contains modified content
diff --git a/Loop/Loop/Managers/LoopDataManager.swift b/Loop/Loop/Managers/LoopDataManager.swift
index 00b64996..f17a2457 100644
--- a/Loop/Loop/Managers/LoopDataManager.swift
+++ b/Loop/Loop/Managers/LoopDataManager.swift
@@ -484,6 +484,54 @@ final class LoopDataManager {
}
}
}
+
+ fileprivate var requiredCarbs: HKQuantity? {
+ didSet {
+ let number = requiredCarbs?.doubleValue(for: .gram()) ?? 0
+ DispatchQueue.main.async {
+ UIApplication.shared.applicationIconBadgeNumber = Int(number)
+ }
+ }
+ }
+
+ /// The carb sensitivity schedule, applying recent overrides relative to the current moment in time.
+ /// This is measured in <blood glucose>/gram
+ var carbSensitivityScheduleApplyingOverrideHistory: CarbSensitivitySchedule? {
+ guard let crSchedule = carbRatioScheduleApplyingOverrideHistory,
+ var isfSchedule = insulinSensitivityScheduleApplyingOverrideHistory
+ else {
+ return nil
+ }
+ // FreeAPS specific fix:
+ // This prevents the timeZone bug in future and allows recovery with rebuild
+ if crSchedule.timeZone != isfSchedule.timeZone {
+ isfSchedule.timeZone = crSchedule.timeZone
+ }
+
+ return .carbSensitivitySchedule(insulinSensitivitySchedule: isfSchedule, carbRatioSchedule: crSchedule)
+ }
+
+ func updateRequiredCarbs() {
+ dispatchPrecondition(condition: .onQueue(dataAccessQueue))
+ guard
+ let unit = glucoseStore.preferredUnit,
+ let predictedGlucose = self.predictedGlucose?.last,
+ let csfSchedule = carbSensitivityScheduleApplyingOverrideHistory,
+ let glucoseTargetRange = settings.effectiveGlucoseTargetRangeSchedule()
+ else {
+ requiredCarbs = nil
+ return
+ }
+ let delta = glucoseTargetRange.minQuantity(at: predictedGlucose.startDate).doubleValue(for: unit)
+ - predictedGlucose.quantity.doubleValue(for: unit)
+ guard delta > 0 else {
+ requiredCarbs = nil
+ return
+ }
+
+ let now = Date()
+ requiredCarbs = HKQuantity(unit: .gram(), doubleValue: delta / csfSchedule.value(at: now))
+ }
}
// MARK: Background task management
@@ -1071,6 +1119,8 @@ extension LoopDataManager {
dosingDecision.predictedGlucose = predictedGlucose
dosingDecision.automaticDoseRecommendation = recommendedAutomaticDose?.recommendation
+ updateRequiredCarbs()
+
// If the glucose prediction hasn't changed, then nothing has changed, so just use pre-existing recommendations
guard predictedGlucose == nil else {