|
|
@@ -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(
|