FreeAPSSettings.swift 13 KB

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