Selaa lähdekoodia

Refactoring continued WIP

Deniz Cengiz 1 vuosi sitten
vanhempi
commit
220408ff42

+ 1 - 0
FreeAPS/Sources/Models/ContactTrickEntry.swift

@@ -1,6 +1,7 @@
 import SwiftUI
 
 struct ContactTrickEntry: Hashable, Sendable {
+    var id = UUID()
     var layout: ContactTrickLayout = .single
     var ring: ContactTrickLargeRing = .none
     var primary: ContactTrickValue = .glucose

+ 17 - 1
FreeAPS/Sources/Modules/ContactTrick/ContactTrickStateModel.swift

@@ -8,6 +8,7 @@ extension ContactTrick {
         private(set) var changed: Bool = false
 
         @ObservationIgnored @Injected() var contactTrickManager: ContactTrickManager!
+        @ObservationIgnored @Injected() var contactTrickStorage: ContactTrickStorage!
 
         var units: GlucoseUnits = .mmolL
 
@@ -42,11 +43,26 @@ extension ContactTrick {
             changed = true
         }
 
-        func save() {
+        @MainActor func save() async {
             syncInProgress = true
             let contacts = items.map { item -> ContactTrickEntry in
                 item.entry
             }
+
+            let didUpdateStatus = await contactTrickManager.updateContacts(contacts: contacts)
+
+            //            for contact in contacts {
+            //                await contactTrickStorage.storeContactTrickEntry(contact)
+            //            }
+
+            syncInProgress = didUpdateStatus
+            changed = didUpdateStatus
+
+            if didUpdateStatus {
+                contacts.enumerated().forEach { index, item in
+                    self.items[index].entry = item
+                }
+            }
 //            provider.saveContacts(contacts)
 //                .receive(on: DispatchQueue.main)
 //                .sink { _ in

+ 1 - 1
FreeAPS/Sources/Modules/ContactTrick/View/ContactTrickRootView.swift

@@ -30,7 +30,7 @@ extension ContactTrick {
                             if state.syncInProgress {
                                 ProgressView().padding(.trailing, 10)
                             }
-                            Button { state.save() }
+                            Button { Task { await state.save() } }
                             label: {
                                 Text(state.syncInProgress ? "Saving..." : "Save")
                             }

+ 26 - 32
FreeAPS/Sources/Services/ContactTrick/ContactTrickManager.swift

@@ -6,7 +6,7 @@ import Foundation
 import Swinject
 
 protocol ContactTrickManager {
-    func updateContacts(contacts: [ContactTrickEntry]) async
+    func updateContacts(contacts: [ContactTrickEntry]) async -> Bool
     var currentContacts: [ContactTrickEntry] { get }
 }
 
@@ -217,30 +217,27 @@ final class BaseContactTrickManager: NSObject, ContactTrickManager, Injectable {
                     self.state.eventualBG = eventualBGAsString.map { "⇢ " + $0 }
                 }
 
-                guard (try? JSONEncoder().encode(state)) != nil else {
-                    warning(.service, "Cannot encode watch state")
-                    return
-                }
+//                guard (try? JSONEncoder().encode(state)) != nil else {
+//                    warning(.service, "Cannot encode watch state")
+//                    return
+//                }
 
                 if contacts.isNotEmpty, CNContactStore.authorizationStatus(for: .contacts) == .authorized {
-                    let newContacts = contacts.enumerated().map { index, entry in
-                        self.renderContact(entry, index + 1, self.state)
-                    }
-
-                    // Find new entries in newContacts that are not in contacts
-                    let newEntries = newContacts.filter { newContact in
-                        !self.contacts.contains(where: { $0.contactId == newContact.contactId })
-                    }
-
-                    // When we create new contacts we store the IDs, in that case we need to write into the settings storage
-                    // TODO: We actually need to UPDATE the respective entry in CD, so rather RETURN the contactId here and UPDATE the respective entry in CD.
-//                    // Save the new entries into Core Data
-//                    for newEntry in newEntries {
-//                        Task {
-//                            await self.contactTrickStorage.storeContactTrickEntry(newEntry)
-//                        }
-//                    }
-
+                    let newContacts = contacts.enumerated()
+                        .map { index, entry in self.renderContact(entry, index + 1, self.state) }
+                    
+                    // TODO: existiert die zurück gegebene contact ID in CD
+                    // wenn ja, eintrag ignorieren
+                    // wemnn nein, eintrag neu einlegen
+                
+                    
+                    if newContacts != contacts {
+                        // when we create new contacts we store the IDs, in that case we need to write into the settings storage
+                        for contactToStore in newContacts {
+                            Task {
+                                await self.contactTrickStorage.storeContactTrickEntry(contactToStore)
+                            }
+                        } }
                     contacts = newContacts
                 }
             }
@@ -250,22 +247,19 @@ final class BaseContactTrickManager: NSObject, ContactTrickManager, Injectable {
         }
     }
 
-    func updateContacts(contacts: [ContactTrickEntry]) async {
+    func updateContacts(contacts: [ContactTrickEntry]) async -> Bool {
         self.contacts = contacts
         let newIds = contacts.compactMap(\.contactId)
         let knownSet = Set(knownIds)
         let newSet = Set(newIds)
         let removedIds = knownSet.subtracting(newSet)
 
-        await viewContext.perform {
-            let fetchRequest: NSFetchRequest<ContactTrickEntryStored> = ContactTrickEntryStored.fetchRequest()
-            fetchRequest.predicate = NSPredicate(format: "contactId IN %@", removedIds)
-
+        return await context.perform {
             do {
+                let fetchRequest: NSFetchRequest<ContactTrickEntryStored> = ContactTrickEntryStored.fetchRequest()
+                fetchRequest.predicate = NSPredicate(format: "contactId IN %@", removedIds)
                 let objectIDsToDelete = try self.viewContext.fetch(fetchRequest).compactMap(\.objectID)
 
-                guard !objectIDsToDelete.isEmpty else { return }
-
                 for objectID in objectIDsToDelete {
                     Task {
                         await self.contactTrickStorage.deleteContactTrickEntry(objectID)
@@ -276,11 +270,12 @@ final class BaseContactTrickManager: NSObject, ContactTrickManager, Injectable {
                     await self.configureContactTrickState()
                 }
                 self.knownIds = self.contacts.compactMap(\.contactId)
-
+                return true
             } catch let error as NSError {
                 debugPrint(
                     "\(DebuggingIdentifiers.failed) \(#file) \(#function) Failed to delete Contact Trick Entry: \(error.userInfo)"
                 )
+                return false
             }
         }
     }
@@ -320,7 +315,6 @@ final class BaseContactTrickManager: NSObject, ContactTrickManager, Injectable {
                 print("in handleEnabledContact, failed to fetch the contact: \(error.localizedDescription)")
                 return entry
             }
-
         } else {
             print("no contact \(index) - creating")
             mutableContact = createNewContact(