|
@@ -20,7 +20,7 @@ final class BaseContactTrickManager: NSObject, ContactTrickManager, Injectable {
|
|
|
@Injected() private var broadcaster: Broadcaster!
|
|
@Injected() private var broadcaster: Broadcaster!
|
|
|
@Injected() private var settingsManager: SettingsManager!
|
|
@Injected() private var settingsManager: SettingsManager!
|
|
|
@Injected() private var apsManager: APSManager!
|
|
@Injected() private var apsManager: APSManager!
|
|
|
- @Injected() private var storage: FileStorage!
|
|
|
|
|
|
|
+ @Injected() private var contactTrickStorage: ContactTrickStorage!
|
|
|
@Injected() private var carbsStorage: CarbsStorage!
|
|
@Injected() private var carbsStorage: CarbsStorage!
|
|
|
@Injected() private var tempTargetsStorage: TempTargetsStorage!
|
|
@Injected() private var tempTargetsStorage: TempTargetsStorage!
|
|
|
@Injected() private var glucoseStorage: GlucoseStorage!
|
|
@Injected() private var glucoseStorage: GlucoseStorage!
|
|
@@ -84,6 +84,9 @@ final class BaseContactTrickManager: NSObject, ContactTrickManager, Injectable {
|
|
|
// contacts = storage.retrieve(OpenAPS.Settings.contactTrick, as: [ContactTrickEntry].self)
|
|
// contacts = storage.retrieve(OpenAPS.Settings.contactTrick, as: [ContactTrickEntry].self)
|
|
|
// ?? [ContactTrickEntry](from: OpenAPS.defaults(for: OpenAPS.Settings.contactTrick))
|
|
// ?? [ContactTrickEntry](from: OpenAPS.defaults(for: OpenAPS.Settings.contactTrick))
|
|
|
// ?? []
|
|
// ?? []
|
|
|
|
|
+ Task {
|
|
|
|
|
+ contacts = await contactTrickStorage.fetchContactTrickEntries()
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
knownIds = contacts.compactMap(\.contactId)
|
|
knownIds = contacts.compactMap(\.contactId)
|
|
|
|
|
|
|
@@ -115,13 +118,6 @@ final class BaseContactTrickManager: NSObject, ContactTrickManager, Injectable {
|
|
|
}
|
|
}
|
|
|
}.store(in: &subscriptions)
|
|
}.store(in: &subscriptions)
|
|
|
|
|
|
|
|
- coreDataPublisher?.filterByEntityName("OverrideStored").sink { [weak self] _ in
|
|
|
|
|
- guard let self = self else { return }
|
|
|
|
|
- Task {
|
|
|
|
|
- await self.configureState()
|
|
|
|
|
- }
|
|
|
|
|
- }.store(in: &subscriptions)
|
|
|
|
|
-
|
|
|
|
|
// Observes Deletion of Glucose Objects
|
|
// Observes Deletion of Glucose Objects
|
|
|
coreDataPublisher?.filterByEntityName("GlucoseStored").sink { [weak self] _ in
|
|
coreDataPublisher?.filterByEntityName("GlucoseStored").sink { [weak self] _ in
|
|
|
guard let self = self else { return }
|
|
guard let self = self else { return }
|
|
@@ -155,24 +151,6 @@ final class BaseContactTrickManager: NSObject, ContactTrickManager, Injectable {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private func fetchLatestOverride() async -> NSManagedObjectID? {
|
|
|
|
|
- let results = await CoreDataStack.shared.fetchEntitiesAsync(
|
|
|
|
|
- ofType: OverrideStored.self,
|
|
|
|
|
- onContext: context,
|
|
|
|
|
- predicate: NSPredicate.predicateForOneDayAgo,
|
|
|
|
|
- key: "date",
|
|
|
|
|
- ascending: false,
|
|
|
|
|
- fetchLimit: 1,
|
|
|
|
|
- propertiesToFetch: ["enabled", "percentage", "objectID"]
|
|
|
|
|
- )
|
|
|
|
|
-
|
|
|
|
|
- return await context.perform {
|
|
|
|
|
- guard let fetchedResults = results as? [[String: Any]] else { return nil }
|
|
|
|
|
-
|
|
|
|
|
- return fetchedResults.compactMap { $0["objectID"] as? NSManagedObjectID }.first
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
private func fetchGlucose() async -> [NSManagedObjectID] {
|
|
private func fetchGlucose() async -> [NSManagedObjectID] {
|
|
|
let results = await CoreDataStack.shared.fetchEntitiesAsync(
|
|
let results = await CoreDataStack.shared.fetchEntitiesAsync(
|
|
|
ofType: GlucoseStored.self,
|
|
ofType: GlucoseStored.self,
|
|
@@ -251,19 +229,36 @@ final class BaseContactTrickManager: NSObject, ContactTrickManager, Injectable {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private func sendState() {
|
|
private func sendState() {
|
|
|
|
|
+ // TODO: why does this have to be JSON ?!
|
|
|
guard let data = try? JSONEncoder().encode(state) else {
|
|
guard let data = try? JSONEncoder().encode(state) else {
|
|
|
warning(.service, "Cannot encode watch state")
|
|
warning(.service, "Cannot encode watch state")
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if contacts.isNotEmpty, CNContactStore.authorizationStatus(for: .contacts) == .authorized {
|
|
if contacts.isNotEmpty, CNContactStore.authorizationStatus(for: .contacts) == .authorized {
|
|
|
- let newContacts = contacts.enumerated().map { index, entry in renderContact(entry, index + 1, self.state) }
|
|
|
|
|
- if newContacts != contacts {
|
|
|
|
|
- // when we create new contacts we store the IDs, in that case we need to write into the settings storage
|
|
|
|
|
|
|
+ let newContacts = contacts.enumerated().map { index, entry in
|
|
|
|
|
+ renderContact(entry, index + 1, self.state)
|
|
|
|
|
+ }
|
|
|
|
|
+ // if newContacts != contacts {
|
|
|
|
|
+ // // when we create new contacts we store the IDs, in that case we need to write into the settings storage
|
|
|
|
|
+ //
|
|
|
|
|
+ // // TODO: save this in CD
|
|
|
|
|
+ //// storage.save(newContacts, as: OpenAPS.Settings.contactTrick)
|
|
|
|
|
+ // }
|
|
|
|
|
+
|
|
|
|
|
+ // Find new entries in newContacts that are not in contacts
|
|
|
|
|
+ let newEntries = newContacts.filter { newContact in
|
|
|
|
|
+ !contacts.contains(where: { $0.contactId == newContact.contactId })
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- // TODO: save this in CD
|
|
|
|
|
-// storage.save(newContacts, as: OpenAPS.Settings.contactTrick)
|
|
|
|
|
|
|
+ // When we create new contacts we store the IDs, in that case we need to write into the settings storage
|
|
|
|
|
+ // Save the new entries into Core Data
|
|
|
|
|
+ for newEntry in newEntries {
|
|
|
|
|
+ Task {
|
|
|
|
|
+ await contactTrickStorage.storeContactTrickEntry(newEntry)
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
contacts = newContacts
|
|
contacts = newContacts
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|