FreeAPSSettings.swift 9.6 KB

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