| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- import CoreData
- import Foundation
- extension Home.StateModel {
- func setupInsulinArray() {
- Task {
- do {
- let ids = try await self.fetchInsulin()
- let insulinObjects: [PumpEventStored] = try await CoreDataStack.shared
- .getNSManagedObject(with: ids, context: viewContext)
- await updateInsulinArray(with: insulinObjects)
- } catch {
- debug(
- .default,
- "\(DebuggingIdentifiers.failed) Error setting up insulin array: \(error)"
- )
- }
- }
- }
- private func fetchInsulin() async throws -> [NSManagedObjectID] {
- let results = try await CoreDataStack.shared.fetchEntitiesAsync(
- ofType: PumpEventStored.self,
- onContext: pumpHistoryFetchContext,
- predicate: NSPredicate.pumpHistoryLast24h,
- key: "timestamp",
- ascending: true,
- batchSize: 30
- )
- return try await pumpHistoryFetchContext.perform {
- guard let pumpEvents = results as? [PumpEventStored] else {
- throw CoreDataError.fetchError(function: #function, file: #file)
- }
- return pumpEvents.map(\.objectID)
- }
- }
- @MainActor private func updateInsulinArray(with insulinObjects: [PumpEventStored]) {
- insulinFromPersistence = insulinObjects
- manualTempBasal = apsManager.isManualTempBasal
- tempBasals = insulinFromPersistence.filter { $0.tempBasal != nil }
- /// The suspensions variable is actually is a list of all pump suspend and resume events
- suspensions = insulinFromPersistence.filter {
- $0.type == EventType.pumpSuspend.rawValue || $0.type == EventType.pumpResume.rawValue
- }
- /// Determining if the pump is currently suspended is now handled in DeviceDataManager
- }
- // Setup Last Bolus to display the bolus progress bar
- // The predicate filters out all external boluses to prevent the progress bar from displaying the amount of an external bolus when an external bolus is added after a pump bolus
- func setupLastBolus() {
- Task {
- do {
- guard let id = try await self.fetchLastBolus() else { return }
- await updateLastBolus(with: id)
- } catch {
- debug(
- .default,
- "\(DebuggingIdentifiers.failed) Error setting up last bolus: \(error)"
- )
- }
- }
- }
- func fetchLastBolus() async throws -> NSManagedObjectID? {
- let results = try await CoreDataStack.shared.fetchEntitiesAsync(
- ofType: PumpEventStored.self,
- onContext: pumpHistoryFetchContext,
- predicate: NSPredicate.lastPumpBolus,
- key: "timestamp",
- ascending: false,
- fetchLimit: 1
- )
- return try await pumpHistoryFetchContext.perform {
- guard let fetchedResults = results as? [PumpEventStored] else {
- throw CoreDataError.fetchError(function: #function, file: #file)
- }
- return fetchedResults.map(\.objectID).first
- }
- }
- @MainActor private func updateLastBolus(with ID: NSManagedObjectID) {
- do {
- lastPumpBolus = try viewContext.existingObject(with: ID) as? PumpEventStored
- } catch {
- debugPrint(
- "Home State: \(#function) \(DebuggingIdentifiers.failed) error while updating the insulin array: \(error)"
- )
- }
- }
- }
|