DataManager.swift 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 LiveActivityBridge {
  5. func fetchAndMapGlucose() async {
  6. let result = 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. await context.perform {
  15. self.glucoseFromPersistence = result
  16. .map { GlucoseData(glucose: Int($0.glucose), date: $0.date ?? Date(), direction: $0.directionEnum) }
  17. }
  18. }
  19. func fetchAndMapDetermination() async {
  20. let result = await CoreDataStack.shared.fetchEntitiesAsync(
  21. ofType: OrefDetermination.self,
  22. onContext: context,
  23. predicate: NSPredicate.predicateFor30MinAgoForDetermination,
  24. key: "deliverAt",
  25. ascending: false,
  26. fetchLimit: 1,
  27. propertiesToFetch: ["iob", "cob", "deliverAt"]
  28. )
  29. await context.perform {
  30. self.determination = result.first.map { DeterminationData(cob: Int($0.cob), iob: $0.iob?.decimalValue ?? 0) }
  31. }
  32. }
  33. func fetchAndMapOverride() async {
  34. let result = await CoreDataStack.shared.fetchEntitiesAsync(
  35. ofType: OverrideStored.self,
  36. onContext: context,
  37. predicate: NSPredicate.predicateForOneDayAgo,
  38. key: "date",
  39. ascending: false,
  40. fetchLimit: 1
  41. )
  42. await context.perform {
  43. self.isOverridesActive = result.first.map { OverrideData(isActive: $0.enabled) }
  44. }
  45. }
  46. }