FreeAPSSettings.swift 13 KB

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