TempTargetPreset.swift 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // LoopFollow
  2. // TempTargetPreset.swift
  3. import Foundation
  4. import HealthKit
  5. struct TempTargetPreset: Identifiable, Codable {
  6. var id: UUID
  7. var name: String
  8. var target: HKQuantity
  9. var duration: HKQuantity
  10. enum CodingKeys: String, CodingKey {
  11. case id, name, targetValue, durationValue
  12. }
  13. init(id: UUID = UUID(), name: String, target: HKQuantity, duration: HKQuantity) {
  14. self.id = id
  15. self.name = name
  16. self.target = target
  17. self.duration = duration
  18. }
  19. init(from decoder: Decoder) throws {
  20. let container = try decoder.container(keyedBy: CodingKeys.self)
  21. id = try container.decode(UUID.self, forKey: .id)
  22. name = try container.decode(String.self, forKey: .name)
  23. let targetValue = try container.decode(Double.self, forKey: .targetValue)
  24. target = HKQuantity(unit: .milligramsPerDeciliter, doubleValue: targetValue)
  25. let durationValue = try container.decode(Double.self, forKey: .durationValue)
  26. duration = HKQuantity(unit: .minute(), doubleValue: durationValue)
  27. }
  28. func encode(to encoder: Encoder) throws {
  29. var container = encoder.container(keyedBy: CodingKeys.self)
  30. try container.encode(id, forKey: .id)
  31. try container.encode(name, forKey: .name)
  32. try container.encode(target.doubleValue(for: .milligramsPerDeciliter), forKey: .targetValue)
  33. try container.encode(duration.doubleValue(for: .minute()), forKey: .durationValue)
  34. }
  35. }