GlucoseLiveActivityAttributes.swift 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // LoopFollow
  2. // GlucoseLiveActivityAttributes.swift
  3. // swiftformat:disable indent
  4. #if !targetEnvironment(macCatalyst)
  5. import ActivityKit
  6. import Foundation
  7. struct GlucoseLiveActivityAttributes: ActivityAttributes {
  8. struct ContentState: Codable, Hashable {
  9. let snapshot: GlucoseSnapshot
  10. let seq: Int
  11. let reason: String
  12. let producedAt: Date
  13. init(snapshot: GlucoseSnapshot, seq: Int, reason: String, producedAt: Date) {
  14. self.snapshot = snapshot
  15. self.seq = seq
  16. self.reason = reason
  17. self.producedAt = producedAt
  18. }
  19. init(from decoder: Decoder) throws {
  20. let container = try decoder.container(keyedBy: CodingKeys.self)
  21. snapshot = try container.decode(GlucoseSnapshot.self, forKey: .snapshot)
  22. seq = try container.decode(Int.self, forKey: .seq)
  23. reason = try container.decode(String.self, forKey: .reason)
  24. let producedAtInterval = try container.decode(Double.self, forKey: .producedAt)
  25. producedAt = Date(timeIntervalSince1970: producedAtInterval)
  26. }
  27. func encode(to encoder: Encoder) throws {
  28. var container = encoder.container(keyedBy: CodingKeys.self)
  29. try container.encode(snapshot, forKey: .snapshot)
  30. try container.encode(seq, forKey: .seq)
  31. try container.encode(reason, forKey: .reason)
  32. try container.encode(producedAt.timeIntervalSince1970, forKey: .producedAt)
  33. }
  34. private enum CodingKeys: String, CodingKey {
  35. case snapshot, seq, reason, producedAt
  36. }
  37. }
  38. /// Reserved for future metadata. Keep minimal for stability.
  39. let title: String
  40. }
  41. #endif