DataManager.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import Foundation
  2. // Fetch Data for Glucose and Determination from Core Data and map them to the Structs in order to pass them thread safe to the glucoseDidUpdate/ pushUpdate function
  3. @available(iOS 16.2, *)
  4. extension LiveActivityManager {
  5. func fetchAndMapGlucose() async throws -> [GlucoseData] {
  6. let results = try await CoreDataStack.shared.fetchEntitiesAsync(
  7. ofType: GlucoseStored.self,
  8. onContext: context,
  9. predicate: NSPredicate.predicateForSixHoursAgo,
  10. key: "date",
  11. ascending: false,
  12. fetchLimit: 72
  13. )
  14. return try await context.perform {
  15. guard let glucoseResults = results as? [GlucoseStored] else {
  16. throw CoreDataError.fetchError(function: #function, file: #file)
  17. }
  18. return glucoseResults.map {
  19. GlucoseData(glucose: Int($0.glucose), date: $0.date ?? Date(), direction: $0.directionEnum)
  20. }
  21. }
  22. }
  23. // TODO: extract logic or at least rename function appropiately
  24. func fetchAndMapDetermination() async throws -> DeterminationData? {
  25. let results = try await CoreDataStack.shared.fetchEntitiesAsync(
  26. ofType: OrefDetermination.self,
  27. onContext: context,
  28. predicate: NSPredicate.predicateFor30MinAgoForDetermination,
  29. key: "deliverAt",
  30. ascending: false,
  31. fetchLimit: 1,
  32. propertiesToFetch: ["cob", "currentTarget", "deliverAt"]
  33. )
  34. let tddResults = try await CoreDataStack.shared.fetchEntitiesAsync(
  35. ofType: TDDStored.self,
  36. onContext: context,
  37. predicate: NSPredicate.predicateFor30MinAgo,
  38. key: "date",
  39. ascending: false,
  40. fetchLimit: 1,
  41. propertiesToFetch: ["total"]
  42. )
  43. return try await context.perform {
  44. guard let determinationResults = results as? [[String: Any]], let tddResults = tddResults as? [[String: Any]] else {
  45. throw CoreDataError.fetchError(function: #function, file: #file)
  46. }
  47. guard let determination = determinationResults.first else {
  48. return nil
  49. }
  50. let tddValue = (tddResults.first?["total"] as? NSDecimalNumber)?.decimalValue ?? 0
  51. return DeterminationData(
  52. cob: (determination["cob"] as? Int) ?? 0,
  53. tdd: tddValue,
  54. target: (determination["currentTarget"] as? NSDecimalNumber)?.decimalValue ?? 0,
  55. date: determination["deliverAt"] as? Date ?? nil
  56. )
  57. }
  58. }
  59. func fetchAndMapTempTarget() async throws -> TempTargetData? {
  60. let results = try await CoreDataStack.shared.fetchEntitiesAsync(
  61. ofType: TempTargetStored.self,
  62. onContext: context,
  63. predicate: NSPredicate.predicateForOneDayAgo,
  64. key: "date",
  65. ascending: false,
  66. fetchLimit: 1,
  67. propertiesToFetch: ["enabled", "name", "target", "date", "duration"]
  68. )
  69. return try await context.perform {
  70. guard let tempTargetResults = results as? [[String: Any]] else {
  71. throw CoreDataError.fetchError(function: #function, file: #file)
  72. }
  73. return tempTargetResults.first.map {
  74. TempTargetData(
  75. isActive: $0["enabled"] as? Bool ?? false,
  76. tempTargetName: $0["name"] as? String ?? "Temp Target",
  77. date: $0["date"] as? Date ?? Date(),
  78. duration: $0["duration"] as? Decimal ?? 0,
  79. target: $0["target"] as? Decimal ?? 0
  80. )
  81. }
  82. }
  83. }
  84. func fetchAndMapOverride() async throws -> OverrideData? {
  85. let results = try await CoreDataStack.shared.fetchEntitiesAsync(
  86. ofType: OverrideStored.self,
  87. onContext: context,
  88. predicate: NSPredicate.predicateForOneDayAgo,
  89. key: "date",
  90. ascending: false,
  91. fetchLimit: 1,
  92. propertiesToFetch: ["enabled", "name", "target", "date", "duration"]
  93. )
  94. return try await context.perform {
  95. guard let overrideResults = results as? [[String: Any]] else {
  96. throw CoreDataError.fetchError(function: #function, file: #file)
  97. }
  98. return overrideResults.first.map {
  99. OverrideData(
  100. isActive: $0["enabled"] as? Bool ?? false,
  101. overrideName: $0["name"] as? String ?? "Override",
  102. date: $0["date"] as? Date ?? Date(),
  103. duration: $0["duration"] as? Decimal ?? 0,
  104. target: $0["target"] as? Decimal ?? 0
  105. )
  106. }
  107. }
  108. }
  109. }