TrioSettings.swift 15 KB

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