Преглед изворни кода

Completely new code for FPU conversion. This mimics the Warsaw method calculator more.

Jon Mårtensson пре 3 година
родитељ
комит
844353fb49

+ 3 - 0
FreeAPS/Sources/Localizations/Main/en.lproj/Localizable.strings

@@ -1045,6 +1045,9 @@ Enact a temp Basal or a temp target */
 
 /* -------------------------------------------- FPU Strings ------------------------------------------------------*/
 
+/* Enable FPU */
+"Enable" = "Enable";
+
 /* Header */
 "Convert Fat and Protein" = "Convert Fat and Protein";
 

+ 4 - 1
FreeAPS/Sources/Localizations/Main/sv.lproj/Localizable.strings

@@ -1043,8 +1043,11 @@ Enact a temp Basal or a temp target */
 "Total" = "Totalt";
 
 /* -------------------------------------------- FPU Strings ------------------------------------------------------*/
-/* Header */
 
+/* Enable FPU */
+"Enable" = "Aktivera";
+
+/* Header */
 "Convert Fat and Protein" = "Omvandla fett orch protein";
 
 /* Header */

+ 2 - 2
FreeAPS/Sources/Models/FreeAPSSettings.swift

@@ -27,7 +27,7 @@ struct FreeAPSSettings: JSON, Equatable {
     var displayStatistics: Bool = false
     var useFPUconversion: Bool = false
     var individualAdjustmentFactor: Decimal = 0.5
-    var timeCap: Decimal = 8
+    var timeCap: Int = 8
     var minuteInterval: Int = 30
     var delay: Int = 60
 }
@@ -110,7 +110,7 @@ extension FreeAPSSettings: Decodable {
             settings.individualAdjustmentFactor = individualAdjustmentFactor
         }
 
-        if let timeCap = try? container.decode(Decimal.self, forKey: .timeCap) {
+        if let timeCap = try? container.decode(Int.self, forKey: .timeCap) {
             settings.timeCap = timeCap
         }
 

+ 44 - 22
FreeAPS/Sources/Modules/AddCarbs/AddCarbsStateModel.swift

@@ -23,37 +23,59 @@ extension AddCarbs {
                 return
             }
 
-            let interval = settings.settings.minuteInterval
-            let timeCap = settings.settings.timeCap * (60 / Decimal(interval))
-            let adjustment = settings.settings.individualAdjustmentFactor
-            let delay = settings.settings.delay
+            if useFPU {
+                // ----------- FPU ------------------------------------------------
+                let interval = settings.settings.minuteInterval // Interval betwwen carbs
+                let timeCap = settings.settings.timeCap // Max Duration
+                let adjustment = settings.settings.individualAdjustmentFactor
+                let delay = settings.settings.delay // Tme before first future carb entry
 
-            // Convert fat and protein to carb equivalents and store as future carbs
-            let fpucarb = 0.4 * protein + 0.9 * fat
-            let fpus = (fat * 9.0 + protein * 4.0) / 100.0
-            var counter: Decimal = (fpus * 2) - 1.0
-            counter = max(timeCap, counter)
-            var roundedCounter: Decimal = 0
-            NSDecimalRound(&roundedCounter, &counter, 0, .up)
-            let carbequiv = (fpucarb / roundedCounter) * adjustment
-            let firstDate = date.addingTimeInterval(delay.minutes.timeInterval)
-            var previousDate = date
+                let kcal = protein * 4 + fat * 9
+                let carbEquivalents = (kcal / 10) * adjustment
+                let fpus = carbEquivalents / 10
 
-            while counter > 0, carbequiv > 0 {
-                var useDate = date + 1 * Double(interval * 60)
-                // Fix Interval and Delay
-                useDate = max(previousDate.addingTimeInterval(interval.minutes.timeInterval), useDate, firstDate)
-                if useDate > previousDate {
+                // Duration in hours used for extended boluses with Warsaw Method. Here used for total duration of the computed carbquivalents instead, excluding the configurable delay.
+                var computedDuration = 0
+                switch fpus {
+                case ..<2:
+                    computedDuration = 3
+                case 2 ... 3:
+                    computedDuration = 4
+                case 3 ... 4:
+                    computedDuration = 5
+                default:
+                    computedDuration = timeCap
+                }
+
+                // Size of each created carb equivalent if 60 minutes interval
+                var carbPortions: Decimal = carbEquivalents / Decimal(computedDuration)
+                // Adjust for interval setting other than 60 minutes
+                carbPortions /= Decimal(60 / interval)
+                // Number of equivalents
+                var numberOfPortions = carbEquivalents / carbPortions
+                // Only use delay in first loop
+                var firstIndex = true
+                // New date for each carb equivalent
+                var useDate = Date()
+                
+                // Loop and save all carb entries
+                while carbEquivalents > 0, numberOfPortions > 0 {
+                    if firstIndex {
+                        useDate = date.addingTimeInterval(delay.minutes.timeInterval)
+                        firstIndex = false
+                    } else { useDate = date.addingTimeInterval(interval.minutes.timeInterval) }
                     carbsStorage.storeCarbs([
                         CarbsEntry(
-                            id: UUID().uuidString, createdAt: useDate, carbs: carbequiv,
+                            id: UUID().uuidString, createdAt: useDate, carbs: carbPortions,
                             enteredBy: CarbsEntry.manual
                         )
                     ])
+                    numberOfPortions -= 1
+                    date = useDate // Update date
                 }
-                previousDate = useDate
-                counter -= 1
             }
+            // ------------------------- END OF TPU -----------------------------------------------
+
             // Store the real carbs
             if carbs > 0 {
                 carbsStorage

+ 3 - 4
FreeAPS/Sources/Modules/FPUConfig/FPUConfigStateModel.swift

@@ -10,11 +10,10 @@ extension FPUConfig {
 
         override func subscribe() {
             subscribeSetting(\.useFPUconversion, on: $useFPUconversion) { useFPUconversion = $0 }
-            subscribeSetting(\.timeCap, on: $timeCap) { timeCap = $0 }
 
-            subscribeSetting(\.timeCap, on: $timeCap, initial: {
-                let value = max(min($0, 12), 6)
-                timeCap = value
+            subscribeSetting(\.timeCap, on: $timeCap.map(Int.init), initial: {
+                let value = max(min($0, 12), 5)
+                timeCap = Decimal(value)
             }, map: {
                 $0
             })