Sfoglia il codice sorgente

Prevent double tapping.
Only save FPUs once to carbStorage.
Will speed up the UI significantly and will prevent the inadvertently double tapping and also avoid unnecessary writing to disc and coding of JSONs.

Jon Mårtensson 3 anni fa
parent
commit
50d133c7f2
1 ha cambiato i file con 14 aggiunte e 8 eliminazioni
  1. 14 8
      FreeAPS/Sources/Modules/AddCarbs/AddCarbsStateModel.swift

+ 14 - 8
FreeAPS/Sources/Modules/AddCarbs/AddCarbsStateModel.swift

@@ -60,22 +60,28 @@ extension AddCarbs {
                 // Group and Identify all FPUs together
                 // Group and Identify all FPUs together
                 let fpuID = UUID().uuidString
                 let fpuID = UUID().uuidString
 
 
-                // Loop and save all carb entries
+                // Create an array of all future carb equivalents.
+                var futureCarbArray = [CarbsEntry]()
                 while carbEquivalents > 0, numberOfEquivalents > 0 {
                 while carbEquivalents > 0, numberOfEquivalents > 0 {
+                    
                     if firstIndex {
                     if firstIndex {
                         useDate = useDate.addingTimeInterval(delay.minutes.timeInterval)
                         useDate = useDate.addingTimeInterval(delay.minutes.timeInterval)
                         firstIndex = false
                         firstIndex = false
                     } else { useDate = useDate.addingTimeInterval(interval.minutes.timeInterval) }
                     } else { useDate = useDate.addingTimeInterval(interval.minutes.timeInterval) }
-                    carbsStorage.storeCarbs([
-                        CarbsEntry(
-                            id: UUID().uuidString, createdAt: useDate, carbs: equivalent,
-                            enteredBy: CarbsEntry.manual, isFPU: true, fpuID: fpuID
-                        )
-                    ])
+
+                    let eachCarbEntry = CarbsEntry(
+                        id: UUID().uuidString, createdAt: useDate, carbs: equivalent, enteredBy: CarbsEntry.manual, isFPU: true,
+                        fpuID: fpuID
+                    )
+                    futureCarbArray.append(eachCarbEntry)
                     numberOfEquivalents -= 1
                     numberOfEquivalents -= 1
                 }
                 }
+                // Save the array
+                if carbEquivalents > 0 {
+                    carbsStorage.storeCarbs(futureCarbArray)
+                }
             } // ------------------------- END OF TPU ----------------------------------------
             } // ------------------------- END OF TPU ----------------------------------------
-            
+
             // Store the real carbs
             // Store the real carbs
             if carbs > 0 {
             if carbs > 0 {
                 carbsStorage
                 carbsStorage