FreeAPSSettings.swift 9.3 KB

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