DataManager.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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: ["iob", "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. iob: (determination["iob"] as? NSDecimalNumber)?.decimalValue ?? 0,
  54. tdd: tddValue,
  55. target: (determination["currentTarget"] as? NSDecimalNumber)?.decimalValue ?? 0,
  56. date: determination["deliverAt"] as? Date ?? nil
  57. )
  58. }
  59. }
  60. func fetchAndMapOverride() async throws -> OverrideData? {
  61. let results = try await CoreDataStack.shared.fetchEntitiesAsync(
  62. ofType: OverrideStored.self,
  63. onContext: context,
  64. predicate: NSPredicate.predicateForOneDayAgo,
  65. key: "date",
  66. ascending: false,
  67. fetchLimit: 1,
  68. propertiesToFetch: ["enabled", "name", "target", "date", "duration"]
  69. )
  70. return try await context.perform {
  71. guard let overrideResults = results as? [[String: Any]] else {
  72. throw CoreDataError.fetchError(function: #function, file: #file)
  73. }
  74. return overrideResults.first.map {
  75. OverrideData(
  76. isActive: $0["enabled"] as? Bool ?? false,
  77. overrideName: $0["name"] as? String ?? "Override",
  78. date: $0["date"] as? Date ?? Date(),
  79. duration: $0["duration"] as? Decimal ?? 0,
  80. target: $0["target"] as? Decimal ?? 0
  81. )
  82. }
  83. }
  84. }
  85. }