FreeAPSSettings.swift 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 displayHR: Bool = false
  14. var cgm: CGMType = .nightscout
  15. var uploadGlucose: Bool = false
  16. var useCalendar: Bool = false
  17. var useAppleHealth: Bool = false
  18. var glucoseBadge: Bool = false
  19. var glucoseNotificationsAlways: Bool = false
  20. var useAlarmSound: Bool = false
  21. var addSourceInfoToGlucoseNotifications: Bool = false
  22. var lowGlucose: Decimal = 72
  23. var highGlucose: Decimal = 270
  24. var carbsRequiredThreshold: Decimal = 10
  25. var animatedBackground: Bool = false
  26. var displayStatistics: Bool = false
  27. var useFPUconversion: Bool = false
  28. var individualAdjustmentFactor: Decimal = 0.5
  29. var timeCap: Int = 8
  30. var minuteInterval: Int = 30
  31. var delay: Int = 60
  32. var smoothGlucose: Bool = false
  33. }
  34. extension FreeAPSSettings: Decodable {
  35. // Needed to decode incomplete JSON
  36. init(from decoder: Decoder) throws {
  37. let container = try decoder.container(keyedBy: CodingKeys.self)
  38. var settings = FreeAPSSettings()
  39. if let units = try? container.decode(GlucoseUnits.self, forKey: .units) {
  40. settings.units = units
  41. }
  42. if let closedLoop = try? container.decode(Bool.self, forKey: .closedLoop) {
  43. settings.closedLoop = closedLoop
  44. }
  45. if let allowAnnouncements = try? container.decode(Bool.self, forKey: .allowAnnouncements) {
  46. settings.allowAnnouncements = allowAnnouncements
  47. }
  48. if let useAutotune = try? container.decode(Bool.self, forKey: .useAutotune) {
  49. settings.useAutotune = useAutotune
  50. }
  51. if let isUploadEnabled = try? container.decode(Bool.self, forKey: .isUploadEnabled) {
  52. settings.isUploadEnabled = isUploadEnabled
  53. }
  54. if let useLocalGlucoseSource = try? container.decode(Bool.self, forKey: .useLocalGlucoseSource) {
  55. settings.useLocalGlucoseSource = useLocalGlucoseSource
  56. }
  57. if let localGlucosePort = try? container.decode(Int.self, forKey: .localGlucosePort) {
  58. settings.localGlucosePort = localGlucosePort
  59. }
  60. if let debugOptions = try? container.decode(Bool.self, forKey: .debugOptions) {
  61. settings.debugOptions = debugOptions
  62. }
  63. if let insulinReqFraction = try? container.decode(Decimal.self, forKey: .insulinReqFraction) {
  64. settings.insulinReqFraction = insulinReqFraction
  65. }
  66. if let skipBolusScreenAfterCarbs = try? container.decode(Bool.self, forKey: .skipBolusScreenAfterCarbs) {
  67. settings.skipBolusScreenAfterCarbs = skipBolusScreenAfterCarbs
  68. }
  69. if let displayHR = try? container.decode(Bool.self, forKey: .displayHR) {
  70. settings.displayHR = displayHR
  71. }
  72. if let cgm = try? container.decode(CGMType.self, forKey: .cgm) {
  73. settings.cgm = cgm
  74. }
  75. if let uploadGlucose = try? container.decode(Bool.self, forKey: .uploadGlucose) {
  76. settings.uploadGlucose = uploadGlucose
  77. }
  78. if let useCalendar = try? container.decode(Bool.self, forKey: .useCalendar) {
  79. settings.useCalendar = useCalendar
  80. }
  81. if let useAppleHealth = try? container.decode(Bool.self, forKey: .useAppleHealth) {
  82. settings.useAppleHealth = useAppleHealth
  83. }
  84. if let glucoseBadge = try? container.decode(Bool.self, forKey: .glucoseBadge) {
  85. settings.glucoseBadge = glucoseBadge
  86. }
  87. if let useFPUconversion = try? container.decode(Bool.self, forKey: .useFPUconversion) {
  88. settings.useFPUconversion = useFPUconversion
  89. }
  90. if let individualAdjustmentFactor = try? container.decode(Decimal.self, forKey: .individualAdjustmentFactor) {
  91. settings.individualAdjustmentFactor = individualAdjustmentFactor
  92. }
  93. if let timeCap = try? container.decode(Int.self, forKey: .timeCap) {
  94. settings.timeCap = timeCap
  95. }
  96. if let minuteInterval = try? container.decode(Int.self, forKey: .minuteInterval) {
  97. settings.minuteInterval = minuteInterval
  98. }
  99. if let delay = try? container.decode(Int.self, forKey: .delay) {
  100. settings.delay = delay
  101. }
  102. if let glucoseNotificationsAlways = try? container.decode(Bool.self, forKey: .glucoseNotificationsAlways) {
  103. settings.glucoseNotificationsAlways = glucoseNotificationsAlways
  104. }
  105. if let useAlarmSound = try? container.decode(Bool.self, forKey: .useAlarmSound) {
  106. settings.useAlarmSound = useAlarmSound
  107. }
  108. if let addSourceInfoToGlucoseNotifications = try? container.decode(
  109. Bool.self,
  110. forKey: .addSourceInfoToGlucoseNotifications
  111. ) {
  112. settings.addSourceInfoToGlucoseNotifications = addSourceInfoToGlucoseNotifications
  113. }
  114. if let lowGlucose = try? container.decode(Decimal.self, forKey: .lowGlucose) {
  115. settings.lowGlucose = lowGlucose
  116. }
  117. if let highGlucose = try? container.decode(Decimal.self, forKey: .highGlucose) {
  118. settings.highGlucose = highGlucose
  119. }
  120. if let carbsRequiredThreshold = try? container.decode(Decimal.self, forKey: .carbsRequiredThreshold) {
  121. settings.carbsRequiredThreshold = carbsRequiredThreshold
  122. }
  123. if let animatedBackground = try? container.decode(Bool.self, forKey: .animatedBackground) {
  124. settings.animatedBackground = animatedBackground
  125. }
  126. if let displayStatistics = try? container.decode(Bool.self, forKey: .displayStatistics) {
  127. settings.displayStatistics = displayStatistics
  128. }
  129. if let smoothGlucose = try? container.decode(Bool.self, forKey: .smoothGlucose) {
  130. settings.smoothGlucose = smoothGlucose
  131. }
  132. self = settings
  133. }
  134. }