FreeAPSSettings.swift 9.9 KB

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