FreeAPSSettings.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. import Foundation
  2. enum BolusShortcutLimit: String, JSON, CaseIterable, Identifiable {
  3. var id: String { rawValue }
  4. case notAllowed
  5. case limitBolusMax
  6. var displayName: String {
  7. switch self {
  8. case .notAllowed:
  9. return String(localized: "Not allowed", table: "ShortcutsDetail")
  10. case .limitBolusMax:
  11. return String(localized: "Max bolus", table: "ShortcutsDetail")
  12. }
  13. }
  14. }
  15. struct FreeAPSSettings: JSON, Equatable {
  16. var units: GlucoseUnits = .mgdL
  17. var closedLoop: Bool = false
  18. var allowAnnouncements: Bool = false
  19. var useAutotune: Bool = false
  20. var isUploadEnabled: Bool = false
  21. var isDownloadEnabled: Bool = false
  22. var useLocalGlucoseSource: Bool = false
  23. var localGlucosePort: Int = 8080
  24. var debugOptions: Bool = false
  25. var displayHR: Bool = false
  26. var cgm: CGMType = .none
  27. var cgmPluginIdentifier: String = ""
  28. var uploadGlucose: Bool = true
  29. var useCalendar: Bool = false
  30. var displayCalendarIOBandCOB: Bool = false
  31. var displayCalendarEmojis: Bool = false
  32. var glucoseBadge: Bool = false
  33. var glucoseNotificationsAlways: Bool = false
  34. var useAlarmSound: Bool = false
  35. var addSourceInfoToGlucoseNotifications: Bool = false
  36. var lowGlucose: Decimal = 72
  37. var highGlucose: Decimal = 270
  38. var carbsRequiredThreshold: Decimal = 10
  39. var showCarbsRequiredBadge: Bool = true
  40. var useFPUconversion: Bool = true
  41. var totalInsulinDisplayType: TotalInsulinDisplayType = .totalDailyDose
  42. var individualAdjustmentFactor: Decimal = 0.5
  43. var timeCap: Int = 8
  44. var minuteInterval: Int = 30
  45. var delay: Int = 60
  46. var useAppleHealth: Bool = false
  47. var smoothGlucose: Bool = false
  48. var displayOnWatch: AwConfig = .BGTarget
  49. var overrideHbA1cUnit: Bool = false
  50. var high: Decimal = 180
  51. var low: Decimal = 70
  52. var hours: Int = 6
  53. var xGridLines: Bool = true
  54. var yGridLines: Bool = true
  55. var oneDimensionalGraph: Bool = false
  56. var rulerMarks: Bool = true
  57. var forecastDisplayType: ForecastDisplayType = .cone
  58. var maxCarbs: Decimal = 250
  59. var maxFat: Decimal = 250
  60. var maxProtein: Decimal = 250
  61. var displayFatAndProteinOnWatch: Bool = false
  62. var confirmBolusFaster: Bool = false
  63. var onlyAutotuneBasals: Bool = false
  64. var overrideFactor: Decimal = 0.8
  65. var fattyMeals: Bool = false
  66. var fattyMealFactor: Decimal = 0.7
  67. var sweetMeals: Bool = false
  68. var sweetMealFactor: Decimal = 2
  69. var displayPresets: Bool = true
  70. var useLiveActivity: Bool = false
  71. var lockScreenView: LockScreenView = .simple
  72. var showChart: Bool = true
  73. var showCurrentGlucose: Bool = true
  74. var showChangeLabel: Bool = true
  75. var showIOB: Bool = true
  76. var showCOB: Bool = true
  77. var showUpdatedLabel: Bool = true
  78. var bolusShortcut: BolusShortcutLimit = .notAllowed
  79. }
  80. extension FreeAPSSettings: Decodable {
  81. // Needed to decode incomplete JSON
  82. init(from decoder: Decoder) throws {
  83. let container = try decoder.container(keyedBy: CodingKeys.self)
  84. var settings = FreeAPSSettings()
  85. if let units = try? container.decode(GlucoseUnits.self, forKey: .units) {
  86. settings.units = units
  87. }
  88. if let closedLoop = try? container.decode(Bool.self, forKey: .closedLoop) {
  89. settings.closedLoop = closedLoop
  90. }
  91. if let allowAnnouncements = try? container.decode(Bool.self, forKey: .allowAnnouncements) {
  92. settings.allowAnnouncements = allowAnnouncements
  93. }
  94. if let useAutotune = try? container.decode(Bool.self, forKey: .useAutotune) {
  95. settings.useAutotune = useAutotune
  96. }
  97. if let isUploadEnabled = try? container.decode(Bool.self, forKey: .isUploadEnabled) {
  98. settings.isUploadEnabled = isUploadEnabled
  99. }
  100. if let isDownloadEnabled = try? container.decode(Bool.self, forKey: .isDownloadEnabled) {
  101. settings.isDownloadEnabled = isDownloadEnabled
  102. }
  103. if let useLocalGlucoseSource = try? container.decode(Bool.self, forKey: .useLocalGlucoseSource) {
  104. settings.useLocalGlucoseSource = useLocalGlucoseSource
  105. }
  106. if let localGlucosePort = try? container.decode(Int.self, forKey: .localGlucosePort) {
  107. settings.localGlucosePort = localGlucosePort
  108. }
  109. if let debugOptions = try? container.decode(Bool.self, forKey: .debugOptions) {
  110. settings.debugOptions = debugOptions
  111. }
  112. if let displayHR = try? container.decode(Bool.self, forKey: .displayHR) {
  113. settings.displayHR = displayHR
  114. // compatibility if displayOnWatch is not available in json files
  115. settings.displayOnWatch = (displayHR == true) ? AwConfig.HR : AwConfig.BGTarget
  116. }
  117. if let displayOnWatch = try? container.decode(AwConfig.self, forKey: .displayOnWatch) {
  118. settings.displayOnWatch = displayOnWatch
  119. }
  120. if let cgm = try? container.decode(CGMType.self, forKey: .cgm) {
  121. settings.cgm = cgm
  122. }
  123. if let cgmPluginIdentifier = try? container.decode(String.self, forKey: .cgmPluginIdentifier) {
  124. settings.cgmPluginIdentifier = cgmPluginIdentifier
  125. }
  126. if let uploadGlucose = try? container.decode(Bool.self, forKey: .uploadGlucose) {
  127. settings.uploadGlucose = uploadGlucose
  128. }
  129. if let useCalendar = try? container.decode(Bool.self, forKey: .useCalendar) {
  130. settings.useCalendar = useCalendar
  131. }
  132. if let displayCalendarIOBandCOB = try? container.decode(Bool.self, forKey: .displayCalendarIOBandCOB) {
  133. settings.displayCalendarIOBandCOB = displayCalendarIOBandCOB
  134. }
  135. if let displayCalendarEmojis = try? container.decode(Bool.self, forKey: .displayCalendarEmojis) {
  136. settings.displayCalendarEmojis = displayCalendarEmojis
  137. }
  138. if let useAppleHealth = try? container.decode(Bool.self, forKey: .useAppleHealth) {
  139. settings.useAppleHealth = useAppleHealth
  140. }
  141. if let glucoseBadge = try? container.decode(Bool.self, forKey: .glucoseBadge) {
  142. settings.glucoseBadge = glucoseBadge
  143. }
  144. if let useFPUconversion = try? container.decode(Bool.self, forKey: .useFPUconversion) {
  145. settings.useFPUconversion = useFPUconversion
  146. }
  147. if let totalInsulinDisplayType = try? container.decode(TotalInsulinDisplayType.self, forKey: .totalInsulinDisplayType) {
  148. settings.totalInsulinDisplayType = totalInsulinDisplayType
  149. }
  150. if let individualAdjustmentFactor = try? container.decode(Decimal.self, forKey: .individualAdjustmentFactor) {
  151. settings.individualAdjustmentFactor = individualAdjustmentFactor
  152. }
  153. if let fattyMeals = try? container.decode(Bool.self, forKey: .fattyMeals) {
  154. settings.fattyMeals = fattyMeals
  155. }
  156. if let fattyMealFactor = try? container.decode(Decimal.self, forKey: .fattyMealFactor) {
  157. settings.fattyMealFactor = fattyMealFactor
  158. }
  159. if let sweetMeals = try? container.decode(Bool.self, forKey: .sweetMeals) {
  160. settings.sweetMeals = sweetMeals
  161. }
  162. if let sweetMealFactor = try? container.decode(Decimal.self, forKey: .sweetMealFactor) {
  163. settings.sweetMealFactor = sweetMealFactor
  164. }
  165. if let overrideFactor = try? container.decode(Decimal.self, forKey: .overrideFactor) {
  166. settings.overrideFactor = overrideFactor
  167. }
  168. if let timeCap = try? container.decode(Int.self, forKey: .timeCap) {
  169. settings.timeCap = timeCap
  170. }
  171. if let minuteInterval = try? container.decode(Int.self, forKey: .minuteInterval) {
  172. settings.minuteInterval = minuteInterval
  173. }
  174. if let delay = try? container.decode(Int.self, forKey: .delay) {
  175. settings.delay = delay
  176. }
  177. if let glucoseNotificationsAlways = try? container.decode(Bool.self, forKey: .glucoseNotificationsAlways) {
  178. settings.glucoseNotificationsAlways = glucoseNotificationsAlways
  179. }
  180. if let useAlarmSound = try? container.decode(Bool.self, forKey: .useAlarmSound) {
  181. settings.useAlarmSound = useAlarmSound
  182. }
  183. if let addSourceInfoToGlucoseNotifications = try? container.decode(
  184. Bool.self,
  185. forKey: .addSourceInfoToGlucoseNotifications
  186. ) {
  187. settings.addSourceInfoToGlucoseNotifications = addSourceInfoToGlucoseNotifications
  188. }
  189. if let lowGlucose = try? container.decode(Decimal.self, forKey: .lowGlucose) {
  190. settings.lowGlucose = lowGlucose
  191. }
  192. if let highGlucose = try? container.decode(Decimal.self, forKey: .highGlucose) {
  193. settings.highGlucose = highGlucose
  194. }
  195. if let carbsRequiredThreshold = try? container.decode(Decimal.self, forKey: .carbsRequiredThreshold) {
  196. settings.carbsRequiredThreshold = carbsRequiredThreshold
  197. }
  198. if let showCarbsRequiredBadge = try? container.decode(Bool.self, forKey: .showCarbsRequiredBadge) {
  199. settings.showCarbsRequiredBadge = showCarbsRequiredBadge
  200. }
  201. if let smoothGlucose = try? container.decode(Bool.self, forKey: .smoothGlucose) {
  202. settings.smoothGlucose = smoothGlucose
  203. }
  204. if let low = try? container.decode(Decimal.self, forKey: .low) {
  205. settings.low = low
  206. }
  207. if let high = try? container.decode(Decimal.self, forKey: .high) {
  208. settings.high = high
  209. }
  210. if let hours = try? container.decode(Int.self, forKey: .hours) {
  211. settings.hours = hours
  212. }
  213. if let xGridLines = try? container.decode(Bool.self, forKey: .xGridLines) {
  214. settings.xGridLines = xGridLines
  215. }
  216. if let yGridLines = try? container.decode(Bool.self, forKey: .yGridLines) {
  217. settings.yGridLines = yGridLines
  218. }
  219. if let oneDimensionalGraph = try? container.decode(Bool.self, forKey: .oneDimensionalGraph) {
  220. settings.oneDimensionalGraph = oneDimensionalGraph
  221. }
  222. if let rulerMarks = try? container.decode(Bool.self, forKey: .rulerMarks) {
  223. settings.rulerMarks = rulerMarks
  224. }
  225. if let forecastDisplayType = try? container.decode(ForecastDisplayType.self, forKey: .forecastDisplayType) {
  226. settings.forecastDisplayType = forecastDisplayType
  227. }
  228. if let overrideHbA1cUnit = try? container.decode(Bool.self, forKey: .overrideHbA1cUnit) {
  229. settings.overrideHbA1cUnit = overrideHbA1cUnit
  230. }
  231. if let maxCarbs = try? container.decode(Decimal.self, forKey: .maxCarbs) {
  232. settings.maxCarbs = maxCarbs
  233. }
  234. if let maxFat = try? container.decode(Decimal.self, forKey: .maxFat) {
  235. settings.maxFat = maxFat
  236. }
  237. if let maxProtein = try? container.decode(Decimal.self, forKey: .maxProtein) {
  238. settings.maxProtein = maxProtein
  239. }
  240. if let displayFatAndProteinOnWatch = try? container.decode(Bool.self, forKey: .displayFatAndProteinOnWatch) {
  241. settings.displayFatAndProteinOnWatch = displayFatAndProteinOnWatch
  242. }
  243. if let confirmBolusFaster = try? container.decode(Bool.self, forKey: .confirmBolusFaster) {
  244. settings.confirmBolusFaster = confirmBolusFaster
  245. }
  246. if let onlyAutotuneBasals = try? container.decode(Bool.self, forKey: .onlyAutotuneBasals) {
  247. settings.onlyAutotuneBasals = onlyAutotuneBasals
  248. }
  249. if let displayPresets = try? container.decode(Bool.self, forKey: .displayPresets) {
  250. settings.displayPresets = displayPresets
  251. }
  252. if let useLiveActivity = try? container.decode(Bool.self, forKey: .useLiveActivity) {
  253. settings.useLiveActivity = useLiveActivity
  254. }
  255. if let lockScreenView = try? container.decode(LockScreenView.self, forKey: .lockScreenView) {
  256. settings.lockScreenView = lockScreenView
  257. }
  258. if let showChart = try? container.decode(Bool.self, forKey: .showChart) {
  259. settings.showChart = showChart
  260. }
  261. if let showCurrentGlucose = try? container.decode(Bool.self, forKey: .showCurrentGlucose) {
  262. settings.showCurrentGlucose = showCurrentGlucose
  263. }
  264. if let showChangeLabel = try? container.decode(Bool.self, forKey: .showChangeLabel) {
  265. settings.showChangeLabel = showChangeLabel
  266. }
  267. if let showIOB = try? container.decode(Bool.self, forKey: .showIOB) {
  268. settings.showIOB = showIOB
  269. }
  270. if let showCOB = try? container.decode(Bool.self, forKey: .showCOB) {
  271. settings.showCOB = showCOB
  272. }
  273. if let showUpdatedLabel = try? container.decode(Bool.self, forKey: .showUpdatedLabel) {
  274. settings.showUpdatedLabel = showUpdatedLabel
  275. }
  276. if let bolusShortcut = try? container.decode(BolusShortcutLimit.self, forKey: .bolusShortcut) {
  277. settings.bolusShortcut = bolusShortcut
  278. }
  279. self = settings
  280. }
  281. }