FreeAPSSettings.swift 11 KB

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