FreeAPSSettings.swift 10 KB

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