polscm32 2 lat temu
rodzic
commit
ef88eaa081

+ 11 - 3
FreeAPS.xcodeproj/project.pbxproj

@@ -1638,7 +1638,7 @@
 		388E594F25AD948C0019842D = {
 			isa = PBXGroup;
 			children = (
-				FEFA5C0D299F810B00765C17 /* Core_Data.xcdatamodeld */,
+				587A54C82BCDCE0F009D38E2 /* Model */,
 				38F3783A2613555C009DB701 /* Config.xcconfig */,
 				3818AA42274BBC1100843DB3 /* ConfigOverride.xcconfig */,
 				388E595A25AD948C0019842D /* FreeAPS */,
@@ -1735,7 +1735,6 @@
 		388E5A5A25B6F05F0019842D /* Helpers */ = {
 			isa = PBXGroup;
 			children = (
-				FEFA5C10299F814A00765C17 /* CoreDataStack.swift */,
 				38F37827261260DC009DB701 /* Color+Extensions.swift */,
 				389ECE042601144100D86C4F /* ConcurrentMap.swift */,
 				38192E0C261BAF980094D973 /* ConvenienceExtensions.swift */,
@@ -1776,7 +1775,6 @@
 				38FCF3FC25E997A80078B0D1 /* PumpHistoryStorage.swift */,
 				38F3B2EE25ED8E2A005C48AA /* TempTargetsStorage.swift */,
 				CE82E02428E867BA00473A9C /* AlertStorage.swift */,
-				1956FB202AFF79E200C7B4FF /* CoreDataStorage.swift */,
 			);
 			path = Storage;
 			sourceTree = "<group>";
@@ -2042,6 +2040,16 @@
 			path = View;
 			sourceTree = "<group>";
 		};
+		587A54C82BCDCE0F009D38E2 /* Model */ = {
+			isa = PBXGroup;
+			children = (
+				FEFA5C10299F814A00765C17 /* CoreDataStack.swift */,
+				1956FB202AFF79E200C7B4FF /* CoreDataStorage.swift */,
+				FEFA5C0D299F810B00765C17 /* Core_Data.xcdatamodeld */,
+			);
+			path = Model;
+			sourceTree = "<group>";
+		};
 		64271A287C92581EADCB47FA /* View */ = {
 			isa = PBXGroup;
 			children = (

+ 15 - 0
FreeAPS/Sources/APS/OpenAPS/OpenAPS.swift

@@ -10,6 +10,7 @@ final class OpenAPS {
     private let storage: FileStorage
 
     let coredataContext = CoreDataStack.shared.persistentContainer.viewContext // newBackgroundContext()
+    let context = CoreDataStack.shared.persistentContainer.newBackgroundContext()
 
     init(storage: FileStorage) {
         self.storage = storage
@@ -83,6 +84,20 @@ final class OpenAPS {
                     suggestion.timestamp = suggestion.deliverAt ?? clock
                     self.storage.save(suggestion, as: Enact.suggested)
 
+                    // save to core data asynchronously
+                    self.coredataContext.perform {
+                        let new = Determination(context: self.coredataContext)
+                        new.cob = suggestion.cob as? NSDecimalNumber
+                        new.iob = suggestion.iob as? NSDecimalNumber
+                        new.deliverAt = suggestion.deliverAt
+                        new.glucose = suggestion.bg as? NSDecimalNumber
+                        do {
+                            try self.coredataContext.save()
+                        } catch {
+                            print("failed")
+                        }
+                    }
+
                     // MARK: Save to CoreData also. To do: Remove JSON saving
 
                     if suggestion.tdd ?? 0 > 0 {

+ 0 - 40
FreeAPS/Sources/Helpers/CoreDataStack.swift

@@ -1,40 +0,0 @@
-import CoreData
-import Foundation
-
-class CoreDataStack: ObservableObject {
-    init() {}
-
-    static let shared = CoreDataStack()
-
-    lazy var persistentContainer: NSPersistentContainer = {
-        let container = NSPersistentContainer(name: "Core_Data")
-
-        container.loadPersistentStores(completionHandler: { _, error in
-            guard let error = error as NSError? else { return }
-            fatalError("Unresolved error: \(error), \(error.userInfo)")
-        })
-
-        return container
-    }()
-
-    func saveContext() {
-        let context = persistentContainer.viewContext
-
-        context.perform {
-            if context.hasChanges {
-                do {
-                    try context.save()
-                } catch {
-                    // Replace this implementation with code to handle the error appropriately.
-                    // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping    application, although it may be useful during development.
-                    let nserror = error as NSError
-                    fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
-                }
-            }
-        }
-    }
-
-    func delete(obj: NSManagedObject) {
-        persistentContainer.viewContext.delete(obj)
-    }
-}

+ 7 - 2
FreeAPS/Sources/Modules/Home/View/HomeRootView.swift

@@ -42,6 +42,11 @@ extension Home {
         ) var fetchedPercent: FetchedResults<Override>
 
         @FetchRequest(
+            entity: Determination.entity(),
+            sortDescriptors: [NSSortDescriptor(key: "deliverAt", ascending: false)]
+        ) var determination: FetchedResults<Determination>
+
+        @FetchRequest(
             entity: OverridePresets.entity(),
             sortDescriptors: [NSSortDescriptor(key: "name", ascending: true)], predicate: NSPredicate(
                 format: "name != %@", "" as String
@@ -462,7 +467,7 @@ extension Home {
                         .font(.system(size: 16))
                         .foregroundColor(Color.insulin)
                     Text(
-                        (numberFormatter.string(from: (state.suggestion?.iob ?? 0) as NSNumber) ?? "0") +
+                        (numberFormatter.string(from: (determination.first?.iob ?? 0) as NSNumber) ?? "0") +
                             NSLocalizedString(" U", comment: "Insulin unit")
                     )
                     .font(.system(size: 16, weight: .bold, design: .rounded))
@@ -475,7 +480,7 @@ extension Home {
                         .font(.system(size: 16))
                         .foregroundColor(.loopYellow)
                     Text(
-                        (numberFormatter.string(from: (state.suggestion?.cob ?? 0) as NSNumber) ?? "0") +
+                        (numberFormatter.string(from: (determination.first?.cob ?? 0) as NSNumber) ?? "0") +
                             NSLocalizedString(" g", comment: "gram of carbs")
                     )
                     .font(.system(size: 16, weight: .bold, design: .rounded))

+ 19 - 0
Model/CoreDataStack.swift

@@ -0,0 +1,19 @@
+import CoreData
+import Foundation
+
+class CoreDataStack: ObservableObject {
+    init() {}
+
+    static let shared = CoreDataStack()
+
+    lazy var persistentContainer: NSPersistentContainer = {
+        let container = NSPersistentContainer(name: "Core_Data")
+
+        container.loadPersistentStores(completionHandler: { _, error in
+            guard let error = error as NSError? else { return }
+            fatalError("Unresolved error: \(error), \(error.userInfo)")
+        })
+
+        return container
+    }()
+}

FreeAPS/Sources/APS/Storage/CoreDataStorage.swift → Model/CoreDataStorage.swift


+ 8 - 1
Core_Data.xcdatamodeld/Core_Data.xcdatamodel/contents

@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
-<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="22225" systemVersion="22G120" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
+<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="22222" systemVersion="22G120" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
     <entity name="Autosens_" representedClassName="Autosens_" syncable="YES" codeGenerationType="class">
         <attribute name="newisf" optional="YES" attributeType="Decimal" defaultValueString="0.0"/>
         <attribute name="ratio" optional="YES" attributeType="Decimal" defaultValueString="0.0"/>
@@ -30,6 +30,13 @@
         <attribute name="date" optional="YES" attributeType="Date" usesScalarValueType="NO"/>
         <attribute name="enteredBy" optional="YES" attributeType="String"/>
     </entity>
+    <entity name="Determination" representedClassName="Determination" syncable="YES" codeGenerationType="class">
+        <attribute name="cob" optional="YES" attributeType="Decimal" defaultValueString="0"/>
+        <attribute name="deliverAt" optional="YES" attributeType="Date" usesScalarValueType="NO"/>
+        <attribute name="enacted" optional="YES" attributeType="Boolean" usesScalarValueType="YES"/>
+        <attribute name="glucose" optional="YES" attributeType="Decimal" defaultValueString="0.0"/>
+        <attribute name="iob" optional="YES" attributeType="Decimal" defaultValueString="0.0"/>
+    </entity>
     <entity name="Fat" representedClassName="Fat" syncable="YES" codeGenerationType="class">
         <attribute name="date" optional="YES" attributeType="Date" usesScalarValueType="NO"/>
         <attribute name="enteredBy" optional="YES" attributeType="String"/>