FreeAPSSettings.swift 8.7 KB

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