PumpHistorySetup.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import CoreData
  2. import Foundation
  3. extension Home.StateModel {
  4. func setupInsulinArray() {
  5. Task {
  6. do {
  7. let ids = try await self.fetchInsulin()
  8. let insulinObjects: [PumpEventStored] = try await CoreDataStack.shared
  9. .getNSManagedObject(with: ids, context: viewContext)
  10. await updateInsulinArray(with: insulinObjects)
  11. } catch {
  12. debug(
  13. .default,
  14. "\(DebuggingIdentifiers.failed) Error setting up insulin array: \(error)"
  15. )
  16. }
  17. }
  18. }
  19. private func fetchInsulin() async throws -> [NSManagedObjectID] {
  20. let results = try await CoreDataStack.shared.fetchEntitiesAsync(
  21. ofType: PumpEventStored.self,
  22. onContext: pumpHistoryFetchContext,
  23. predicate: NSPredicate.pumpHistoryLast24h,
  24. key: "timestamp",
  25. ascending: true,
  26. batchSize: 30
  27. )
  28. return try await pumpHistoryFetchContext.perform {
  29. guard let pumpEvents = results as? [PumpEventStored] else {
  30. throw CoreDataError.fetchError(function: #function, file: #file)
  31. }
  32. return pumpEvents.map(\.objectID)
  33. }
  34. }
  35. @MainActor private func updateInsulinArray(with insulinObjects: [PumpEventStored]) {
  36. insulinFromPersistence = insulinObjects
  37. manualTempBasal = apsManager.isManualTempBasal
  38. tempBasals = insulinFromPersistence.filter { $0.tempBasal != nil }
  39. /// The suspensions variable is actually is a list of all pump suspend and resume events
  40. suspensions = insulinFromPersistence.filter {
  41. $0.type == EventType.pumpSuspend.rawValue || $0.type == EventType.pumpResume.rawValue
  42. }
  43. /// Determining if the pump is currently suspended is now handled in DeviceDataManager
  44. }
  45. // Setup Last Bolus to display the bolus progress bar
  46. // 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
  47. func setupLastBolus() {
  48. Task {
  49. do {
  50. guard let id = try await self.fetchLastBolus() else { return }
  51. await updateLastBolus(with: id)
  52. } catch {
  53. debug(
  54. .default,
  55. "\(DebuggingIdentifiers.failed) Error setting up last bolus: \(error)"
  56. )
  57. }
  58. }
  59. }
  60. func fetchLastBolus() async throws -> NSManagedObjectID? {
  61. let results = try await CoreDataStack.shared.fetchEntitiesAsync(
  62. ofType: PumpEventStored.self,
  63. onContext: pumpHistoryFetchContext,
  64. predicate: NSPredicate.lastPumpBolus,
  65. key: "timestamp",
  66. ascending: false,
  67. fetchLimit: 1
  68. )
  69. return try await pumpHistoryFetchContext.perform {
  70. guard let fetchedResults = results as? [PumpEventStored] else {
  71. throw CoreDataError.fetchError(function: #function, file: #file)
  72. }
  73. return fetchedResults.map(\.objectID).first
  74. }
  75. }
  76. @MainActor private func updateLastBolus(with ID: NSManagedObjectID) {
  77. do {
  78. lastPumpBolus = try viewContext.existingObject(with: ID) as? PumpEventStored
  79. } catch {
  80. debugPrint(
  81. "Home State: \(#function) \(DebuggingIdentifiers.failed) error while updating the insulin array: \(error)"
  82. )
  83. }
  84. }
  85. }