-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbasicICBolusCalc.patch
116 lines (111 loc) · 5.19 KB
/
basicICBolusCalc.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
diff --git a/Loop/Loop/View Models/BolusEntryViewModel.swift b/Loop/Loop/View Models/BolusEntryViewModel.swift
index a86f20e0..335f1169 100644
--- a/Loop/Loop/View Models/BolusEntryViewModel.swift
+++ b/Loop/Loop/View Models/BolusEntryViewModel.swift
@@ -788,6 +788,56 @@ final class BolusEntryViewModel: ObservableObject {
chartDateInterval = DateInterval(start: chartStartDate, duration: .hours(totalHours))
}
+
+ // Gets the current insulin-to-carb ratio based on the carb ratio schedule
+ func currentInsulinToCarbRatio(carbs: HKQuantity) -> Double? {
+ // Ensure the carb ratio schedule is available
+ guard let carbRatioSchedule = delegate?.settings.carbRatioSchedule else {
+ return nil
+ }
+
+ // Get the current date
+ let date = Date()
+
+ // Retrieve the carb ratio at the current date
+ let carbRatio = carbRatioSchedule.quantity(at: date)
+ let currentICRatio = carbRatio.doubleValue(for: .gram())
+
+ return currentICRatio
+ }
+
+ // Calculates the bolus amount based on the given carbs, carbs on board, the insulin-to-carb ratio, and the active insulin
+ func calculateBasicBolus(carbs: HKQuantity) -> Double? {
+ // Get the current insulin-to-carb ratio, return nil if it cannot be obtained
+ guard let currentICRatio = currentInsulinToCarbRatio(carbs: carbs), currentICRatio != 0 else {
+ return nil
+ }
+
+ // Get the insulin on board, using a minimum value of 0
+ let insulinOnBoard = max(activeInsulin?.doubleValue(for: .internationalUnit()) ?? 0, 0)
+
+ // Get the carbs on board, using a default value of 0 if it cannot be obtained
+ let carbsOnBoard = activeCarbs?.doubleValue(for: .gram()) ?? 0
+
+ // Calculate the total carbs by adding the entered carbs and carbs on board
+ let totalCarbs = carbs.doubleValue(for: HKUnit.gram()) + carbsOnBoard
+
+ // Calculate the unadjusted bolus by dividing the total carbs by the current insulin-to-carb ratio
+ let unadjustedBolus = totalCarbs / currentICRatio
+
+ // Calculate the bolus with insulin on board considered, ensuring it's not less than 0
+ let bolusWithIOB = max(unadjustedBolus - insulinOnBoard, 0)
+
+ // Round the bolus volume according to the pump's settings
+ let roundedBolus = roundedBolusVolume(units: bolusWithIOB)
+
+ return roundedBolus
+ }
+
+ // Rounds the bolus volume according to the pump's specifications
+ func roundedBolusVolume(units: Double) -> Double {
+ return delegate?.roundBolusVolume(units: units) ?? units
+ }
func formatBolusAmount(_ bolusAmount: Double) -> String {
bolusAmountFormatter.string(from: bolusAmount) ?? String(bolusAmount)
diff --git a/Loop/Loop/Views/BolusEntryView.swift b/Loop/Loop/Views/BolusEntryView.swift
index 4dd0c11a..ffed8fb5 100644
--- a/Loop/Loop/Views/BolusEntryView.swift
+++ b/Loop/Loop/Views/BolusEntryView.swift
@@ -156,6 +156,42 @@ struct BolusEntryView: View {
maxFractionDigits: 2
)
}
+
+ // SwiftUI view component displaying the basic I:C bolus calculation
+ private var basicICBolusCalcRow: some View {
+ HStack {
+ Text("Basic I:C Bolus Calc", comment: "Label for basic I:C bolus calc row on bolus screen")
+ Spacer()
+ HStack(alignment: .firstTextBaseline) {
+ Text(basicBolusCalcString)
+ .font(.title)
+ .foregroundColor(Color(.red))
+ bolusUnitsLabel
+ }
+ }
+ .accessibilityElement(children: .combine)
+ // Handle tap gestures to update enteredBolusStringBinding with the calculated bolus value
+ .onTapGesture {
+ enteredBolusStringBinding.wrappedValue = basicBolusCalcString
+ }
+ }
+
+ // Computed property returning the string representation of the basic bolus calculation
+ private var basicBolusCalcString: String {
+ // Ensure a potential carb entry is available
+ guard let potentialCarbEntry = viewModel.potentialCarbEntry else { return "–" }
+
+ // Get the carbs quantity
+ let carbsQuantity = potentialCarbEntry.quantity
+
+ // Calculate the basic bolus using the view model's function
+ guard let basicBolus = viewModel.calculateBasicBolus(carbs: carbsQuantity) else {
+ return "–"
+ }
+
+ // Return the formatted basic bolus string
+ return Self.doseAmountFormatter.string(from: basicBolus) ?? "–"
+ }
private var predictedGlucoseChart: some View {
PredictedGlucoseChartView(
@@ -194,6 +230,7 @@ struct BolusEntryView: View {
if viewModel.isManualGlucoseEntryEnabled || viewModel.potentialCarbEntry != nil {
recommendedBolusRow
+ basicICBolusCalcRow
}
bolusEntryRow