TrioSettings.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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")
  10. case .limitBolusMax:
  11. return String(localized: "Max bolus")
  12. }
  13. }
  14. }
  15. struct TrioSettings: JSON, Equatable, Encodable {
  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 cgm: CGMType = .none
  24. var cgmPluginIdentifier: String = ""
  25. var uploadGlucose: Bool = true
  26. var useCalendar: Bool = false
  27. var displayCalendarIOBandCOB: Bool = false
  28. var displayCalendarEmojis: Bool = false
  29. var glucoseBadge: Bool = false
  30. var notificationsPump: Bool = true
  31. var notificationsCgm: Bool = true
  32. var notificationsCarb: Bool = true
  33. var notificationsAlgorithm: Bool = true
  34. var glucoseNotificationsOption: GlucoseNotificationsOption = .onlyAlarmLimits
  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 = false
  41. var individualAdjustmentFactor: Decimal = 0.5
  42. var minuteInterval: Decimal = 30
  43. var delay: Decimal = 60
  44. var useAppleHealth: Bool = false
  45. var smoothGlucose: Bool = false
  46. var eA1cDisplayUnit: EstimatedA1cDisplayUnit = .percent
  47. var high: Decimal = 180
  48. var low: Decimal = 70
  49. var glucoseColorScheme: GlucoseColorScheme = .staticColor
  50. var xGridLines: Bool = true
  51. var yGridLines: Bool = true
  52. var hideInsulinBadge: Bool = false
  53. var allowDilution: Bool = false
  54. var insulinConcentration: Decimal = 1
  55. var showCobIobChart: Bool = true
  56. var rulerMarks: Bool = true
  57. var bolusDisplayThreshold: BolusDisplayThreshold = .allUnits
  58. var forecastDisplayType: ForecastDisplayType = .cone
  59. var maxCarbs: Decimal = 250
  60. var maxFat: Decimal = 250
  61. var maxProtein: Decimal = 250
  62. var confirmBolusFaster: Bool = false
  63. var overrideFactor: Decimal = 0.8
  64. var fattyMeals: Bool = false
  65. var fattyMealFactor: Decimal = 0.7
  66. var sweetMeals: Bool = false
  67. var sweetMealFactor: Decimal = 1
  68. var displayPresets: Bool = true
  69. var confirmBolus: Bool = false
  70. var useLiveActivity: Bool = false
  71. var lockScreenView: LockScreenView = .simple
  72. var smartStackView: LockScreenView = .simple
  73. var bolusShortcut: BolusShortcutLimit = .notAllowed
  74. var timeInRangeType: TimeInRangeType = .timeInTightRange
  75. /// Selected Garmin watchface (Trio or SwissAlpine)
  76. var garminWatchface: GarminWatchface = .trio
  77. var garminDatafield: GarminDatafield = .none
  78. /// Primary attribute choice for Garmin display (COB, ISF, or Sensitivity Ratio)
  79. var primaryAttributeChoice: GarminPrimaryAttributeChoice = .cob
  80. /// Secondary attribute choice for Garmin display (TBR or Eventual BG)
  81. var secondaryAttributeChoice: GarminSecondaryAttributeChoice = .tbr
  82. /// Controls whether watchface data transmission is enabled
  83. var isWatchfaceDataEnabled: Bool = false
  84. /// Computed property that groups all Garmin settings into a single struct
  85. var garminSettings: GarminWatchSettings {
  86. get {
  87. GarminWatchSettings(
  88. watchface: garminWatchface,
  89. datafield: garminDatafield,
  90. primaryAttributeChoice: primaryAttributeChoice,
  91. secondaryAttributeChoice: secondaryAttributeChoice,
  92. isWatchfaceDataEnabled: isWatchfaceDataEnabled
  93. )
  94. }
  95. set {
  96. garminWatchface = newValue.watchface
  97. garminDatafield = newValue.datafield
  98. primaryAttributeChoice = newValue.primaryAttributeChoice
  99. secondaryAttributeChoice = newValue.secondaryAttributeChoice
  100. isWatchfaceDataEnabled = newValue.isWatchfaceDataEnabled
  101. }
  102. }
  103. }
  104. extension TrioSettings: Decodable {
  105. /// Custom decoder to handle incomplete JSON and provide default values for missing fields
  106. init(from decoder: Decoder) throws {
  107. let container = try decoder.container(keyedBy: CodingKeys.self)
  108. var settings = TrioSettings()
  109. if let units = try? container.decode(GlucoseUnits.self, forKey: .units) {
  110. settings.units = units
  111. }
  112. if let closedLoop = try? container.decode(Bool.self, forKey: .closedLoop) {
  113. settings.closedLoop = closedLoop
  114. }
  115. if let isUploadEnabled = try? container.decode(Bool.self, forKey: .isUploadEnabled) {
  116. settings.isUploadEnabled = isUploadEnabled
  117. }
  118. if let isDownloadEnabled = try? container.decode(Bool.self, forKey: .isDownloadEnabled) {
  119. settings.isDownloadEnabled = isDownloadEnabled
  120. }
  121. if let useLocalGlucoseSource = try? container.decode(Bool.self, forKey: .useLocalGlucoseSource) {
  122. settings.useLocalGlucoseSource = useLocalGlucoseSource
  123. }
  124. if let localGlucosePort = try? container.decode(Int.self, forKey: .localGlucosePort) {
  125. settings.localGlucosePort = localGlucosePort
  126. }
  127. if let debugOptions = try? container.decode(Bool.self, forKey: .debugOptions) {
  128. settings.debugOptions = debugOptions
  129. }
  130. if let cgm = try? container.decode(CGMType.self, forKey: .cgm) {
  131. settings.cgm = cgm
  132. }
  133. if let cgmPluginIdentifier = try? container.decode(String.self, forKey: .cgmPluginIdentifier) {
  134. settings.cgmPluginIdentifier = cgmPluginIdentifier
  135. }
  136. if let uploadGlucose = try? container.decode(Bool.self, forKey: .uploadGlucose) {
  137. settings.uploadGlucose = uploadGlucose
  138. }
  139. if let useCalendar = try? container.decode(Bool.self, forKey: .useCalendar) {
  140. settings.useCalendar = useCalendar
  141. }
  142. if let displayCalendarIOBandCOB = try? container.decode(Bool.self, forKey: .displayCalendarIOBandCOB) {
  143. settings.displayCalendarIOBandCOB = displayCalendarIOBandCOB
  144. }
  145. if let displayCalendarEmojis = try? container.decode(Bool.self, forKey: .displayCalendarEmojis) {
  146. settings.displayCalendarEmojis = displayCalendarEmojis
  147. }
  148. if let useAppleHealth = try? container.decode(Bool.self, forKey: .useAppleHealth) {
  149. settings.useAppleHealth = useAppleHealth
  150. }
  151. if let glucoseBadge = try? container.decode(Bool.self, forKey: .glucoseBadge) {
  152. settings.glucoseBadge = glucoseBadge
  153. }
  154. if let useFPUconversion = try? container.decode(Bool.self, forKey: .useFPUconversion) {
  155. settings.useFPUconversion = useFPUconversion
  156. }
  157. if let individualAdjustmentFactor = try? container.decode(Decimal.self, forKey: .individualAdjustmentFactor) {
  158. settings.individualAdjustmentFactor = individualAdjustmentFactor
  159. }
  160. if let fattyMeals = try? container.decode(Bool.self, forKey: .fattyMeals) {
  161. settings.fattyMeals = fattyMeals
  162. }
  163. if let fattyMealFactor = try? container.decode(Decimal.self, forKey: .fattyMealFactor) {
  164. settings.fattyMealFactor = fattyMealFactor
  165. }
  166. if let sweetMeals = try? container.decode(Bool.self, forKey: .sweetMeals) {
  167. settings.sweetMeals = sweetMeals
  168. }
  169. if let sweetMealFactor = try? container.decode(Decimal.self, forKey: .sweetMealFactor) {
  170. settings.sweetMealFactor = sweetMealFactor
  171. }
  172. if let overrideFactor = try? container.decode(Decimal.self, forKey: .overrideFactor) {
  173. settings.overrideFactor = overrideFactor
  174. }
  175. if let minuteInterval = try? container.decode(Decimal.self, forKey: .minuteInterval) {
  176. settings.minuteInterval = minuteInterval
  177. }
  178. if let delay = try? container.decode(Decimal.self, forKey: .delay) {
  179. settings.delay = delay
  180. }
  181. if let notificationsPump = try? container.decode(Bool.self, forKey: .notificationsPump) {
  182. settings.notificationsPump = notificationsPump
  183. }
  184. if let notificationsCgm = try? container.decode(Bool.self, forKey: .notificationsCgm) {
  185. settings.notificationsCgm = notificationsCgm
  186. }
  187. if let notificationsCarb = try? container.decode(Bool.self, forKey: .notificationsCarb) {
  188. settings.notificationsCarb = notificationsCarb
  189. }
  190. if let notificationsAlgorithm = try? container.decode(Bool.self, forKey: .notificationsAlgorithm) {
  191. settings.notificationsAlgorithm = notificationsAlgorithm
  192. }
  193. if let glucoseNotificationsOption = try? container.decode(
  194. GlucoseNotificationsOption.self,
  195. forKey: .glucoseNotificationsOption
  196. ) {
  197. settings.glucoseNotificationsOption = glucoseNotificationsOption
  198. }
  199. if let addSourceInfoToGlucoseNotifications = try? container.decode(
  200. Bool.self,
  201. forKey: .addSourceInfoToGlucoseNotifications
  202. ) {
  203. settings.addSourceInfoToGlucoseNotifications = addSourceInfoToGlucoseNotifications
  204. }
  205. if let lowGlucose = try? container.decode(Decimal.self, forKey: .lowGlucose) {
  206. settings.lowGlucose = lowGlucose
  207. }
  208. if let highGlucose = try? container.decode(Decimal.self, forKey: .highGlucose) {
  209. settings.highGlucose = highGlucose
  210. }
  211. if let carbsRequiredThreshold = try? container.decode(Decimal.self, forKey: .carbsRequiredThreshold) {
  212. settings.carbsRequiredThreshold = carbsRequiredThreshold
  213. }
  214. if let showCarbsRequiredBadge = try? container.decode(Bool.self, forKey: .showCarbsRequiredBadge) {
  215. settings.showCarbsRequiredBadge = showCarbsRequiredBadge
  216. }
  217. if let smoothGlucose = try? container.decode(Bool.self, forKey: .smoothGlucose) {
  218. settings.smoothGlucose = smoothGlucose
  219. }
  220. if let low = try? container.decode(Decimal.self, forKey: .low) {
  221. settings.low = low
  222. }
  223. if let high = try? container.decode(Decimal.self, forKey: .high) {
  224. settings.high = high
  225. }
  226. if let glucoseColorScheme = try? container.decode(GlucoseColorScheme.self, forKey: .glucoseColorScheme) {
  227. settings.glucoseColorScheme = glucoseColorScheme
  228. }
  229. if let xGridLines = try? container.decode(Bool.self, forKey: .xGridLines) {
  230. settings.xGridLines = xGridLines
  231. }
  232. if let yGridLines = try? container.decode(Bool.self, forKey: .yGridLines) {
  233. settings.yGridLines = yGridLines
  234. }
  235. if let showCobIobChart = try? container.decode(Bool.self, forKey: .showCobIobChart) {
  236. settings.showCobIobChart = showCobIobChart
  237. }
  238. if let hideInsulinBadge = try? container.decode(Bool.self, forKey: .hideInsulinBadge) {
  239. settings.hideInsulinBadge = hideInsulinBadge
  240. }
  241. if let allowDilution = try? container.decode(Bool.self, forKey: .allowDilution) {
  242. settings.allowDilution = allowDilution
  243. }
  244. if let insulinConcentration = try? container.decode(Decimal.self, forKey: .insulinConcentration) {
  245. settings.insulinConcentration = insulinConcentration
  246. }
  247. if let rulerMarks = try? container.decode(Bool.self, forKey: .rulerMarks) {
  248. settings.rulerMarks = rulerMarks
  249. }
  250. if let bolusDisplayThreshold = try? container.decode(BolusDisplayThreshold.self, forKey: .bolusDisplayThreshold) {
  251. settings.bolusDisplayThreshold = bolusDisplayThreshold
  252. }
  253. if let forecastDisplayType = try? container.decode(ForecastDisplayType.self, forKey: .forecastDisplayType) {
  254. settings.forecastDisplayType = forecastDisplayType
  255. }
  256. if let eA1cDisplayUnit = try? container.decode(EstimatedA1cDisplayUnit.self, forKey: .eA1cDisplayUnit) {
  257. settings.eA1cDisplayUnit = eA1cDisplayUnit
  258. }
  259. if let maxCarbs = try? container.decode(Decimal.self, forKey: .maxCarbs) {
  260. settings.maxCarbs = maxCarbs
  261. }
  262. if let maxFat = try? container.decode(Decimal.self, forKey: .maxFat) {
  263. settings.maxFat = maxFat
  264. }
  265. if let maxProtein = try? container.decode(Decimal.self, forKey: .maxProtein) {
  266. settings.maxProtein = maxProtein
  267. }
  268. if let confirmBolusFaster = try? container.decode(Bool.self, forKey: .confirmBolusFaster) {
  269. settings.confirmBolusFaster = confirmBolusFaster
  270. }
  271. if let displayPresets = try? container.decode(Bool.self, forKey: .displayPresets) {
  272. settings.displayPresets = displayPresets
  273. }
  274. if let confirmBolus = try? container.decode(Bool.self, forKey: .confirmBolus) {
  275. settings.confirmBolus = confirmBolus
  276. }
  277. if let useLiveActivity = try? container.decode(Bool.self, forKey: .useLiveActivity) {
  278. settings.useLiveActivity = useLiveActivity
  279. }
  280. if let lockScreenView = try? container.decode(LockScreenView.self, forKey: .lockScreenView) {
  281. settings.lockScreenView = lockScreenView
  282. }
  283. if let smartStackView = try? container.decode(LockScreenView.self, forKey: .smartStackView) {
  284. settings.smartStackView = smartStackView
  285. }
  286. if let bolusShortcut = try? container.decode(BolusShortcutLimit.self, forKey: .bolusShortcut) {
  287. settings.bolusShortcut = bolusShortcut
  288. }
  289. if let timeInRangeType = try? container.decode(TimeInRangeType.self, forKey: .timeInRangeType) {
  290. settings.timeInRangeType = timeInRangeType
  291. }
  292. if let garminWatchface = try? container.decode(GarminWatchface.self, forKey: .garminWatchface) {
  293. settings.garminWatchface = garminWatchface
  294. }
  295. if let garminDatafield = try? container.decode(GarminDatafield.self, forKey: .garminDatafield) {
  296. settings.garminDatafield = garminDatafield
  297. }
  298. if let primaryAttributeChoice = try? container
  299. .decode(GarminPrimaryAttributeChoice.self, forKey: .primaryAttributeChoice)
  300. {
  301. settings.primaryAttributeChoice = primaryAttributeChoice
  302. }
  303. if let secondaryAttributeChoice = try? container.decode(
  304. GarminSecondaryAttributeChoice.self,
  305. forKey: .secondaryAttributeChoice
  306. ) {
  307. settings.secondaryAttributeChoice = secondaryAttributeChoice
  308. }
  309. if let isWatchfaceDataEnabled = try? container.decode(Bool.self, forKey: .isWatchfaceDataEnabled) {
  310. settings.isWatchfaceDataEnabled = isWatchfaceDataEnabled
  311. }
  312. self = settings
  313. }
  314. }