PumpHistorySetup.swift 3.0 KB

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