FreeAPSSettings.swift 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 insulinReqPercentage: Decimal = 70
  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 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. var animatedBackground: Bool = false
  25. var useFPUconversion: Bool = true
  26. var individualAdjustmentFactor: Decimal = 0.5
  27. var timeCap: Int = 8
  28. var minuteInterval: Int = 30
  29. var delay: Int = 60
  30. var useAppleHealth: Bool = false
  31. var smoothGlucose: Bool = false
  32. var displayOnWatch: AwConfig = .BGTarget
  33. var overrideHbA1cUnit: Bool = false
  34. var high: Decimal = 145
  35. var low: Decimal = 70
  36. var uploadStats: Bool = true
  37. var hours: Int = 6
  38. var xGridLines: Bool = true
  39. var yGridLines: Bool = true
  40. var oneDimensionalGraph: Bool = false
  41. var rulerMarks: Bool = false
  42. }
  43. extension FreeAPSSettings: Decodable {
  44. // Needed to decode incomplete JSON
  45. init(from decoder: Decoder) throws {
  46. let container = try decoder.container(keyedBy: CodingKeys.self)
  47. var settings = FreeAPSSettings()
  48. if let units = try? container.decode(GlucoseUnits.self, forKey: .units) {
  49. settings.units = units
  50. }
  51. if let closedLoop = try? container.decode(Bool.self, forKey: .closedLoop) {
  52. settings.closedLoop = closedLoop
  53. }
  54. if let allowAnnouncements = try? container.decode(Bool.self, forKey: .allowAnnouncements) {
  55. settings.allowAnnouncements = allowAnnouncements
  56. }
  57. if let useAutotune = try? container.decode(Bool.self, forKey: .useAutotune) {
  58. settings.useAutotune = useAutotune
  59. }
  60. if let isUploadEnabled = try? container.decode(Bool.self, forKey: .isUploadEnabled) {
  61. settings.isUploadEnabled = isUploadEnabled
  62. }
  63. if let useLocalGlucoseSource = try? container.decode(Bool.self, forKey: .useLocalGlucoseSource) {
  64. settings.useLocalGlucoseSource = useLocalGlucoseSource
  65. }
  66. if let localGlucosePort = try? container.decode(Int.self, forKey: .localGlucosePort) {
  67. settings.localGlucosePort = localGlucosePort
  68. }
  69. if let debugOptions = try? container.decode(Bool.self, forKey: .debugOptions) {
  70. settings.debugOptions = debugOptions
  71. }
  72. if let insulinReqPercentage = try? container.decode(Decimal.self, forKey: .insulinReqPercentage) {
  73. settings.insulinReqPercentage = insulinReqPercentage
  74. }
  75. if let skipBolusScreenAfterCarbs = try? container.decode(Bool.self, forKey: .skipBolusScreenAfterCarbs) {
  76. settings.skipBolusScreenAfterCarbs = skipBolusScreenAfterCarbs
  77. }
  78. if let displayHR = try? container.decode(Bool.self, forKey: .displayHR) {
  79. settings.displayHR = displayHR
  80. // compatibility if displayOnWatch is not available in json files
  81. settings.displayOnWatch = (displayHR == true) ? AwConfig.HR : AwConfig.BGTarget
  82. }
  83. if let displayOnWatch = try? container.decode(AwConfig.self, forKey: .displayOnWatch) {
  84. settings.displayOnWatch = displayOnWatch
  85. }
  86. if let cgm = try? container.decode(CGMType.self, forKey: .cgm) {
  87. settings.cgm = cgm
  88. }
  89. if let uploadGlucose = try? container.decode(Bool.self, forKey: .uploadGlucose) {
  90. settings.uploadGlucose = uploadGlucose
  91. }
  92. if let useCalendar = try? container.decode(Bool.self, forKey: .useCalendar) {
  93. settings.useCalendar = useCalendar
  94. }
  95. if let useAppleHealth = try? container.decode(Bool.self, forKey: .useAppleHealth) {
  96. settings.useAppleHealth = useAppleHealth
  97. }
  98. if let glucoseBadge = try? container.decode(Bool.self, forKey: .glucoseBadge) {
  99. settings.glucoseBadge = glucoseBadge
  100. }
  101. if let useFPUconversion = try? container.decode(Bool.self, forKey: .useFPUconversion) {
  102. settings.useFPUconversion = useFPUconversion
  103. }
  104. if let individualAdjustmentFactor = try? container.decode(Decimal.self, forKey: .individualAdjustmentFactor) {
  105. settings.individualAdjustmentFactor = individualAdjustmentFactor
  106. }
  107. if let timeCap = try? container.decode(Int.self, forKey: .timeCap) {
  108. settings.timeCap = timeCap
  109. }
  110. if let minuteInterval = try? container.decode(Int.self, forKey: .minuteInterval) {
  111. settings.minuteInterval = minuteInterval
  112. }
  113. if let delay = try? container.decode(Int.self, forKey: .delay) {
  114. settings.delay = delay
  115. }
  116. if let glucoseNotificationsAlways = try? container.decode(Bool.self, forKey: .glucoseNotificationsAlways) {
  117. settings.glucoseNotificationsAlways = glucoseNotificationsAlways
  118. }
  119. if let useAlarmSound = try? container.decode(Bool.self, forKey: .useAlarmSound) {
  120. settings.useAlarmSound = useAlarmSound
  121. }
  122. if let addSourceInfoToGlucoseNotifications = try? container.decode(
  123. Bool.self,
  124. forKey: .addSourceInfoToGlucoseNotifications
  125. ) {
  126. settings.addSourceInfoToGlucoseNotifications = addSourceInfoToGlucoseNotifications
  127. }
  128. if let lowGlucose = try? container.decode(Decimal.self, forKey: .lowGlucose) {
  129. settings.lowGlucose = lowGlucose
  130. }
  131. if let highGlucose = try? container.decode(Decimal.self, forKey: .highGlucose) {
  132. settings.highGlucose = highGlucose
  133. }
  134. if let carbsRequiredThreshold = try? container.decode(Decimal.self, forKey: .carbsRequiredThreshold) {
  135. settings.carbsRequiredThreshold = carbsRequiredThreshold
  136. }
  137. if let animatedBackground = try? container.decode(Bool.self, forKey: .animatedBackground) {
  138. settings.animatedBackground = animatedBackground
  139. }
  140. if let smoothGlucose = try? container.decode(Bool.self, forKey: .smoothGlucose) {
  141. settings.smoothGlucose = smoothGlucose
  142. }
  143. if let low = try? container.decode(Decimal.self, forKey: .low) {
  144. settings.low = low
  145. }
  146. if let high = try? container.decode(Decimal.self, forKey: .high) {
  147. settings.high = high
  148. }
  149. if let uploadStats = try? container.decode(Bool.self, forKey: .uploadStats) {
  150. settings.uploadStats = uploadStats
  151. }
  152. if let hours = try? container.decode(Int.self, forKey: .hours) {
  153. settings.hours = hours
  154. }
  155. if let xGridLines = try? container.decode(Bool.self, forKey: .xGridLines) {
  156. settings.xGridLines = xGridLines
  157. }
  158. if let yGridLines = try? container.decode(Bool.self, forKey: .yGridLines) {
  159. settings.yGridLines = yGridLines
  160. }
  161. if let oneDimensionalGraph = try? container.decode(Bool.self, forKey: .oneDimensionalGraph) {
  162. settings.oneDimensionalGraph = oneDimensionalGraph
  163. }
  164. if let rulerMarks = try? container.decode(Bool.self, forKey: .rulerMarks) {
  165. settings.rulerMarks = rulerMarks
  166. }
  167. self = settings
  168. }
  169. }