FreeAPSSettings.swift 8.5 KB

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