Bladeren bron

solve merge conflicts

polscm32 2 jaren geleden
bovenliggende
commit
66971c9e8f

+ 3 - 1
FreeAPS/Resources/json/defaults/freeaps/freeaps_settings.json

@@ -46,5 +46,7 @@
   "overrideFactor": 0.8,
   "overrideFactor": 0.8,
   "useCalc": false,
   "useCalc": false,
   "fattyMeals": false,
   "fattyMeals": false,
-  "fattyMealFactor": 0.7
+  "fattyMealFactor": 0.7,
+  "sweetMeals": false,
+  "sweetMealFactor": 2
 }
 }

+ 10 - 0
FreeAPS/Sources/Models/FreeAPSSettings.swift

@@ -49,6 +49,8 @@ struct FreeAPSSettings: JSON, Equatable {
     var useCalc: Bool = false
     var useCalc: Bool = false
     var fattyMeals: Bool = false
     var fattyMeals: Bool = false
     var fattyMealFactor: Decimal = 0.7
     var fattyMealFactor: Decimal = 0.7
+    var sweetMeals: Bool = false
+    var sweetMealFactor: Decimal = 2
     var displayPredictions: Bool = true
     var displayPredictions: Bool = true
 }
 }
 
 
@@ -156,6 +158,14 @@ extension FreeAPSSettings: Decodable {
             settings.fattyMealFactor = fattyMealFactor
             settings.fattyMealFactor = fattyMealFactor
         }
         }
 
 
+        if let sweetMeals = try? container.decode(Bool.self, forKey: .sweetMeals) {
+            settings.sweetMeals = sweetMeals
+        }
+
+        if let sweetMealFactor = try? container.decode(Decimal.self, forKey: .sweetMealFactor) {
+            settings.sweetMealFactor = sweetMealFactor
+        }
+
         if let overrideFactor = try? container.decode(Decimal.self, forKey: .overrideFactor) {
         if let overrideFactor = try? container.decode(Decimal.self, forKey: .overrideFactor) {
             settings.overrideFactor = overrideFactor
             settings.overrideFactor = overrideFactor
         }
         }

+ 6 - 0
FreeAPS/Sources/Modules/Bolus/BolusProvider.swift

@@ -12,6 +12,12 @@ extension Bolus {
                 ?? PumpSettings(insulinActionCurve: 6, maxBolus: 10, maxBasal: 2)
                 ?? PumpSettings(insulinActionCurve: 6, maxBolus: 10, maxBasal: 2)
         }
         }
 
 
+        func getProfile() -> [BasalProfileEntry] {
+            storage.retrieve(OpenAPS.Settings.basalProfile, as: [BasalProfileEntry].self)
+                ?? [BasalProfileEntry](from: OpenAPS.defaults(for: OpenAPS.Settings.basalProfile))
+                ?? []
+        }
+
         func fetchGlucose() -> [Readings] {
         func fetchGlucose() -> [Readings] {
             let fetchGlucose = coreDataStorage.fetchGlucose(interval: DateFilter().twoHours)
             let fetchGlucose = coreDataStorage.fetchGlucose(interval: DateFilter().twoHours)
             return fetchGlucose
             return fetchGlucose

+ 48 - 0
FreeAPS/Sources/Modules/Bolus/BolusStateModel.swift

@@ -59,6 +59,12 @@ extension Bolus {
         @Published var useFattyMealCorrectionFactor: Bool = false
         @Published var useFattyMealCorrectionFactor: Bool = false
         @Published var displayPredictions: Bool = true
         @Published var displayPredictions: Bool = true
 
 
+        @Published var currentBasal: Decimal = 0
+        @Published var sweetMeals: Bool = false
+        @Published var sweetMealFactor: Decimal = 0
+        @Published var useSuperBolus: Bool = false
+        @Published var superBolusInsulin: Decimal = 0
+
         @Published var meal: [CarbsEntry]?
         @Published var meal: [CarbsEntry]?
         @Published var carbs: Decimal = 0
         @Published var carbs: Decimal = 0
         @Published var fat: Decimal = 0
         @Published var fat: Decimal = 0
@@ -77,6 +83,8 @@ extension Bolus {
             useCalc = settings.settings.useCalc
             useCalc = settings.settings.useCalc
             fattyMeals = settings.settings.fattyMeals
             fattyMeals = settings.settings.fattyMeals
             fattyMealFactor = settings.settings.fattyMealFactor
             fattyMealFactor = settings.settings.fattyMealFactor
+            sweetMeals = settings.settings.sweetMeals
+            sweetMealFactor = settings.settings.sweetMealFactor
             displayPredictions = settings.settings.displayPredictions
             displayPredictions = settings.settings.displayPredictions
 
 
             if waitForSuggestionInitial {
             if waitForSuggestionInitial {
@@ -99,6 +107,42 @@ extension Bolus {
             }
             }
         }
         }
 
 
+        func getCurrentBasal() {
+            let basalEntries = provider.getProfile()
+
+            let dateFormatter = DateFormatter()
+            dateFormatter.dateFormat = "HH:mm:ss"
+            let currentTime = dateFormatter.string(from: Date())
+
+            // loop throug entries and get current basal entry
+            for (index, entry) in basalEntries.enumerated() {
+                if let entryStartTimeDate = dateFormatter.date(from: entry.start) {
+                    var entryEndTimeDate: Date
+
+                    if index < basalEntries.count - 1 {
+                        let nextEntry = basalEntries[index + 1]
+                        if let nextEntryStartTimeDate = dateFormatter.date(from: nextEntry.start) {
+                            let timeDifference = nextEntryStartTimeDate.timeIntervalSince(entryStartTimeDate)
+                            entryEndTimeDate = entryStartTimeDate.addingTimeInterval(timeDifference)
+                        } else {
+                            continue
+                        }
+                    } else {
+                        entryEndTimeDate = Date()
+                    }
+                    // if currenTime is between start and end of basal entry -> basal = currentBasal
+                    if let currentTimeDate = dateFormatter.date(from: currentTime) {
+                        if currentTimeDate >= entryStartTimeDate, currentTimeDate <= entryEndTimeDate {
+                            if let basal = entry.rate as? Decimal {
+                                currentBasal = basal
+                                break
+                            }
+                        }
+                    }
+                }
+            }
+        }
+
         func getDeltaBG() {
         func getDeltaBG() {
             let glucose = provider.fetchGlucose()
             let glucose = provider.fetchGlucose()
             guard glucose.count >= 3 else { return }
             guard glucose.count >= 3 else { return }
@@ -150,6 +194,9 @@ extension Bolus {
             // apply custom factor if fatty meal toggle in bolus calc config settings is on and the box for fatty meals is checked (in RootView)
             // apply custom factor if fatty meal toggle in bolus calc config settings is on and the box for fatty meals is checked (in RootView)
             if useFattyMealCorrectionFactor {
             if useFattyMealCorrectionFactor {
                 insulinCalculated = result * fattyMealFactor
                 insulinCalculated = result * fattyMealFactor
+            } else if useSuperBolus {
+                superBolusInsulin = sweetMealFactor * currentBasal
+                insulinCalculated = wholeCalc + superBolusInsulin
             } else {
             } else {
                 insulinCalculated = result
                 insulinCalculated = result
             }
             }
@@ -217,6 +264,7 @@ extension Bolus {
                     .roundBolus(amount: max(self.insulinRecommended, 0))
                     .roundBolus(amount: max(self.insulinRecommended, 0))
 
 
                 if self.useCalc {
                 if self.useCalc {
+                    self.getCurrentBasal()
                     self.getDeltaBG()
                     self.getDeltaBG()
                     self.insulinCalculated = self.calculateInsulin()
                     self.insulinCalculated = self.calculateInsulin()
                 }
                 }

+ 58 - 15
FreeAPS/Sources/Modules/Bolus/View/AlternativeBolusCalcRootView.swift

@@ -93,6 +93,23 @@ extension Bolus {
                             .font(.footnote)
                             .font(.footnote)
                             .onChange(of: state.useFattyMealCorrectionFactor) { _ in
                             .onChange(of: state.useFattyMealCorrectionFactor) { _ in
                                 state.insulinCalculated = state.calculateInsulin()
                                 state.insulinCalculated = state.calculateInsulin()
+                                if state.useFattyMealCorrectionFactor {
+                                    state.useSuperBolus = false
+                                }
+                            }
+                        }
+                        if state.sweetMeals {
+                            Spacer()
+                            Toggle(isOn: $state.useSuperBolus) {
+                                Text("Super Bolus")
+                            }
+                            .toggleStyle(CheckboxToggleStyle())
+                            .font(.footnote)
+                            .onChange(of: state.useSuperBolus) { _ in
+                                state.insulinCalculated = state.calculateInsulin()
+                                if state.useSuperBolus {
+                                    state.useFattyMealCorrectionFactor = false
+                                }
                             }
                             }
                         }
                         }
                     }
                     }
@@ -385,6 +402,17 @@ extension Bolus {
                             .foregroundColor(.orange)
                             .foregroundColor(.orange)
                     }
                     }
                 }
                 }
+                if state.useSuperBolus {
+                    HStack {
+                        Text("Super Bolus")
+                            .foregroundColor(.red)
+                        Spacer()
+                        let superBolusInsulin = state.superBolusInsulin
+                        Text(superBolusInsulin.formatted()).foregroundColor(.red)
+                        Text(" U")
+                            .foregroundColor(.secondary)
+                    }
+                }
             }
             }
         }
         }
 
 
@@ -485,27 +513,42 @@ extension Bolus {
                     Text("Result")
                     Text("Result")
                         .fontWeight(.bold)
                         .fontWeight(.bold)
                     Spacer()
                     Spacer()
-                    let fraction = state.fraction
-                    Text(fraction.formatted())
-                    Text(" x ")
-                        .foregroundColor(.secondary)
 
 
-                    // if fatty meal is chosen
-                    if state.useFattyMealCorrectionFactor {
-                        let fattyMealFactor = state.fattyMealFactor
-                        Text(fattyMealFactor.formatted())
-                            .foregroundColor(.orange)
+                    if !state.useSuperBolus {
+                        let fraction = state.fraction
+                        Text(fraction.formatted())
                         Text(" x ")
                         Text(" x ")
                             .foregroundColor(.secondary)
                             .foregroundColor(.secondary)
-                    }
 
 
-                    let insulin = state.roundedWholeCalc
-                    Text(insulin.formatted()).foregroundStyle(state.roundedWholeCalc < 0 ? Color.loopRed : Color.primary)
-                    Text(" U")
-                        .foregroundColor(.secondary)
+                        // if fatty meal is chosen
+                        if state.useFattyMealCorrectionFactor {
+                            let fattyMealFactor = state.fattyMealFactor
+                            Text(fattyMealFactor.formatted())
+                                .foregroundColor(.orange)
+                            Text(" x ")
+                                .foregroundColor(.secondary)
+                        }
+
+                        let insulin = state.roundedWholeCalc
+                        Text(insulin.formatted()).foregroundStyle(state.roundedWholeCalc < 0 ? Color.loopRed : Color.primary)
+                        Text(" U")
+                            .foregroundColor(.secondary)
+                    } else {
+                        // roundedWholeCalc
+                        let insulin = state.roundedWholeCalc
+                        Text(insulin.formatted()).foregroundStyle(state.roundedWholeCalc < 0 ? Color.loopRed : Color.primary)
+                        Text(" U")
+                        // plus
+                        Text(" + ")
+                            .foregroundColor(.secondary)
+                        // superBolusInsulin
+                        let superBolusInsulin = state.superBolusInsulin
+                        Text(superBolusInsulin.formatted()).foregroundColor(.red)
+                        Text(" U")
+                            .foregroundColor(.secondary)
+                    }
                     Text(" = ")
                     Text(" = ")
                         .foregroundColor(.secondary)
                         .foregroundColor(.secondary)
-
                     let result = state.insulinCalculated
                     let result = state.insulinCalculated
                     // rounding
                     // rounding
                     let resultAsDouble = NSDecimalNumber(decimal: result).doubleValue
                     let resultAsDouble = NSDecimalNumber(decimal: result).doubleValue

+ 9 - 0
FreeAPS/Sources/Modules/BolusCalculatorConfig/BolusCalculatorStateModel.swift

@@ -6,6 +6,8 @@ extension BolusCalculatorConfig {
         @Published var useCalc: Bool = false
         @Published var useCalc: Bool = false
         @Published var fattyMeals: Bool = false
         @Published var fattyMeals: Bool = false
         @Published var fattyMealFactor: Decimal = 0
         @Published var fattyMealFactor: Decimal = 0
+        @Published var sweetMeals: Bool = false
+        @Published var sweetMealFactor: Decimal = 0
         @Published var insulinReqPercentage: Decimal = 70
         @Published var insulinReqPercentage: Decimal = 70
         @Published var displayPredictions: Bool = true
         @Published var displayPredictions: Bool = true
 
 
@@ -25,6 +27,13 @@ extension BolusCalculatorConfig {
             }, map: {
             }, map: {
                 $0
                 $0
             })
             })
+            subscribeSetting(\.sweetMeals, on: $sweetMeals) { sweetMeals = $0 }
+            subscribeSetting(\.sweetMealFactor, on: $sweetMealFactor, initial: {
+                let value = max(min($0, 5), 1)
+                sweetMealFactor = value
+            }, map: {
+                $0
+            })
             subscribeSetting(\.insulinReqPercentage, on: $insulinReqPercentage) { insulinReqPercentage = $0 }
             subscribeSetting(\.insulinReqPercentage, on: $insulinReqPercentage) { insulinReqPercentage = $0 }
         }
         }
     }
     }

File diff suppressed because it is too large
+ 12 - 1
FreeAPS/Sources/Modules/BolusCalculatorConfig/View/BolusCalculatorConfigRootView.swift