FreeAPSSettings.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 glucoseNotificationsAlways: Bool = false
  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. }
  76. extension FreeAPSSettings: Decodable {
  77. // Needed to decode incomplete JSON
  78. init(from decoder: Decoder) throws {
  79. let container = try decoder.container(keyedBy: CodingKeys.self)
  80. var settings = FreeAPSSettings()
  81. if let units = try? container.decode(GlucoseUnits.self, forKey: .units) {
  82. settings.units = units
  83. }
  84. if let closedLoop = try? container.decode(Bool.self, forKey: .closedLoop) {
  85. settings.closedLoop = closedLoop
  86. }
  87. if let isUploadEnabled = try? container.decode(Bool.self, forKey: .isUploadEnabled) {
  88. settings.isUploadEnabled = isUploadEnabled
  89. }
  90. if let isDownloadEnabled = try? container.decode(Bool.self, forKey: .isDownloadEnabled) {
  91. settings.isDownloadEnabled = isDownloadEnabled
  92. }
  93. if let useLocalGlucoseSource = try? container.decode(Bool.self, forKey: .useLocalGlucoseSource) {
  94. settings.useLocalGlucoseSource = useLocalGlucoseSource
  95. }
  96. if let localGlucosePort = try? container.decode(Int.self, forKey: .localGlucosePort) {
  97. settings.localGlucosePort = localGlucosePort
  98. }
  99. if let debugOptions = try? container.decode(Bool.self, forKey: .debugOptions) {
  100. settings.debugOptions = debugOptions
  101. }
  102. if let displayHR = try? container.decode(Bool.self, forKey: .displayHR) {
  103. settings.displayHR = displayHR
  104. // compatibility if displayOnWatch is not available in json files
  105. settings.displayOnWatch = (displayHR == true) ? AwConfig.HR : AwConfig.BGTarget
  106. }
  107. if let displayOnWatch = try? container.decode(AwConfig.self, forKey: .displayOnWatch) {
  108. settings.displayOnWatch = displayOnWatch
  109. }
  110. if let cgm = try? container.decode(CGMType.self, forKey: .cgm) {
  111. settings.cgm = cgm
  112. }
  113. if let cgmPluginIdentifier = try? container.decode(String.self, forKey: .cgmPluginIdentifier) {
  114. settings.cgmPluginIdentifier = cgmPluginIdentifier
  115. }
  116. if let uploadGlucose = try? container.decode(Bool.self, forKey: .uploadGlucose) {
  117. settings.uploadGlucose = uploadGlucose
  118. }
  119. if let useCalendar = try? container.decode(Bool.self, forKey: .useCalendar) {
  120. settings.useCalendar = useCalendar
  121. }
  122. if let displayCalendarIOBandCOB = try? container.decode(Bool.self, forKey: .displayCalendarIOBandCOB) {
  123. settings.displayCalendarIOBandCOB = displayCalendarIOBandCOB
  124. }
  125. if let displayCalendarEmojis = try? container.decode(Bool.self, forKey: .displayCalendarEmojis) {
  126. settings.displayCalendarEmojis = displayCalendarEmojis
  127. }
  128. if let useAppleHealth = try? container.decode(Bool.self, forKey: .useAppleHealth) {
  129. settings.useAppleHealth = useAppleHealth
  130. }
  131. if let glucoseBadge = try? container.decode(Bool.self, forKey: .glucoseBadge) {
  132. settings.glucoseBadge = glucoseBadge
  133. }
  134. if let useFPUconversion = try? container.decode(Bool.self, forKey: .useFPUconversion) {
  135. settings.useFPUconversion = useFPUconversion
  136. }
  137. if let totalInsulinDisplayType = try? container.decode(TotalInsulinDisplayType.self, forKey: .totalInsulinDisplayType) {
  138. settings.totalInsulinDisplayType = totalInsulinDisplayType
  139. }
  140. if let individualAdjustmentFactor = try? container.decode(Decimal.self, forKey: .individualAdjustmentFactor) {
  141. settings.individualAdjustmentFactor = individualAdjustmentFactor
  142. }
  143. if let fattyMeals = try? container.decode(Bool.self, forKey: .fattyMeals) {
  144. settings.fattyMeals = fattyMeals
  145. }
  146. if let fattyMealFactor = try? container.decode(Decimal.self, forKey: .fattyMealFactor) {
  147. settings.fattyMealFactor = fattyMealFactor
  148. }
  149. if let sweetMeals = try? container.decode(Bool.self, forKey: .sweetMeals) {
  150. settings.sweetMeals = sweetMeals
  151. }
  152. if let sweetMealFactor = try? container.decode(Decimal.self, forKey: .sweetMealFactor) {
  153. settings.sweetMealFactor = sweetMealFactor
  154. }
  155. if let overrideFactor = try? container.decode(Decimal.self, forKey: .overrideFactor) {
  156. settings.overrideFactor = overrideFactor
  157. }
  158. if let timeCap = try? container.decode(Int.self, forKey: .timeCap) {
  159. settings.timeCap = timeCap
  160. }
  161. if let minuteInterval = try? container.decode(Int.self, forKey: .minuteInterval) {
  162. settings.minuteInterval = minuteInterval
  163. }
  164. if let delay = try? container.decode(Int.self, forKey: .delay) {
  165. settings.delay = delay
  166. }
  167. if let notificationsPump = try? container.decode(Bool.self, forKey: .notificationsPump) {
  168. settings.notificationsPump = notificationsPump
  169. }
  170. if let notificationsCgm = try? container.decode(Bool.self, forKey: .notificationsCgm) {
  171. settings.notificationsCgm = notificationsCgm
  172. }
  173. if let notificationsCarb = try? container.decode(Bool.self, forKey: .notificationsCarb) {
  174. settings.notificationsCarb = notificationsCarb
  175. }
  176. if let notificationsAlgorithm = try? container.decode(Bool.self, forKey: .notificationsAlgorithm) {
  177. settings.notificationsAlgorithm = notificationsAlgorithm
  178. }
  179. if let glucoseNotificationsAlways = try? container.decode(Bool.self, forKey: .glucoseNotificationsAlways) {
  180. settings.glucoseNotificationsAlways = glucoseNotificationsAlways
  181. }
  182. if let useAlarmSound = try? container.decode(Bool.self, forKey: .useAlarmSound) {
  183. settings.useAlarmSound = useAlarmSound
  184. }
  185. if let addSourceInfoToGlucoseNotifications = try? container.decode(
  186. Bool.self,
  187. forKey: .addSourceInfoToGlucoseNotifications
  188. ) {
  189. settings.addSourceInfoToGlucoseNotifications = addSourceInfoToGlucoseNotifications
  190. }
  191. if let lowGlucose = try? container.decode(Decimal.self, forKey: .lowGlucose) {
  192. settings.lowGlucose = lowGlucose
  193. }
  194. if let highGlucose = try? container.decode(Decimal.self, forKey: .highGlucose) {
  195. settings.highGlucose = highGlucose
  196. }
  197. if let carbsRequiredThreshold = try? container.decode(Decimal.self, forKey: .carbsRequiredThreshold) {
  198. settings.carbsRequiredThreshold = carbsRequiredThreshold
  199. }
  200. if let showCarbsRequiredBadge = try? container.decode(Bool.self, forKey: .showCarbsRequiredBadge) {
  201. settings.showCarbsRequiredBadge = showCarbsRequiredBadge
  202. }
  203. if let smoothGlucose = try? container.decode(Bool.self, forKey: .smoothGlucose) {
  204. settings.smoothGlucose = smoothGlucose
  205. }
  206. if let low = try? container.decode(Decimal.self, forKey: .low) {
  207. settings.low = low
  208. }
  209. if let high = try? container.decode(Decimal.self, forKey: .high) {
  210. settings.high = high
  211. }
  212. if let hours = try? container.decode(Int.self, forKey: .hours) {
  213. settings.hours = hours
  214. }
  215. if let glucoseColorScheme = try? container.decode(GlucoseColorScheme.self, forKey: .glucoseColorScheme) {
  216. settings.glucoseColorScheme = glucoseColorScheme
  217. }
  218. if let xGridLines = try? container.decode(Bool.self, forKey: .xGridLines) {
  219. settings.xGridLines = xGridLines
  220. }
  221. if let yGridLines = try? container.decode(Bool.self, forKey: .yGridLines) {
  222. settings.yGridLines = yGridLines
  223. }
  224. if let timeInRangeChartStyle = try? container.decode(TimeInRangeChartStyle.self, forKey: .timeInRangeChartStyle) {
  225. settings.timeInRangeChartStyle = timeInRangeChartStyle
  226. }
  227. if let rulerMarks = try? container.decode(Bool.self, forKey: .rulerMarks) {
  228. settings.rulerMarks = rulerMarks
  229. }
  230. if let forecastDisplayType = try? container.decode(ForecastDisplayType.self, forKey: .forecastDisplayType) {
  231. settings.forecastDisplayType = forecastDisplayType
  232. }
  233. if let hbA1cDisplayUnit = try? container.decode(HbA1cDisplayUnit.self, forKey: .hbA1cDisplayUnit) {
  234. settings.hbA1cDisplayUnit = hbA1cDisplayUnit
  235. }
  236. if let maxCarbs = try? container.decode(Decimal.self, forKey: .maxCarbs) {
  237. settings.maxCarbs = maxCarbs
  238. }
  239. if let maxFat = try? container.decode(Decimal.self, forKey: .maxFat) {
  240. settings.maxFat = maxFat
  241. }
  242. if let maxProtein = try? container.decode(Decimal.self, forKey: .maxProtein) {
  243. settings.maxProtein = maxProtein
  244. }
  245. if let displayFatAndProteinOnWatch = try? container.decode(Bool.self, forKey: .displayFatAndProteinOnWatch) {
  246. settings.displayFatAndProteinOnWatch = displayFatAndProteinOnWatch
  247. }
  248. if let confirmBolusFaster = try? container.decode(Bool.self, forKey: .confirmBolusFaster) {
  249. settings.confirmBolusFaster = confirmBolusFaster
  250. }
  251. if let displayPresets = try? container.decode(Bool.self, forKey: .displayPresets) {
  252. settings.displayPresets = displayPresets
  253. }
  254. if let useLiveActivity = try? container.decode(Bool.self, forKey: .useLiveActivity) {
  255. settings.useLiveActivity = useLiveActivity
  256. }
  257. if let lockScreenView = try? container.decode(LockScreenView.self, forKey: .lockScreenView) {
  258. settings.lockScreenView = lockScreenView
  259. }
  260. if let bolusShortcut = try? container.decode(BolusShortcutLimit.self, forKey: .bolusShortcut) {
  261. settings.bolusShortcut = bolusShortcut
  262. }
  263. self = settings
  264. }
  265. }