Просмотр исходного кода

Add insulin without actual bolusing

Ivan Valkou 5 лет назад
Родитель
Сommit
19740d9c29

+ 17 - 15
FreeAPS/Sources/APS/Storage/PumpHistoryStorage.swift

@@ -9,6 +9,7 @@ protocol PumpHistoryObserver {
 
 protocol PumpHistoryStorage {
     func storePumpEvents(_ events: [NewPumpEvent])
+    func storeEvents(_ events: [PumpHistoryEvent])
     func storeJournalCarbs(_ carbs: Int)
     func recent() -> [PumpHistoryEvent]
     func nightscoutTretmentsNotUploaded() -> [NigtscoutTreatment]
@@ -132,7 +133,7 @@ final class BasePumpHistoryStorage: PumpHistoryStorage, Injectable {
                 }
             }
 
-            self.processNewEvents(eventsToStore)
+            self.storeEvents(eventsToStore)
         }
     }
 
@@ -151,23 +152,24 @@ final class BasePumpHistoryStorage: PumpHistoryStorage, Injectable {
                     carbInput: carbs
                 )
             ]
-            self.processNewEvents(eventsToStore)
+            self.storeEvents(eventsToStore)
         }
     }
 
-    private func processNewEvents(_ events: [PumpHistoryEvent]) {
-        dispatchPrecondition(condition: .onQueue(processQueue))
-        let file = OpenAPS.Monitor.pumpHistory
-        var uniqEvents: [PumpHistoryEvent] = []
-        storage.transaction { storage in
-            storage.append(events, to: file, uniqBy: \.id)
-            uniqEvents = storage.retrieve(file, as: [PumpHistoryEvent].self)?
-                .filter { $0.timestamp.addingTimeInterval(1.days.timeInterval) > Date() }
-                .sorted { $0.timestamp > $1.timestamp } ?? []
-            storage.save(Array(uniqEvents), as: file)
-        }
-        broadcaster.notify(PumpHistoryObserver.self, on: processQueue) {
-            $0.pumpHistoryDidUpdate(uniqEvents)
+    func storeEvents(_ events: [PumpHistoryEvent]) {
+        processQueue.async {
+            let file = OpenAPS.Monitor.pumpHistory
+            var uniqEvents: [PumpHistoryEvent] = []
+            self.storage.transaction { storage in
+                storage.append(events, to: file, uniqBy: \.id)
+                uniqEvents = storage.retrieve(file, as: [PumpHistoryEvent].self)?
+                    .filter { $0.timestamp.addingTimeInterval(1.days.timeInterval) > Date() }
+                    .sorted { $0.timestamp > $1.timestamp } ?? []
+                storage.save(Array(uniqEvents), as: file)
+            }
+            self.broadcaster.notify(PumpHistoryObserver.self, on: self.processQueue) {
+                $0.pumpHistoryDidUpdate(uniqEvents)
+            }
         }
     }
 

+ 9 - 1
FreeAPS/Sources/Models/CarbsEntry.swift

@@ -1,11 +1,19 @@
 import Foundation
 
-struct CarbsEntry: JSON, Equatable {
+struct CarbsEntry: JSON, Equatable, Hashable {
     let createdAt: Date
     let carbs: Decimal
     let enteredBy: String?
 
     static let manual = "freeaps-x"
+
+    static func == (lhs: CarbsEntry, rhs: CarbsEntry) -> Bool {
+        lhs.createdAt == rhs.createdAt
+    }
+
+    func hash(into hasher: inout Hasher) {
+        hasher.combine(createdAt)
+    }
 }
 
 extension CarbsEntry {

+ 9 - 1
FreeAPS/Sources/Models/TempTarget.swift

@@ -1,6 +1,6 @@
 import Foundation
 
-struct TempTarget: JSON, Identifiable, Equatable {
+struct TempTarget: JSON, Identifiable, Equatable, Hashable {
     var id = UUID().uuidString
     let name: String?
     var createdAt: Date
@@ -17,6 +17,14 @@ struct TempTarget: JSON, Identifiable, Equatable {
     var displayName: String {
         name ?? reason ?? TempTarget.custom
     }
+
+    static func == (lhs: TempTarget, rhs: TempTarget) -> Bool {
+        lhs.createdAt == rhs.createdAt
+    }
+
+    func hash(into hasher: inout Hasher) {
+        hasher.combine(createdAt)
+    }
 }
 
 extension TempTarget {

+ 29 - 0
FreeAPS/Sources/Modules/Bolus/BolusViewModel.swift

@@ -7,6 +7,7 @@ extension Bolus {
         @Injected() var apsManager: APSManager!
         @Injected() var broadcaster: Broadcaster!
         @Injected() var settingsManager: SettingsManager!
+        @Injected() var pumpHistotyStorage: PumpHistoryStorage!
         @Published var amount: Decimal = 0
         @Published var inslinRecommended: Decimal = 0
         @Published var inslinRequired: Decimal = 0
@@ -26,6 +27,10 @@ extension Bolus {
         override func subscribe() {
             setupInsulinRequired()
             broadcaster.register(SuggestionObserver.self, observer: self)
+
+            if waitForSuggestionInitial {
+                apsManager.determineBasal().sink { _ in }.store(in: &lifetime)
+            }
         }
 
         func add() {
@@ -41,6 +46,30 @@ extension Bolus {
                 .store(in: &lifetime)
         }
 
+        func addWithoutBolus() {
+            guard amount > 0 else {
+                showModal(for: nil)
+                return
+            }
+
+            pumpHistotyStorage.storeEvents(
+                [
+                    PumpHistoryEvent(
+                        id: UUID().uuidString,
+                        type: .bolus,
+                        timestamp: Date(),
+                        amount: amount,
+                        duration: nil,
+                        durationMin: nil,
+                        rate: nil,
+                        temp: nil,
+                        carbInput: nil
+                    )
+                ]
+            )
+            showModal(for: nil)
+        }
+
         func setupInsulinRequired() {
             DispatchQueue.main.async {
                 self.inslinRequired = self.provider.suggestion?.insulinReq ?? 0

+ 3 - 0
FreeAPS/Sources/Modules/Bolus/View/BolusRootView.swift

@@ -63,6 +63,9 @@ extension Bolus {
                         if viewModel.waitForSuggestionInitial {
                             Button { viewModel.showModal(for: nil) }
                             label: { Text("Continue without bolus") }
+                        } else {
+                            Button { viewModel.addWithoutBolus() }
+                            label: { Text("Add insulin without actual bolusing") }
                         }
                     }
                 }

+ 0 - 2
FreeAPS/Sources/Modules/Home/HomeViewModel.swift

@@ -277,12 +277,10 @@ extension Home.ViewModel:
 
     func tempTargetsDidUpdate(_: [TempTarget]) {
         setupTempTargets()
-        apsManager.determineBasal().sink { _ in }.store(in: &lifetime)
     }
 
     func carbsDidUpdate(_: [CarbsEntry]) {
         setupCarbs()
-        apsManager.determineBasal().sink { _ in }.store(in: &lifetime)
     }
 
     func enactedSuggestionDidUpdate(_ suggestion: Suggestion) {