FreeAPSSettings.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import Foundation
  2. struct FreeAPSSettings: JSON, Equatable {
  3. var units: GlucoseUnits = .mmolL
  4. var closedLoop: Bool = false
  5. var allowAnnouncements: Bool = false
  6. var useAutotune: Bool = false
  7. var isUploadEnabled: Bool = false
  8. var useLocalGlucoseSource: Bool = false
  9. var localGlucosePort: Int = 8080
  10. var debugOptions: Bool = false
  11. var insulinReqFraction: Decimal = 0.7
  12. var skipBolusScreenAfterCarbs: Bool = false
  13. var cgm: CGMType = .nightscout
  14. var uploadGlucose: Bool = false
  15. var useCalendar: Bool = false
  16. var useAppleHealth: Bool = false
  17. var glucoseBadge: Bool = false
  18. var glucoseNotificationsAlways: Bool = false
  19. var useAlarmSound: Bool = false
  20. var addSourceInfoToGlucoseNotifications: Bool = false
  21. var lowGlucose: Decimal = 72
  22. var highGlucose: Decimal = 270
  23. var carbsRequiredThreshold: Decimal = 10
  24. }
  25. extension FreeAPSSettings: Decodable {
  26. // Needed to decode incomplete JSON
  27. init(from decoder: Decoder) throws {
  28. let container = try decoder.container(keyedBy: CodingKeys.self)
  29. var settings = FreeAPSSettings()
  30. if let units = try? container.decode(GlucoseUnits.self, forKey: .units) {
  31. settings.units = units
  32. }
  33. if let closedLoop = try? container.decode(Bool.self, forKey: .closedLoop) {
  34. settings.closedLoop = closedLoop
  35. }
  36. if let allowAnnouncements = try? container.decode(Bool.self, forKey: .allowAnnouncements) {
  37. settings.allowAnnouncements = allowAnnouncements
  38. }
  39. if let useAutotune = try? container.decode(Bool.self, forKey: .useAutotune) {
  40. settings.useAutotune = useAutotune
  41. }
  42. if let isUploadEnabled = try? container.decode(Bool.self, forKey: .isUploadEnabled) {
  43. settings.isUploadEnabled = isUploadEnabled
  44. }
  45. if let useLocalGlucoseSource = try? container.decode(Bool.self, forKey: .useLocalGlucoseSource) {
  46. settings.useLocalGlucoseSource = useLocalGlucoseSource
  47. }
  48. if let localGlucosePort = try? container.decode(Int.self, forKey: .localGlucosePort) {
  49. settings.localGlucosePort = localGlucosePort
  50. }
  51. if let debugOptions = try? container.decode(Bool.self, forKey: .debugOptions) {
  52. settings.debugOptions = debugOptions
  53. }
  54. if let insulinReqFraction = try? container.decode(Decimal.self, forKey: .insulinReqFraction) {
  55. settings.insulinReqFraction = insulinReqFraction
  56. }
  57. if let skipBolusScreenAfterCarbs = try? container.decode(Bool.self, forKey: .skipBolusScreenAfterCarbs) {
  58. settings.skipBolusScreenAfterCarbs = skipBolusScreenAfterCarbs
  59. }
  60. if let cgm = try? container.decode(CGMType.self, forKey: .cgm) {
  61. settings.cgm = cgm
  62. }
  63. if let uploadGlucose = try? container.decode(Bool.self, forKey: .uploadGlucose) {
  64. settings.uploadGlucose = uploadGlucose
  65. }
  66. if let useCalendar = try? container.decode(Bool.self, forKey: .useCalendar) {
  67. settings.useCalendar = useCalendar
  68. }
  69. if let useAppleHealth = try? container.decode(Bool.self, forKey: .useAppleHealth) {
  70. settings.useAppleHealth = useAppleHealth
  71. }
  72. if let glucoseBadge = try? container.decode(Bool.self, forKey: .glucoseBadge) {
  73. settings.glucoseBadge = glucoseBadge
  74. }
  75. if let glucoseNotificationsAlways = try? container.decode(Bool.self, forKey: .glucoseNotificationsAlways) {
  76. settings.glucoseNotificationsAlways = glucoseNotificationsAlways
  77. }
  78. if let useAlarmSound = try? container.decode(Bool.self, forKey: .useAlarmSound) {
  79. settings.useAlarmSound = useAlarmSound
  80. }
  81. if let addSourceInfoToGlucoseNotifications = try? container.decode(
  82. Bool.self,
  83. forKey: .addSourceInfoToGlucoseNotifications
  84. ) {
  85. settings.addSourceInfoToGlucoseNotifications = addSourceInfoToGlucoseNotifications
  86. }
  87. if let lowGlucose = try? container.decode(Decimal.self, forKey: .lowGlucose) {
  88. settings.lowGlucose = lowGlucose
  89. }
  90. if let highGlucose = try? container.decode(Decimal.self, forKey: .highGlucose) {
  91. settings.highGlucose = highGlucose
  92. }
  93. if let carbsRequiredThreshold = try? container.decode(Decimal.self, forKey: .carbsRequiredThreshold) {
  94. settings.carbsRequiredThreshold = carbsRequiredThreshold
  95. }
  96. self = settings
  97. }
  98. }