PumpHistorySetup.swift 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import CoreData
  2. import Foundation
  3. extension Home.StateModel {
  4. func setupInsulinArray() {
  5. Task {
  6. let ids = await self.fetchInsulin()
  7. let insulinObjects: [PumpEventStored] = await CoreDataStack.shared.getNSManagedObject(with: ids, context: viewContext)
  8. await updateInsulinArray(with: insulinObjects)
  9. }
  10. }
  11. private func fetchInsulin() async -> [NSManagedObjectID] {
  12. let results = await CoreDataStack.shared.fetchEntitiesAsync(
  13. ofType: PumpEventStored.self,
  14. onContext: pumpHistoryFetchContext,
  15. predicate: NSPredicate.pumpHistoryLast24h,
  16. key: "timestamp",
  17. ascending: true,
  18. batchSize: 30
  19. )
  20. return await pumpHistoryFetchContext.perform {
  21. guard let pumpEvents = results as? [PumpEventStored] else {
  22. return []
  23. }
  24. return pumpEvents.map(\.objectID)
  25. }
  26. }
  27. @MainActor private func updateInsulinArray(with insulinObjects: [PumpEventStored]) {
  28. insulinFromPersistence = insulinObjects
  29. manualTempBasal = apsManager.isManualTempBasal
  30. tempBasals = insulinFromPersistence.filter({ $0.tempBasal != nil })
  31. suspensions = insulinFromPersistence.filter {
  32. $0.type == EventType.pumpSuspend.rawValue || $0.type == EventType.pumpResume.rawValue
  33. }
  34. let lastSuspension = suspensions.last
  35. pumpSuspended = tempBasals.last?.timestamp ?? Date() > lastSuspension?.timestamp ?? .distantPast && lastSuspension?
  36. .type == EventType.pumpSuspend.rawValue
  37. }
  38. // Setup Last Bolus to display the bolus progress bar
  39. // 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
  40. func setupLastBolus() {
  41. Task {
  42. guard let id = await self.fetchLastBolus() else { return }
  43. await updateLastBolus(with: id)
  44. }
  45. }
  46. func fetchLastBolus() async -> NSManagedObjectID? {
  47. let results = await CoreDataStack.shared.fetchEntitiesAsync(
  48. ofType: PumpEventStored.self,
  49. onContext: pumpHistoryFetchContext,
  50. predicate: NSPredicate.lastPumpBolus,
  51. key: "timestamp",
  52. ascending: false,
  53. fetchLimit: 1
  54. )
  55. return await pumpHistoryFetchContext.perform {
  56. guard let fetchedResults = results as? [PumpEventStored] else { return [].first }
  57. return fetchedResults.map(\.objectID).first
  58. }
  59. }
  60. @MainActor private func updateLastBolus(with ID: NSManagedObjectID) {
  61. do {
  62. lastPumpBolus = try viewContext.existingObject(with: ID) as? PumpEventStored
  63. } catch {
  64. debugPrint(
  65. "Home State: \(#function) \(DebuggingIdentifiers.failed) error while updating the insulin array: \(error.localizedDescription)"
  66. )
  67. }
  68. }
  69. }