FreeAPSSettings.swift 13 KB

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