TrioSettings.swift 14 KB

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