Sam King 1 год назад
Родитель
Сommit
0e0f272ee2

+ 5 - 1
Trio.xcodeproj/project.pbxproj

@@ -290,6 +290,8 @@
 		3BC0AA412DA8B900000DF7B7 /* iob-history-prepare.js in Resources */ = {isa = PBXBuildFile; fileRef = 3BC0AA402DA8B8F7000DF7B7 /* iob-history-prepare.js */; };
 		3BC26E552D7418830066ACD6 /* IobSuspendTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3BC26E542D7418830066ACD6 /* IobSuspendTests.swift */; };
 		3BC4053B2D931620006A03E9 /* IobJsonTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3BC4053A2D931620006A03E9 /* IobJsonTests.swift */; };
+		3BAD36B22D7CDC1A00CC298D /* MainLoadingView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3BAD36B12D7CDC1400CC298D /* MainLoadingView.swift */; };
+		3BAD36CC2D7D420E00CC298D /* CoreDataInitializationCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3BAD36CB2D7D420500CC298D /* CoreDataInitializationCoordinator.swift */; };
 		3BCE75B32D4B38AE009E9453 /* InsulinSensitivities+Convert.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3BCE75B22D4B38A0009E9453 /* InsulinSensitivities+Convert.swift */; };
 		3BCE75B52D4B391F009E9453 /* Decimal+rounding.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3BCE75B42D4B3917009E9453 /* Decimal+rounding.swift */; };
 		3BCA5F7C2DC7B16400A7EAC7 /* pumphistory-with-external.json in Resources */ = {isa = PBXBuildFile; fileRef = 3BCA5F7B2DC7B15400A7EAC7 /* pumphistory-with-external.json */; };
@@ -1164,6 +1166,8 @@
 		3BC0AA402DA8B8F7000DF7B7 /* iob-history-prepare.js */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.javascript; path = "iob-history-prepare.js"; sourceTree = "<group>"; };
 		3BC26E542D7418830066ACD6 /* IobSuspendTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IobSuspendTests.swift; sourceTree = "<group>"; };
 		3BC4053A2D931620006A03E9 /* IobJsonTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IobJsonTests.swift; sourceTree = "<group>"; };
+		3BAD36B12D7CDC1400CC298D /* MainLoadingView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainLoadingView.swift; sourceTree = "<group>"; };
+		3BAD36CB2D7D420500CC298D /* CoreDataInitializationCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CoreDataInitializationCoordinator.swift; sourceTree = "<group>"; };
 		3BCE75B22D4B38A0009E9453 /* InsulinSensitivities+Convert.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "InsulinSensitivities+Convert.swift"; sourceTree = "<group>"; };
 		3BCE75B42D4B3917009E9453 /* Decimal+rounding.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Decimal+rounding.swift"; sourceTree = "<group>"; };
 		3BCA5F7B2DC7B15400A7EAC7 /* pumphistory-with-external.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = "pumphistory-with-external.json"; sourceTree = "<group>"; };
@@ -2740,10 +2744,10 @@
 		3B5CD1EB2D4912A600CE213C /* OpenAPSSwift */ = {
 			isa = PBXGroup;
 			children = (
-				DD9E6DA02D59A11200514CEC /* Meal */,
 				3B5CD2A42D4AEA5D00CE213C /* Extensions */,
 				3B1C5C282D68E1E3004E9273 /* Iob */,
 				3BEA3ADF2D58F79700A67A1D /* Logging */,
+				DD9E6DA02D59A11200514CEC /* Meal */,
 				3B5CD2B22D4AEA6600CE213C /* Models */,
 				3B5CD2972D4AEA3C00CE213C /* Profile */,
 				3B5CD2A02D4AEA5100CE213C /* Utils */,

+ 10 - 3
Trio/Sources/APS/OpenAPSSwift/Meal/MealGenerator.swift

@@ -10,9 +10,16 @@ enum MealGeneratorError {
         glucoseHistory: [BloodGlucose]
     ) -> ComputedCarbs? {
         var treatments: [MealInput] = MealHistory.findMealInputs(pumpHistory: pumpHistory, carbHistory: carbHistory)
-        
+
         // TODO: do we need to handle the clock timezone handling? We'll parse in a proper Swift Date anyhow
-        
-        return MealTotal.recentCarbs(treatments: treatments, pumpHistory: pumpHistory, profile: profile, basalProfile: basalProfile, glucose: glucoseHistory, time: clock)
+
+        return MealTotal.recentCarbs(
+            treatments: treatments,
+            pumpHistory: pumpHistory,
+            profile: profile,
+            basalProfile: basalProfile,
+            glucose: glucoseHistory,
+            time: clock
+        )
     }
 }

+ 1 - 1
Trio/Sources/APS/OpenAPSSwift/Meal/MealHistory.swift

@@ -107,7 +107,7 @@ enum MealHistory {
                     duplicates += 1
                 }
             }
-            
+
             // Trio will never send any pump history contents of the following types to oref
             // Ignoring for JavaScript -> Swift port.
             // .bolusWizard

+ 40 - 36
Trio/Sources/APS/OpenAPSSwift/Meal/MealTotal.swift

@@ -30,43 +30,48 @@ struct COBInputs {
 }
 
 enum MealTotal {
-    static func recentCarbs(treatments: [MealInput], pumpHistory: [PumpHistoryEvent], profile: Profile, basalProfile: [BasalProfileEntry], glucose: [BloodGlucose], time: Date) -> ComputedCarbs? {
+    static func recentCarbs(
+        treatments: [MealInput],
+        pumpHistory: [PumpHistoryEvent],
+        profile: Profile,
+        basalProfile: [BasalProfileEntry],
+        glucose: [BloodGlucose],
+        time: Date
+    ) -> ComputedCarbs? {
         guard treatments.isNotEmpty else { return nil }
-        
+
         var _treatments = treatments
-        var carbs: Decimal = Decimal(0)
-        var nsCarbs: Decimal = Decimal(0)
-        var bwCarbs: Decimal = Decimal(0)
-        var journalCarbs: Decimal = Decimal(0)
+        var carbs = Decimal(0)
+        var nsCarbs = Decimal(0)
+        var bwCarbs = Decimal(0)
+        var journalCarbs = Decimal(0)
         let mealCarbTime: TimeInterval = time.timeIntervalSince1970
         var lastCarbTime: TimeInterval = 0
         var bwFound: Bool = false
-        
+
         let iobInputs = IOBInput(profile: profile, history: pumpHistory)
         var cobInputs = COBInputs(glucoseData: glucose, iobInputs: iobInputs, basalProfile: basalProfile, mealTime: mealCarbTime)
-        var mealCOB: Decimal = Decimal(0)
+        var mealCOB = Decimal(0)
 
-        
         _treatments.sort(by: {
             $0.timestamp > $1.timestamp
         })
-        
-        var carbsToRemove: Decimal = Decimal(0)
-           var nsCarbsToRemove: Decimal = Decimal(0)
-           var bwCarbsToRemove: Decimal = Decimal(0)
-           var journalCarbsToRemove: Decimal = Decimal(0)
-        
+
+        var carbsToRemove = Decimal(0)
+        var nsCarbsToRemove = Decimal(0)
+        var bwCarbsToRemove = Decimal(0)
+        var journalCarbsToRemove = Decimal(0)
+
         for treatment in _treatments {
             let now = time.timeIntervalSince1970
-    
-           // Use new maxMealAbsorptionTime setting here instead of default 6 hrs
+
+            // Use new maxMealAbsorptionTime setting here instead of default 6 hrs
             var carbWindow = now - TimeInterval(hours: Double(truncating: profile.maxMealAbsorptionTime as NSNumber))
-            
+
             let treatmentDate = treatment.timestamp
             let treatmentTime = treatmentDate.timeIntervalSince1970
-            
-            if (treatmentTime > carbWindow && treatmentTime <= now) {
-                
+
+            if treatmentTime > carbWindow, treatmentTime <= now {
                 if var _carbs = treatment.carbs, carbs >= 1 {
                     if var _nsCarbs = treatment.nsCarbs, nsCarbs >= 1 {
                         nsCarbs += _nsCarbs
@@ -78,18 +83,18 @@ enum MealTotal {
                     } else {
                         print("Treatment carbs unclassified: \(treatment)")
                     }
-                    
+
                     carbs += _carbs
-                    
+
                     cobInputs.mealTime = treatmentTime
-                    lastCarbTime = max(lastCarbTime,treatmentTime)
-                    
+                    lastCarbTime = max(lastCarbTime, treatmentTime)
+
                     let myCarbsAbsorbed = Decimal(0) // TODO: call perted cob method here
-                    
+
                     // TODO: add logging?
                     let myMealCOB = max(0, carbs - myCarbsAbsorbed)
                     mealCOB = max(mealCOB, myMealCOB)
-                    
+
                     if myMealCOB < mealCOB {
                         carbsToRemove += treatment.carbs ?? 0
                         if var _nsCarbs = treatment.nsCarbs, nsCarbs >= 1 {
@@ -100,28 +105,28 @@ enum MealTotal {
                             journalCarbsToRemove += _journalCarbs
                         }
                     } else {
-                        carbsToRemove = 0;
-                        nsCarbsToRemove = 0;
-                        bwCarbsToRemove = 0;
+                        carbsToRemove = 0
+                        nsCarbsToRemove = 0
+                        bwCarbsToRemove = 0
                     }
                 }
             }
         }
-        
+
         // only include carbs actually used in calculating COB
         carbs -= carbsToRemove
         nsCarbs -= nsCarbsToRemove
         bwCarbs -= bwCarbsToRemove
         journalCarbs -= journalCarbsToRemove
-        
+
         // calculate the current deviation and steepest deviation downslope over the last hour
         cobInputs.ciTime = time.timeIntervalSince1970
         cobInputs.mealTime = TimeInterval(hours: Double(truncating: profile.maxMealAbsorptionTime as NSNumber))
-        
+
         // set a hard upper limit on COB to mitigate impact of erroneous or malicious carb entry
         mealCOB = min(profile.maxCOB, mealCOB)
         /// omiting maxCOB check here, the setting is not Optional in Swift and must be part of profile
-    
+
         // if currentDeviation is null or maxDeviation is 0, set mealCOB to 0 for zombie-carb safety
         // TODO: make these adjustments once we have cob.js ported
 //        if (typeof(c.currentDeviation) === 'undefined' || c.currentDeviation === null) {
@@ -134,7 +139,7 @@ enum MealTotal {
 //            console.error("Warning: setting mealCOB to 0 because maxDeviation is 0 or undefined");
 //            mealCOB = 0;
 //        }
-        
+
         return ComputedCarbs(
             carbs: carbs,
             nsCarbs: nsCarbs,
@@ -152,4 +157,3 @@ enum MealTotal {
         )
     }
 }
-