OnboardingView+Util.swift 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. import SwiftUI
  2. /// Represents the navigation direction in the onboarding flow
  3. enum OnboardingNavigationDirection {
  4. case forward
  5. case backward
  6. }
  7. /// Represents the different steps in the onboarding process.
  8. enum OnboardingStep: Int, CaseIterable, Identifiable, Equatable {
  9. case welcome
  10. case startupGuide
  11. case overview
  12. case diagnostics
  13. case nightscout
  14. case unitSelection
  15. case glucoseTarget
  16. case basalRates
  17. case carbRatio
  18. case insulinSensitivity
  19. case deliveryLimits
  20. case algorithmSettings
  21. case autosensSettings
  22. case smbSettings
  23. case targetBehavior
  24. case completed
  25. var id: Int { rawValue }
  26. var hasSubsteps: Bool {
  27. self == .deliveryLimits
  28. }
  29. var substeps: [DeliveryLimitSubstep] {
  30. guard hasSubsteps else { return [] }
  31. return DeliveryLimitSubstep.allCases
  32. }
  33. /// The title to display for this onboarding step.
  34. var title: String {
  35. switch self {
  36. case .welcome:
  37. return String(localized: "Welcome to Trio")
  38. case .startupGuide:
  39. return String(localized: "Startup Guide")
  40. case .overview:
  41. return String(localized: "Overview")
  42. case .diagnostics:
  43. return String(localized: "Diagnostics")
  44. case .nightscout:
  45. return String(localized: "Nightscout")
  46. case .unitSelection:
  47. return String(localized: "Units & Pump")
  48. case .glucoseTarget:
  49. return String(localized: "Glucose Targets")
  50. case .basalRates:
  51. return String(localized: "Basal Rates")
  52. case .carbRatio:
  53. return String(localized: "Carb Ratios")
  54. case .insulinSensitivity:
  55. return String(localized: "Insulin Sensitivities")
  56. case .deliveryLimits:
  57. return String(localized: "Delivery Limits")
  58. case .algorithmSettings:
  59. return String(localized: "Algorithm Settings")
  60. case .autosensSettings:
  61. return String(localized: "Autosens")
  62. case .smbSettings:
  63. return String(localized: "Super Micro Bolus (SMB)")
  64. case .targetBehavior:
  65. return String(localized: "Target Behavior")
  66. case .completed:
  67. return String(localized: "All Set!")
  68. }
  69. }
  70. /// A detailed description of what this onboarding step is about.
  71. var description: String {
  72. switch self {
  73. case .welcome:
  74. return String(
  75. localized: "Trio is a powerful app that helps you manage your diabetes. Let's get started by setting up a few important parameters that will help Trio work effectively for you."
  76. )
  77. case .startupGuide:
  78. return String(
  79. localized: "Trio comes with a helpful Startup Guide. We recommend opening it now and following along as you go — side by side."
  80. )
  81. case .overview:
  82. return String(
  83. localized: "Trio's Onboarding takes about 15-30 minutes to complete. We'll guide you through each step."
  84. )
  85. case .diagnostics:
  86. return String(
  87. localized: "By default, Trio collects crash reports and other anonymized data related to errors, exceptions, and overall app performance."
  88. )
  89. case .nightscout:
  90. return String(
  91. localized: "Nightscout is a cloud-based platform that allows you to store your diabetes data. It's often used by caregivers to remotely monitor what Trio is doing."
  92. )
  93. case .unitSelection:
  94. return String(
  95. localized: "Before you can begin with configuring your therapy settigns, Trio needs to know which units you use for your glucose and insulin measurements (based on your pump model)."
  96. )
  97. case .glucoseTarget:
  98. return String(
  99. localized: "Your glucose target is the blood glucose level you aim to maintain. Trio will use this to calculate insulin doses and provide recommendations."
  100. )
  101. case .basalRates:
  102. return String(
  103. localized: "Your basal profile represents the amount of background insulin you need throughout the day. This helps Trio calculate your insulin needs."
  104. )
  105. case .carbRatio:
  106. return String(
  107. localized: "Your carb ratio tells how many grams of carbohydrates one unit of insulin will cover. This is essential for accurate meal bolus calculations."
  108. )
  109. case .insulinSensitivity:
  110. return String(
  111. localized: "Your insulin sensitivity factor (ISF) indicates how much one unit of insulin will lower your blood glucose. This helps calculate correction boluses."
  112. )
  113. case .deliveryLimits:
  114. return String(
  115. localized: "Trio includes several safety limits for insulin delivery and carbohydrate entry, helping ensure a safe and effective experience."
  116. )
  117. case .algorithmSettings:
  118. return String(
  119. localized: "Trio includes several algorithm settings that allow you to customize the oref algorithm behavior to suit your specific needs."
  120. )
  121. case .autosensSettings:
  122. return String(
  123. localized: "Auto-sensitivity (Autosens) adjusts insulin delivery based on observed sensitivity or resistance."
  124. )
  125. case .smbSettings:
  126. return String(
  127. localized: "SMB (Super Micro Bolus) is an oref algorithm feature that delivers small frequent boluses instead of temporary basals for faster glucose control."
  128. )
  129. case .targetBehavior:
  130. return String(
  131. localized: "Target Behavior allows you to adjust how temporary targets influence ISF, basal, and auto-targeting based on sensitivity or resistance."
  132. )
  133. case .completed:
  134. return String(
  135. localized: "Great job! You've completed the initial setup of Trio. You can always adjust these settings later in the app."
  136. )
  137. }
  138. }
  139. /// The system icon name associated with this step.
  140. var iconName: String {
  141. switch self {
  142. case .welcome:
  143. return "hand.wave.fill"
  144. case .startupGuide:
  145. return "list.bullet.clipboard.fill"
  146. case .overview:
  147. return "checklist.unchecked"
  148. case .diagnostics:
  149. return "waveform.badge.magnifyingglass"
  150. case .nightscout:
  151. return "owl"
  152. case .unitSelection:
  153. return "numbers.rectangle"
  154. case .glucoseTarget:
  155. return "target"
  156. case .basalRates:
  157. return "chart.xyaxis.line"
  158. case .carbRatio:
  159. return "fork.knife"
  160. case .insulinSensitivity:
  161. return "drop.fill"
  162. case .deliveryLimits:
  163. return "slider.horizontal.3"
  164. case .algorithmSettings:
  165. return "gearshape.2.fill"
  166. case .autosensSettings:
  167. return "dial.low.fill"
  168. case .smbSettings:
  169. return "bolt.fill"
  170. case .targetBehavior:
  171. return "gyroscope"
  172. case .completed:
  173. return "checkmark.circle.fill"
  174. }
  175. }
  176. /// Returns the next step in the onboarding process, or nil if this is the last step.
  177. var next: OnboardingStep? {
  178. let allCases = OnboardingStep.allCases
  179. let currentIndex = allCases.firstIndex(of: self) ?? 0
  180. let nextIndex = currentIndex + 1
  181. return nextIndex < allCases.count ? allCases[nextIndex] : nil
  182. }
  183. /// Returns the previous step in the onboarding process, or nil if this is the first step.
  184. var previous: OnboardingStep? {
  185. let allCases = OnboardingStep.allCases
  186. let currentIndex = allCases.firstIndex(of: self) ?? 0
  187. let previousIndex = currentIndex - 1
  188. return previousIndex >= 0 ? allCases[previousIndex] : nil
  189. }
  190. /// The accent color to use for this step.
  191. var accentColor: Color {
  192. switch self {
  193. case .algorithmSettings,
  194. .autosensSettings,
  195. .completed,
  196. .deliveryLimits,
  197. .diagnostics,
  198. .nightscout,
  199. .overview,
  200. .smbSettings,
  201. .startupGuide,
  202. .targetBehavior,
  203. .unitSelection,
  204. .welcome:
  205. return Color.blue
  206. case .glucoseTarget:
  207. return Color.green
  208. case .basalRates:
  209. return Color.purple
  210. case .carbRatio:
  211. return Color.orange
  212. case .insulinSensitivity:
  213. return Color.red
  214. }
  215. }
  216. }
  217. var nonInfoOnboardingSteps: [OnboardingStep] { OnboardingStep.allCases
  218. .filter { $0 != .welcome && $0 != .startupGuide && $0 != .overview && $0 != .completed }
  219. }
  220. enum DeliveryLimitSubstep: Int, CaseIterable, Identifiable {
  221. case maxIOB
  222. case maxBolus
  223. case maxBasal
  224. case maxCOB
  225. case minimumSafetyThreshold
  226. var id: Int { rawValue }
  227. var title: String {
  228. switch self {
  229. case .maxIOB: return String(localized: "Max IOB", comment: "Max IOB")
  230. case .maxBolus: return String(localized: "Max Bolus")
  231. case .maxBasal: return String(localized: "Max Basal Rate")
  232. case .maxCOB: return String(localized: "Max COB", comment: "Max COB")
  233. case .minimumSafetyThreshold: return String(localized: "Minimum Safety Threshold")
  234. }
  235. }
  236. var hint: String {
  237. switch self {
  238. case .maxIOB: return String(localized: "Maximum units of insulin allowed to be active.")
  239. case .maxBolus: return String(localized: "Largest bolus of insulin allowed.")
  240. case .maxBasal: return String(localized: "Largest basal rate allowed.")
  241. case .maxCOB: return String(localized: "Maximum Carbs On Board (COB) allowed.")
  242. case .minimumSafetyThreshold: return String(localized: "Increase the safety threshold used to suspend insulin delivery.")
  243. }
  244. }
  245. func description(units: GlucoseUnits) -> any View {
  246. switch self {
  247. case .maxIOB:
  248. return VStack(alignment: .leading, spacing: 8) {
  249. Text(
  250. "Note: This setting must be greater than 0 for any automatic insulin dosing by Trio."
  251. ).bold().foregroundStyle(Color.primary)
  252. Text(
  253. "This is the maximum amount of Insulin On Board (IOB) above profile basal rates from all sources - positive temporary basal rates, manual or meal boluses, and SMBs - that Trio is allowed to accumulate to address an above target glucose."
  254. )
  255. Text(
  256. "If a calculated amount exceeds this limit, the suggested and / or delivered amount will be reduced so that active insulin on board (IOB) will not exceed this safety limit."
  257. )
  258. Text(
  259. "Note: You can still manually bolus above this limit, but the suggested bolus amount will never exceed this in the bolus calculator."
  260. )
  261. }
  262. case .maxBolus:
  263. return VStack(alignment: .leading, spacing: 8) {
  264. Text(
  265. "This is the maximum bolus allowed to be delivered at one time. This limits manual and automatic bolus."
  266. )
  267. Text("Most set this to their largest meal bolus. Then, adjust if needed.")
  268. Text("If you attempt to request a bolus larger than this, the bolus will not be accepted.")
  269. }
  270. case .maxBasal:
  271. return VStack(alignment: .leading, spacing: 8) {
  272. Text(
  273. "This is the maximum basal rate allowed to be set or scheduled. This applies to both automatic and manual basal rates."
  274. )
  275. Text(
  276. "Note to Medtronic Pump Users: You must also manually set the max basal rate on the pump to this value or higher."
  277. )
  278. }
  279. case .maxCOB:
  280. return VStack(alignment: .leading, spacing: 8) {
  281. Text(
  282. "This setting defines the maximum amount of Carbs On Board (COB) at any given time for Trio to use in dosing calculations. If more carbs are entered than allowed by this limit, Trio will cap the current COB in calculations to Max COB and remain at max until all remaining carbs have shown to be absorbed."
  283. )
  284. Text(
  285. "For example, if Max COB is 120 g and you enter a meal containing 150 g of carbs, your COB will remain at 120 g until the remaining 30 g have been absorbed."
  286. )
  287. Text("This is an important limit when UAM is ON.")
  288. }
  289. case .minimumSafetyThreshold:
  290. return VStack(alignment: .leading, spacing: 8) {
  291. Text("Default: Set by Algorithm").bold()
  292. Text(
  293. "Minimum Threshold Setting is, by default, determined by your set Glucose Target. This threshold automatically suspends insulin delivery if your glucose levels are forecasted to fall below this value. It’s designed to protect against hypoglycemia, particularly during sleep or other vulnerable times."
  294. )
  295. Text(
  296. "Trio will use the larger of the default setting calculation below and the value entered here."
  297. )
  298. VStack(alignment: .leading, spacing: 8) {
  299. VStack(alignment: .leading, spacing: 5) {
  300. Text("The default setting is based on this calculation:").bold()
  301. Text("TargetGlucose - 0.5 × (TargetGlucose - 40)")
  302. }
  303. VStack(alignment: .leading, spacing: 5) {
  304. Text(
  305. "If your glucose target is \(units == .mgdL ? "110" : 110.formattedAsMmolL) \(units.rawValue), Trio will use a safety threshold of \(units == .mgdL ? "75" : 75.formattedAsMmolL) \(units.rawValue), unless you set Minimum Safety Threshold to something > \(units == .mgdL ? "75" : 75.formattedAsMmolL) \(units.rawValue)."
  306. )
  307. Text(
  308. "\(units == .mgdL ? "110" : 110.formattedAsMmolL) - 0.5 × (\(units == .mgdL ? "110" : 110.formattedAsMmolL) - \(units == .mgdL ? "40" : 40.formattedAsMmolL)) = \(units == .mgdL ? "75" : 75.formattedAsMmolL)"
  309. )
  310. }
  311. Text(
  312. "This setting is limited to values between \(units == .mgdL ? "60" : 60.formattedAsMmolL) - \(units == .mgdL ? "120" : 120.formattedAsMmolL) \(units.rawValue)"
  313. )
  314. Text(
  315. "Note: Basal may be resumed if there is negative IOB and glucose is rising faster than the forecast."
  316. )
  317. }
  318. }
  319. }
  320. }
  321. }
  322. enum DiagnosticsSharingOption: String, Equatable, CaseIterable, Identifiable {
  323. case enabled
  324. case disabled
  325. var id: String { rawValue }
  326. var displayName: String {
  327. switch self {
  328. case .enabled:
  329. return "Enable Sharing"
  330. case .disabled:
  331. return "Disable Sharing"
  332. }
  333. }
  334. }
  335. enum PumpOptionForOnboardingUnits: String, Equatable, CaseIterable, Identifiable {
  336. case minimed
  337. case omnipodEros
  338. case omnipodDash
  339. case dana
  340. var id: String { rawValue }
  341. var displayName: String {
  342. switch self {
  343. case .minimed:
  344. return "Medtronic"
  345. case .omnipodEros:
  346. return "Omnipod Eros"
  347. case .omnipodDash:
  348. return "Omnipod Dash"
  349. case .dana:
  350. return "Dana (RS/-i)"
  351. }
  352. }
  353. }
  354. enum NightscoutSetupOption: String, Equatable, CaseIterable, Identifiable {
  355. case setupNightscout
  356. case skipNightscoutSetup
  357. case noSelection
  358. var id: String { rawValue }
  359. var displayName: String {
  360. switch self {
  361. case .setupNightscout:
  362. return String(localized: "Setup Nightscout for Trio")
  363. case .skipNightscoutSetup:
  364. return String(localized: "Skip Nightscout Setup")
  365. case .noSelection:
  366. return ""
  367. }
  368. }
  369. }
  370. enum NightscoutImportOption: String, Equatable, CaseIterable, Identifiable {
  371. case useImport
  372. case skipImport
  373. case noSelection
  374. var id: String { rawValue }
  375. var displayName: String {
  376. switch self {
  377. case .useImport:
  378. return String(localized: "Import Settings")
  379. case .skipImport:
  380. return String(localized: "Configure Yourself")
  381. case .noSelection:
  382. return ""
  383. }
  384. }
  385. }
  386. enum NightscoutSubstep: Int, CaseIterable, Identifiable {
  387. case setupSelection
  388. case connectToNightscout
  389. case importFromNightscout
  390. var id: Int { rawValue }
  391. }
  392. struct BulletPoint: View {
  393. let text: String
  394. init(_ text: String) {
  395. self.text = text
  396. }
  397. var body: some View {
  398. HStack(alignment: .top) {
  399. Text("•")
  400. Text(text)
  401. }
  402. }
  403. }
  404. enum OnboardingSettingItemType: Equatable, CaseIterable, Identifiable {
  405. case overview
  406. case complete
  407. var id: UUID {
  408. UUID()
  409. }
  410. }
  411. enum OnboardingInputSectionType: Equatable {
  412. case decimal
  413. case boolean
  414. static func == (lhs: OnboardingInputSectionType, rhs: OnboardingInputSectionType) -> Bool {
  415. switch (lhs, rhs) {
  416. case (.boolean, .boolean):
  417. return true
  418. case (.decimal, .decimal):
  419. return true
  420. default:
  421. return false
  422. }
  423. }
  424. }
  425. /// A reusable view for displaying setting items in the completed step.
  426. struct SettingItemView: View {
  427. let step: OnboardingStep
  428. let icon: String
  429. let title: String
  430. let type: OnboardingSettingItemType
  431. private var accentColor: Color {
  432. switch type {
  433. case .overview:
  434. Color.blue
  435. case .complete:
  436. Color.green
  437. }
  438. }
  439. var body: some View {
  440. HStack(spacing: 10) {
  441. if step == .nightscout {
  442. Image(icon)
  443. .resizable()
  444. .scaledToFit()
  445. .frame(width: 40, height: 24)
  446. .colorMultiply(accentColor)
  447. } else {
  448. Image(systemName: icon)
  449. .font(.system(size: 24))
  450. .foregroundStyle(accentColor)
  451. .frame(width: 40)
  452. }
  453. VStack(alignment: .leading, spacing: 2) {
  454. Text(title)
  455. .font(.headline)
  456. }
  457. Spacer()
  458. switch type {
  459. case .overview:
  460. let index = nonInfoOnboardingSteps.firstIndex(of: step) ?? 0
  461. let stepNumber = index + 1
  462. Text(stepNumber.description)
  463. .bold()
  464. .frame(width: 32, height: 32, alignment: .center)
  465. .background(accentColor)
  466. .foregroundStyle(.white)
  467. .clipShape(Capsule())
  468. case .complete:
  469. Image(systemName: "checkmark")
  470. .foregroundStyle(accentColor)
  471. }
  472. }
  473. .padding(.vertical, 8)
  474. }
  475. }