FreeAPSSettings.swift 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 isDownloadEnabled: Bool = false
  9. var useLocalGlucoseSource: Bool = false
  10. var localGlucosePort: Int = 8080
  11. var debugOptions: Bool = false
  12. var insulinReqPercentage: Decimal = 70
  13. var skipBolusScreenAfterCarbs: Bool = false
  14. var displayHR: Bool = false
  15. var cgm: CGMType = .none
  16. var cgmPluginIdentifier: String = ""
  17. var uploadGlucose: Bool = true
  18. var useCalendar: 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 = 180
  37. var low: Decimal = 70
  38. var hours: Int = 6
  39. var xGridLines: Bool = true
  40. var yGridLines: Bool = true
  41. var oneDimensionalGraph: Bool = false
  42. var rulerMarks: Bool = true
  43. var maxCarbs: Decimal = 250
  44. var maxFat: Decimal = 250
  45. var maxProtein: Decimal = 250
  46. var displayFatAndProteinOnWatch: Bool = false
  47. var onlyAutotuneBasals: Bool = false
  48. var useLiveActivity: Bool = false
  49. var lockScreenView: LockScreenView = .simple
  50. }
  51. extension FreeAPSSettings: Decodable {
  52. // Needed to decode incomplete JSON
  53. init(from decoder: Decoder) throws {
  54. let container = try decoder.container(keyedBy: CodingKeys.self)
  55. var settings = FreeAPSSettings()
  56. if let units = try? container.decode(GlucoseUnits.self, forKey: .units) {
  57. settings.units = units
  58. }
  59. if let closedLoop = try? container.decode(Bool.self, forKey: .closedLoop) {
  60. settings.closedLoop = closedLoop
  61. }
  62. if let allowAnnouncements = try? container.decode(Bool.self, forKey: .allowAnnouncements) {
  63. settings.allowAnnouncements = allowAnnouncements
  64. }
  65. if let useAutotune = try? container.decode(Bool.self, forKey: .useAutotune) {
  66. settings.useAutotune = useAutotune
  67. }
  68. if let isUploadEnabled = try? container.decode(Bool.self, forKey: .isUploadEnabled) {
  69. settings.isUploadEnabled = isUploadEnabled
  70. }
  71. if let isDownloadEnabled = try? container.decode(Bool.self, forKey: .isDownloadEnabled) {
  72. settings.isDownloadEnabled = isDownloadEnabled
  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 cgmPluginIdentifier = try? container.decode(String.self, forKey: .cgmPluginIdentifier) {
  101. settings.cgmPluginIdentifier = cgmPluginIdentifier
  102. }
  103. if let uploadGlucose = try? container.decode(Bool.self, forKey: .uploadGlucose) {
  104. settings.uploadGlucose = uploadGlucose
  105. }
  106. if let useCalendar = try? container.decode(Bool.self, forKey: .useCalendar) {
  107. settings.useCalendar = useCalendar
  108. }
  109. if let useAppleHealth = try? container.decode(Bool.self, forKey: .useAppleHealth) {
  110. settings.useAppleHealth = useAppleHealth
  111. }
  112. if let glucoseBadge = try? container.decode(Bool.self, forKey: .glucoseBadge) {
  113. settings.glucoseBadge = glucoseBadge
  114. }
  115. if let useFPUconversion = try? container.decode(Bool.self, forKey: .useFPUconversion) {
  116. settings.useFPUconversion = useFPUconversion
  117. }
  118. if let individualAdjustmentFactor = try? container.decode(Decimal.self, forKey: .individualAdjustmentFactor) {
  119. settings.individualAdjustmentFactor = individualAdjustmentFactor
  120. }
  121. if let timeCap = try? container.decode(Int.self, forKey: .timeCap) {
  122. settings.timeCap = timeCap
  123. }
  124. if let minuteInterval = try? container.decode(Int.self, forKey: .minuteInterval) {
  125. settings.minuteInterval = minuteInterval
  126. }
  127. if let delay = try? container.decode(Int.self, forKey: .delay) {
  128. settings.delay = delay
  129. }
  130. if let glucoseNotificationsAlways = try? container.decode(Bool.self, forKey: .glucoseNotificationsAlways) {
  131. settings.glucoseNotificationsAlways = glucoseNotificationsAlways
  132. }
  133. if let useAlarmSound = try? container.decode(Bool.self, forKey: .useAlarmSound) {
  134. settings.useAlarmSound = useAlarmSound
  135. }
  136. if let addSourceInfoToGlucoseNotifications = try? container.decode(
  137. Bool.self,
  138. forKey: .addSourceInfoToGlucoseNotifications
  139. ) {
  140. settings.addSourceInfoToGlucoseNotifications = addSourceInfoToGlucoseNotifications
  141. }
  142. if let lowGlucose = try? container.decode(Decimal.self, forKey: .lowGlucose) {
  143. settings.lowGlucose = lowGlucose
  144. }
  145. if let highGlucose = try? container.decode(Decimal.self, forKey: .highGlucose) {
  146. settings.highGlucose = highGlucose
  147. }
  148. if let carbsRequiredThreshold = try? container.decode(Decimal.self, forKey: .carbsRequiredThreshold) {
  149. settings.carbsRequiredThreshold = carbsRequiredThreshold
  150. }
  151. if let animatedBackground = try? container.decode(Bool.self, forKey: .animatedBackground) {
  152. settings.animatedBackground = animatedBackground
  153. }
  154. if let smoothGlucose = try? container.decode(Bool.self, forKey: .smoothGlucose) {
  155. settings.smoothGlucose = smoothGlucose
  156. }
  157. if let low = try? container.decode(Decimal.self, forKey: .low) {
  158. settings.low = low
  159. }
  160. if let high = try? container.decode(Decimal.self, forKey: .high) {
  161. settings.high = high
  162. }
  163. if let hours = try? container.decode(Int.self, forKey: .hours) {
  164. settings.hours = hours
  165. }
  166. if let xGridLines = try? container.decode(Bool.self, forKey: .xGridLines) {
  167. settings.xGridLines = xGridLines
  168. }
  169. if let yGridLines = try? container.decode(Bool.self, forKey: .yGridLines) {
  170. settings.yGridLines = yGridLines
  171. }
  172. if let oneDimensionalGraph = try? container.decode(Bool.self, forKey: .oneDimensionalGraph) {
  173. settings.oneDimensionalGraph = oneDimensionalGraph
  174. }
  175. if let rulerMarks = try? container.decode(Bool.self, forKey: .rulerMarks) {
  176. settings.rulerMarks = rulerMarks
  177. }
  178. if let overrideHbA1cUnit = try? container.decode(Bool.self, forKey: .overrideHbA1cUnit) {
  179. settings.overrideHbA1cUnit = overrideHbA1cUnit
  180. }
  181. if let maxCarbs = try? container.decode(Decimal.self, forKey: .maxCarbs) {
  182. settings.maxCarbs = maxCarbs
  183. }
  184. if let maxFat = try? container.decode(Decimal.self, forKey: .maxFat) {
  185. settings.maxFat = maxFat
  186. }
  187. if let maxProtein = try? container.decode(Decimal.self, forKey: .maxProtein) {
  188. settings.maxProtein = maxProtein
  189. }
  190. if let displayFatAndProteinOnWatch = try? container.decode(Bool.self, forKey: .displayFatAndProteinOnWatch) {
  191. settings.displayFatAndProteinOnWatch = displayFatAndProteinOnWatch
  192. }
  193. if let onlyAutotuneBasals = try? container.decode(Bool.self, forKey: .onlyAutotuneBasals) {
  194. settings.onlyAutotuneBasals = onlyAutotuneBasals
  195. }
  196. if let useLiveActivity = try? container.decode(Bool.self, forKey: .useLiveActivity) {
  197. settings.useLiveActivity = useLiveActivity
  198. }
  199. if let lockScreenView = try? container.decode(LockScreenView.self, forKey: .lockScreenView) {
  200. settings.lockScreenView = lockScreenView
  201. }
  202. self = settings
  203. }
  204. }