Procházet zdrojové kódy

Only use the CoreData data when needed

Jon Mårtensson před 2 roky
rodič
revize
72c999f67e

+ 17 - 12
FreeAPS/Sources/Modules/Bolus/BolusStateModel.swift

@@ -153,7 +153,6 @@ extension Bolus {
             insulinCalculated = max(insulinCalculated, 0)
             insulinCalculated = max(insulinCalculated, 0)
             let insulinCalculatedAsDouble = Double(insulinCalculated)
             let insulinCalculatedAsDouble = Double(insulinCalculated)
             roundedInsulinCalculated = Decimal(round(100 * insulinCalculatedAsDouble) / 100)
             roundedInsulinCalculated = Decimal(round(100 * insulinCalculatedAsDouble) / 100)
-
             insulinRecommended = min(insulinCalculated, maxBolus)
             insulinRecommended = min(insulinCalculated, maxBolus)
         }
         }
 
 
@@ -193,9 +192,11 @@ extension Bolus {
                 self.basal = self.provider.suggestion?.rate ?? 0
                 self.basal = self.provider.suggestion?.rate ?? 0
                 self.carbRatio = self.provider.suggestion?.carbRatio ?? 0
                 self.carbRatio = self.provider.suggestion?.carbRatio ?? 0
 
 
-                if self.settingsManager.settings.insulinReqPercentage != 100 {
-                    self.insulinRecommended = self.insulin * (self.settingsManager.settings.insulinReqPercentage / 100)
-                } else { self.insulinRecommended = self.insulin }
+                if !self.useCalc {
+                    if self.settingsManager.settings.insulinReqPercentage != 100 {
+                        self.insulinRecommended = self.insulin * (self.settingsManager.settings.insulinReqPercentage / 100)
+                    } else { self.insulinRecommended = self.insulin }
+                }
 
 
                 self.errorString = self.provider.suggestion?.manualBolusErrorString ?? 0
                 self.errorString = self.provider.suggestion?.manualBolusErrorString ?? 0
                 if self.errorString != 0 {
                 if self.errorString != 0 {
@@ -206,19 +207,22 @@ extension Bolus {
                     self.minPredBG = (self.provider.suggestion?.minPredBG ?? 0) * conversion
                     self.minPredBG = (self.provider.suggestion?.minPredBG ?? 0) * conversion
                 } else { self.error = false }
                 } else { self.error = false }
 
 
-                self.insulinRecommended = self.apsManager
-                    .roundBolus(amount: max(self.insulinRecommended, 0))
+                if !self.useCalc {
+                    self.insulinRecommended = self.apsManager
+                        .roundBolus(amount: max(self.insulinRecommended, 0))
+                }
                 self.getDeltaBG()
                 self.getDeltaBG()
             }
             }
         }
         }
 
 
         func backToCarbsView(complexEntry: Bool, _ id: String) {
         func backToCarbsView(complexEntry: Bool, _ id: String) {
-            debug(.apsManager, "Start deleting carbs")
-            nsManager.deleteCarbs(
-                at: id, isFPU: nil, fpuID: nil, syncID: id,
-                complex: complexEntry
-            )
-            showModal(for: .addCarbs(editMode: true))
+            if complexEntry {
+                nsManager.deleteCarbs(
+                    at: id, isFPU: nil, fpuID: nil, syncID: id,
+                    complex: complexEntry
+                )
+            }
+            showModal(for: .addCarbs(editMode: complexEntry))
         }
         }
     }
     }
 }
 }
@@ -229,5 +233,6 @@ extension Bolus.StateModel: SuggestionObserver {
             self.waitForSuggestion = false
             self.waitForSuggestion = false
         }
         }
         setupInsulinRequired()
         setupInsulinRequired()
+        if useCalc { calculateInsulin() }
     }
     }
 }
 }

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

@@ -209,7 +209,6 @@ extension Bolus {
                         HStack {
                         HStack {
                             Text("Calculations")
                             Text("Calculations")
                                 .font(.title3)
                                 .font(.title3)
-                                .fontWeight(.semibold)
                             Spacer()
                             Spacer()
                         }
                         }
                         .padding(.vertical, 10)
                         .padding(.vertical, 10)
@@ -251,66 +250,71 @@ extension Bolus {
                                         Text("g").foregroundColor(.secondary)
                                         Text("g").foregroundColor(.secondary)
                                     }
                                     }
                                 }
                                 }
-                            }
-                            Divider().fontWeight(.bold).padding(10)
+                            }.padding()
                         }
                         }
 
 
-                        HStack {
-                            Text("Carb Ratio")
-                                .foregroundColor(.secondary)
-                            Spacer()
+                        Divider().fontWeight(.bold)
 
 
-                            Text(state.carbRatio.formatted())
-                            Text(NSLocalizedString(" g/U", comment: " grams per Unit"))
-                                .foregroundColor(.secondary)
-                        }
-                        HStack {
-                            Text("ISF")
-                                .foregroundColor(.secondary)
-                            Spacer()
-                            let isf = state.isf
-                            Text(isf.formatted())
-                            Text(state.units.rawValue + NSLocalizedString("/U", comment: "/Insulin unit"))
-                                .foregroundColor(.secondary)
-                        }
-                        HStack {
-                            Text("Target Glucose")
-                                .foregroundColor(.secondary)
-                            Spacer()
-                            let target = state.units == .mmolL ? state.target.asMmolL : state.target
-                            Text(target.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))))
-                            Text(state.units.rawValue)
-                                .foregroundColor(.secondary)
-                        }
-                        HStack {
-                            Text("Basal")
-                                .foregroundColor(.secondary)
-                            Spacer()
-                            let basal = state.basal
-                            Text(basal.formatted())
-                            Text(NSLocalizedString(" U/h", comment: " Units per hour"))
-                                .foregroundColor(.secondary)
-                        }
-                        HStack {
-                            Text("Fraction")
-                                .foregroundColor(.secondary)
-                            Spacer()
-                            let fraction = state.fraction
-                            Text(fraction.formatted())
-                        }
-                        if state.useFattyMealCorrectionFactor {
+                        VStack {
+                            HStack {
+                                Text("Carb Ratio")
+                                    .foregroundColor(.secondary)
+                                Spacer()
+                                Text(state.carbRatio.formatted())
+                                Text(NSLocalizedString(" g/U", comment: " grams per Unit"))
+                                    .foregroundColor(.secondary)
+                            }
+                            HStack {
+                                Text("ISF")
+                                    .foregroundColor(.secondary)
+                                Spacer()
+                                let isf = state.isf
+                                Text(isf.formatted())
+                                Text(state.units.rawValue + NSLocalizedString("/U", comment: "/Insulin unit"))
+                                    .foregroundColor(.secondary)
+                            }
                             HStack {
                             HStack {
-                                Text("Fatty Meal Factor")
-                                    .foregroundColor(.orange)
+                                Text("Target Glucose")
+                                    .foregroundColor(.secondary)
                                 Spacer()
                                 Spacer()
-                                let fraction = state.fattyMealFactor
+                                let target = state.units == .mmolL ? state.target.asMmolL : state.target
+                                Text(
+                                    target
+                                        .formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits)))
+                                )
+                                Text(state.units.rawValue)
+                                    .foregroundColor(.secondary)
+                            }
+                            HStack {
+                                Text("Basal")
+                                    .foregroundColor(.secondary)
+                                Spacer()
+                                let basal = state.basal
+                                Text(basal.formatted())
+                                Text(NSLocalizedString(" U/h", comment: " Units per hour"))
+                                    .foregroundColor(.secondary)
+                            }
+                            HStack {
+                                Text("Fraction")
+                                    .foregroundColor(.secondary)
+                                Spacer()
+                                let fraction = state.fraction
                                 Text(fraction.formatted())
                                 Text(fraction.formatted())
-                                    .foregroundColor(.orange)
+                            }
+                            if state.useFattyMealCorrectionFactor {
+                                HStack {
+                                    Text("Fatty Meal Factor")
+                                        .foregroundColor(.orange)
+                                    Spacer()
+                                    let fraction = state.fattyMealFactor
+                                    Text(fraction.formatted())
+                                        .foregroundColor(.orange)
+                                }
                             }
                             }
                         }
                         }
-                        Divider().fontWeight(.bold).foregroundColor(.white)
-                    }
-                    .padding()
+                    }.padding()
+
+                    Divider().fontWeight(.bold)
 
 
                     VStack(spacing: 2) {
                     VStack(spacing: 2) {
                         HStack {
                         HStack {
@@ -403,7 +407,7 @@ extension Bolus {
                     .padding()
                     .padding()
 
 
                     Divider()
                     Divider()
-                        .fontWeight(.bold).foregroundColor(.white)
+                        .fontWeight(.bold)
 
 
                     HStack {
                     HStack {
                         Text("Full Bolus")
                         Text("Full Bolus")
@@ -413,11 +417,9 @@ extension Bolus {
                         Text(insulin.formatted()).foregroundStyle(state.roundedWholeCalc < 0 ? Color.loopRed : Color.primary)
                         Text(insulin.formatted()).foregroundStyle(state.roundedWholeCalc < 0 ? Color.loopRed : Color.primary)
                         Text(unit)
                         Text(unit)
                             .foregroundColor(.secondary)
                             .foregroundColor(.secondary)
-                    }
-                    .padding()
+                    }.padding()
 
 
-                    Divider()
-                        .fontWeight(.bold)
+                    Divider().fontWeight(.bold)
 
 
                     HStack {
                     HStack {
                         Text("Result")
                         Text("Result")
@@ -457,6 +459,8 @@ extension Bolus {
                     }
                     }
                     .padding()
                     .padding()
 
 
+                    Divider()
+
                     if exceededMaxBolus {
                     if exceededMaxBolus {
                         HStack {
                         HStack {
                             let maxBolus = state.maxBolus
                             let maxBolus = state.maxBolus