OnboardingRootView.swift 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. import SwiftUI
  2. import Swinject
  3. /// The main onboarding view that manages navigation between onboarding steps.
  4. extension Onboarding {
  5. struct RootView: BaseView {
  6. let resolver: Resolver
  7. @State var state = StateModel()
  8. @State private var navigationDirection: OnboardingNavigationDirection = .forward
  9. let onboardingManager: OnboardingManager
  10. // Step management
  11. @State private var currentStep: OnboardingStep = .welcome
  12. @State private var currentNightscoutSubstep: NightscoutSubstep = .setupSelection
  13. @State private var currentDeliverySubstep: DeliveryLimitSubstep = .maxIOB
  14. @State private var currentAutosensSubstep: AutosensSettingsSubstep = .autosensMin
  15. @State private var currentSMBSubstep: SMBSettingsSubstep = .enableSMBAlways
  16. @State private var currentTargetBehaviorSubstep: TargetBehaviorSubstep = .highTempTargetRaisesSensitivity
  17. // Animation states
  18. @State private var animationScale: CGFloat = 1.0
  19. @State private var animationOpacity: Double = 0
  20. @State private var isAnimating = false
  21. // Conditional button states for Nightscout substeps
  22. private var didSelectNightscoutSetupOption: Bool {
  23. currentNightscoutSubstep == .setupSelection && state
  24. .nightscoutSetupOption == .noSelection
  25. }
  26. private var hasValidNightscoutConnection: Bool {
  27. currentNightscoutSubstep == .connectToNightscout && !state.isConnectedToNS
  28. }
  29. private var didSelectNightscoutImportOption: Bool {
  30. currentNightscoutSubstep == .importFromNightscout && state.nightscoutImportOption == .noSelection
  31. }
  32. // Next button conditional
  33. private var shouldDisableNextButton: Bool {
  34. (currentStep == .startupGuide && !state.hasReadImportantStartupNotes)
  35. ||
  36. (currentStep == .diagnostics && state.diagnosticsSharingOption == .enabled && !state.hasAcceptedPrivacyPolicy)
  37. ||
  38. (currentStep == .nightscout && didSelectNightscoutSetupOption)
  39. ||
  40. (currentStep == .nightscout && hasValidNightscoutConnection)
  41. ||
  42. (currentStep == .nightscout && didSelectNightscoutImportOption)
  43. ||
  44. (currentStep == .algorithmSettings && !state.hasReadAlgorithmSetupInformation)
  45. }
  46. var body: some View {
  47. NavigationView {
  48. ZStack {
  49. // Background gradient
  50. LinearGradient(
  51. gradient: Gradient(colors: [Color.bgDarkBlue, Color.bgDarkerDarkBlue]),
  52. startPoint: .top,
  53. endPoint: .bottom
  54. )
  55. .ignoresSafeArea()
  56. VStack(spacing: 0) {
  57. if (nonInfoOnboardingSteps + [OnboardingStep.overview, OnboardingStep.completed]).contains(currentStep) {
  58. // Progress bar
  59. OnboardingProgressBar(
  60. currentStep: currentStep,
  61. currentSubstep: {
  62. switch currentStep {
  63. case .deliveryLimits: return currentDeliverySubstep.rawValue
  64. case .nightscout: return currentNightscoutSubstep.rawValue
  65. case .autosensSettings: return currentAutosensSubstep.rawValue
  66. case .smbSettings: return currentSMBSubstep.rawValue
  67. case .targetBehavior: return currentTargetBehaviorSubstep.rawValue
  68. default: return nil
  69. }
  70. }(),
  71. stepsWithSubsteps: [
  72. .nightscout: NightscoutSubstep.allCases.count,
  73. .deliveryLimits: DeliveryLimitSubstep.allCases.count,
  74. .autosensSettings: state.filteredAutosensSettingsSubsteps.count,
  75. .smbSettings: SMBSettingsSubstep.allCases.count,
  76. .targetBehavior: TargetBehaviorSubstep.allCases.count
  77. ],
  78. nightscoutSetupOption: state.nightscoutSetupOption
  79. )
  80. .padding(.top)
  81. } else {
  82. // avoid letting content scroll beneath the status bar / dynamic island for content views with no progress bar (which adds top spacing)
  83. Color.clear.frame(height: 1)
  84. }
  85. OnboardingStepContent(
  86. currentStep: $currentStep,
  87. currentNightscoutSubstep: $currentNightscoutSubstep,
  88. currentDeliverySubstep: $currentDeliverySubstep,
  89. currentAutosensSubstep: $currentAutosensSubstep,
  90. currentSMBSubstep: $currentSMBSubstep,
  91. currentTargetBehaviorSubstep: $currentTargetBehaviorSubstep,
  92. state: state,
  93. navigationDirection: navigationDirection
  94. )
  95. Spacer()
  96. OnboardingNavigationButtons(
  97. currentStep: $currentStep,
  98. currentNightscoutSubstep: $currentNightscoutSubstep,
  99. currentDeliverySubstep: $currentDeliverySubstep,
  100. currentAutosensSubstep: $currentAutosensSubstep,
  101. currentSMBSubstep: $currentSMBSubstep,
  102. currentTargetBehaviorSubstep: $currentTargetBehaviorSubstep,
  103. onboardingManager: onboardingManager,
  104. state: state,
  105. shouldDisableNextButton: shouldDisableNextButton,
  106. navigationDirectionChanged: { navigationDirection = $0 }
  107. )
  108. }
  109. }
  110. .navigationBarHidden(true)
  111. }
  112. .onChange(of: currentStep) { _, _ in
  113. // Reset animation when step changes
  114. animationScale = 0.9
  115. animationOpacity = 0
  116. isAnimating = false
  117. // Start new animation
  118. DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
  119. withAnimation(.easeInOut(duration: 0.7)) {
  120. animationOpacity = 1
  121. animationScale = 1.0
  122. }
  123. isAnimating = true
  124. }
  125. }
  126. .onAppear(perform: configureView)
  127. }
  128. }
  129. }
  130. /// A progress bar that shows the user's progress through the onboarding process.
  131. struct OnboardingProgressBar: View {
  132. let currentStep: OnboardingStep
  133. let currentSubstep: Int?
  134. let stepsWithSubsteps: [OnboardingStep: Int]
  135. let nightscoutSetupOption: NightscoutSetupOption
  136. var body: some View {
  137. HStack(spacing: 4) {
  138. ForEach(renderedSteps, id: \.id) { step in
  139. ZStack(alignment: .leading) {
  140. Rectangle()
  141. .fill(Color.gray.opacity(0.3))
  142. .frame(height: 4)
  143. .cornerRadius(2)
  144. GeometryReader { geo in
  145. Rectangle()
  146. .fill(Color.blue)
  147. .frame(
  148. width: geo.size.width * fillFraction(for: step.step, totalSubsteps: step.substeps),
  149. height: 4
  150. )
  151. .cornerRadius(2)
  152. }
  153. }
  154. .frame(height: 4)
  155. }
  156. }
  157. .padding(.horizontal)
  158. }
  159. private var renderedSteps: [(id: String, step: OnboardingStep, substeps: Int?)] {
  160. nonInfoOnboardingSteps.map {
  161. (id: "\($0.rawValue)", step: $0, substeps: stepsWithSubsteps[$0])
  162. }
  163. }
  164. private func fillFraction(for step: OnboardingStep, totalSubsteps: Int?) -> CGFloat {
  165. // If currentStep is .completed, fill everything
  166. if currentStep == .completed { return 1.0 }
  167. if let currentIndex = nonInfoOnboardingSteps.firstIndex(of: currentStep),
  168. let stepIndex = nonInfoOnboardingSteps.firstIndex(of: step),
  169. stepIndex < currentIndex
  170. {
  171. return 1.0
  172. }
  173. if step == currentStep {
  174. if let total = totalSubsteps, let current = currentSubstep {
  175. return CGFloat(current + 1) / CGFloat(total)
  176. } else {
  177. return 1.0
  178. }
  179. }
  180. // Handle special case: Nightscout was skipped
  181. if step == .nightscout,
  182. nightscoutSetupOption == .skipNightscoutSetup,
  183. let currentIndex = nonInfoOnboardingSteps.firstIndex(of: currentStep),
  184. let nightscoutIndex = nonInfoOnboardingSteps.firstIndex(of: .nightscout),
  185. currentIndex > nightscoutIndex
  186. {
  187. return 1.0
  188. }
  189. return 0.0
  190. }
  191. }
  192. struct OnboardingStepContent: View {
  193. @Binding var currentStep: OnboardingStep
  194. @Binding var currentNightscoutSubstep: NightscoutSubstep
  195. @Binding var currentDeliverySubstep: DeliveryLimitSubstep
  196. @Binding var currentAutosensSubstep: AutosensSettingsSubstep
  197. @Binding var currentSMBSubstep: SMBSettingsSubstep
  198. @Binding var currentTargetBehaviorSubstep: TargetBehaviorSubstep
  199. @Bindable var state: Onboarding.StateModel
  200. var navigationDirection: OnboardingNavigationDirection
  201. var body: some View {
  202. ScrollViewReader { scrollProxy in
  203. ScrollView(.vertical, showsIndicators: true) {
  204. VStack(alignment: .leading, spacing: 20) {
  205. Color.clear.frame(height: 0).id("top")
  206. if currentStep != .welcome && currentStep != .completed {
  207. HStack {
  208. if currentStep == .nightscout {
  209. Image(currentStep.iconName)
  210. .resizable()
  211. .scaledToFit()
  212. .frame(width: 60, height: 60)
  213. } else if currentStep == .bluetooth {
  214. Image(currentStep.iconName)
  215. .font(.system(size: 40))
  216. .foregroundColor(currentStep.accentColor)
  217. .frame(width: 60, height: 60)
  218. .background(
  219. Circle()
  220. .fill(currentStep.accentColor.opacity(0.2))
  221. )
  222. } else {
  223. Image(systemName: currentStep.iconName)
  224. .font(.system(size: 40))
  225. .foregroundColor(currentStep.accentColor)
  226. .frame(width: 60, height: 60)
  227. .background(
  228. Circle()
  229. .fill(currentStep.accentColor.opacity(0.2))
  230. )
  231. }
  232. VStack(alignment: .leading) {
  233. Text(currentStep.title)
  234. .font(.title)
  235. .fontWeight(.bold)
  236. .foregroundColor(.primary)
  237. Text(currentStep.description)
  238. .font(.subheadline)
  239. .foregroundColor(.secondary)
  240. .fixedSize(horizontal: false, vertical: true)
  241. }
  242. }
  243. .padding([.horizontal, .top])
  244. }
  245. Group {
  246. switch currentStep {
  247. case .welcome:
  248. WelcomeStepView()
  249. case .startupGuide:
  250. StartupGuideStepView(state: state)
  251. case .overview:
  252. OverviewStepView()
  253. case .diagnostics:
  254. DiagnosticsStepView(state: state)
  255. case .nightscout:
  256. switch currentNightscoutSubstep {
  257. case .setupSelection:
  258. NightscoutSetupStepView(state: state)
  259. case .connectToNightscout:
  260. NightscoutLoginStepView(state: state)
  261. case .importFromNightscout:
  262. NightscoutImportStepView(state: state)
  263. }
  264. case .unitSelection:
  265. UnitSelectionStepView(state: state)
  266. case .glucoseTarget:
  267. GlucoseTargetStepView(state: state)
  268. case .basalRates:
  269. BasalProfileStepView(state: state)
  270. case .carbRatio:
  271. CarbRatioStepView(state: state)
  272. case .insulinSensitivity:
  273. InsulinSensitivityStepView(state: state)
  274. case .deliveryLimits:
  275. DeliveryLimitsStepView(state: state, substep: currentDeliverySubstep)
  276. case .algorithmSettings:
  277. AlgorithmSettingsStepView(state: state)
  278. case .autosensSettings:
  279. AlgorithmSettingsSubstepView(state: state, substep: currentAutosensSubstep)
  280. case .smbSettings:
  281. AlgorithmSettingsSubstepView(state: state, substep: currentSMBSubstep)
  282. case .targetBehavior:
  283. AlgorithmSettingsSubstepView(state: state, substep: currentTargetBehaviorSubstep)
  284. case .notifications:
  285. NotificationPermissionStepView(state: state, currentStep: $currentStep)
  286. case .bluetooth:
  287. BluetoothPermissionStepView(
  288. state: state,
  289. bluetoothManager: state.bluetoothManager,
  290. currentStep: $currentStep
  291. )
  292. case .completed:
  293. CompletedStepView()
  294. }
  295. }
  296. .transition(
  297. navigationDirection == .forward
  298. ? .asymmetric(insertion: .move(edge: .trailing), removal: .move(edge: .leading))
  299. : .asymmetric(insertion: .move(edge: .leading), removal: .move(edge: .trailing))
  300. )
  301. .padding(.horizontal)
  302. .id(currentStep.id)
  303. }
  304. .padding(.bottom, 80)
  305. }
  306. .onChange(of: currentStep) { _, _ in scrollProxy.scrollTo("top", anchor: .top) }
  307. .onChange(of: currentNightscoutSubstep) { _, _ in scrollProxy.scrollTo("top", anchor: .top) }
  308. .onChange(of: currentDeliverySubstep) { _, _ in scrollProxy.scrollTo("top", anchor: .top) }
  309. .safeAreaInset(edge: .top) {
  310. // avoid letting content scroll beneath the status bar / dynamic island for content views with not progress bar (which adds top spacing)
  311. if currentStep == .startupGuide || currentStep == .completed {
  312. Color.clear.frame(height: 0)
  313. }
  314. }
  315. }
  316. }
  317. }
  318. struct OnboardingNavigationButtons: View {
  319. @Binding var currentStep: OnboardingStep
  320. @Binding var currentNightscoutSubstep: NightscoutSubstep
  321. @Binding var currentDeliverySubstep: DeliveryLimitSubstep
  322. @Binding var currentAutosensSubstep: AutosensSettingsSubstep
  323. @Binding var currentSMBSubstep: SMBSettingsSubstep
  324. @Binding var currentTargetBehaviorSubstep: TargetBehaviorSubstep
  325. let onboardingManager: OnboardingManager
  326. @Bindable var state: Onboarding.StateModel
  327. var shouldDisableNextButton: Bool
  328. var navigationDirectionChanged: (OnboardingNavigationDirection) -> Void
  329. var body: some View {
  330. HStack {
  331. if currentStep != .welcome {
  332. Button(action: {
  333. navigationDirectionChanged(.backward)
  334. withAnimation {
  335. handleBackNavigation()
  336. }
  337. }) {
  338. HStack {
  339. Image(systemName: "chevron.left")
  340. Text("Back")
  341. }
  342. .padding()
  343. .foregroundColor(.primary)
  344. }
  345. }
  346. Spacer()
  347. Button(action: {
  348. navigationDirectionChanged(.forward)
  349. withAnimation {
  350. handleNextNavigation()
  351. }
  352. }) {
  353. HStack {
  354. Text(currentStep == .completed ? "Get Started" : "Next")
  355. Image(systemName: "chevron.right")
  356. }
  357. .padding()
  358. .foregroundColor(.white)
  359. .background(Capsule().fill(!shouldDisableNextButton ? Color.blue : Color(.systemGray)))
  360. }
  361. .disabled(shouldDisableNextButton)
  362. }
  363. .padding(.horizontal)
  364. .padding(.bottom)
  365. }
  366. // MARK: - Navigation Logic
  367. private func handleBackNavigation() {
  368. switch currentStep {
  369. case .completed:
  370. currentStep = .targetBehavior
  371. currentTargetBehaviorSubstep = .halfBasalTarget
  372. case .nightscout:
  373. if currentNightscoutSubstep == .setupSelection,
  374. let previous = currentStep.previous
  375. {
  376. currentStep = previous
  377. currentNightscoutSubstep = .setupSelection
  378. } else {
  379. currentNightscoutSubstep = NightscoutSubstep(rawValue: currentNightscoutSubstep.rawValue - 1)!
  380. }
  381. case .deliveryLimits:
  382. if let previousSub = DeliveryLimitSubstep(rawValue: currentDeliverySubstep.rawValue - 1) {
  383. currentDeliverySubstep = previousSub
  384. } else if let previous = currentStep.previous {
  385. currentStep = previous
  386. currentDeliverySubstep = .maxIOB
  387. }
  388. case .algorithmSettings:
  389. if let previous = currentStep.previous {
  390. currentStep = previous
  391. currentDeliverySubstep = .minimumSafetyThreshold
  392. currentAutosensSubstep = .autosensMin
  393. }
  394. case .autosensSettings:
  395. let steps = state.filteredAutosensSettingsSubsteps
  396. if let current = steps.firstIndex(of: currentAutosensSubstep),
  397. current > 0
  398. {
  399. currentAutosensSubstep = steps[current - 1]
  400. } else if let previousStep = currentStep.previous {
  401. currentStep = previousStep
  402. currentAutosensSubstep = steps.first ?? .autosensMin
  403. }
  404. case .smbSettings:
  405. if let previous = SMBSettingsSubstep(rawValue: currentSMBSubstep.rawValue - 1) {
  406. /// If user has activated setting `.enableSMBAlways`, when navigating backwards
  407. /// skip other redundant "Enable SMB"-settings and go straight to `enableSMBAlways`
  408. /// from current substep `.allowSMBWithHighTempTarget`.
  409. if state.enableSMBAlways, currentSMBSubstep == .allowSMBWithHighTempTarget {
  410. currentSMBSubstep = .enableSMBAlways
  411. } else {
  412. currentSMBSubstep = previous
  413. }
  414. } else if let previousStep = currentStep.previous {
  415. currentStep = previousStep
  416. currentSMBSubstep = .enableSMBAlways
  417. switch state.pumpOptionForOnboardingUnits {
  418. case .dana,
  419. .minimed:
  420. currentAutosensSubstep = .rewindResetsAutosens
  421. case .omnipodDash,
  422. .omnipodEros:
  423. currentAutosensSubstep = .autosensMax
  424. }
  425. }
  426. case .targetBehavior:
  427. if let previous = TargetBehaviorSubstep(rawValue: currentTargetBehaviorSubstep.rawValue - 1) {
  428. currentTargetBehaviorSubstep = previous
  429. } else if let previousStep = currentStep.previous {
  430. currentStep = previousStep
  431. currentTargetBehaviorSubstep = .highTempTargetRaisesSensitivity
  432. currentSMBSubstep = .maxDeltaGlucoseThreshold
  433. }
  434. default:
  435. if let previous = currentStep.previous {
  436. currentStep = previous
  437. }
  438. }
  439. }
  440. private func handleNextNavigation() {
  441. switch currentStep {
  442. case .nightscout:
  443. if currentNightscoutSubstep != .importFromNightscout {
  444. if currentNightscoutSubstep == .setupSelection,
  445. state.nightscoutSetupOption == .skipNightscoutSetup,
  446. let next = currentStep.next
  447. {
  448. currentStep = next
  449. } else {
  450. currentNightscoutSubstep = NightscoutSubstep(rawValue: currentNightscoutSubstep.rawValue + 1)!
  451. }
  452. } else if currentNightscoutSubstep == .importFromNightscout,
  453. state.nightscoutImportOption == .useImport
  454. {
  455. Task {
  456. await state.importSettingsFromNightscout(currentStep: $currentStep)
  457. }
  458. } else if let next = currentStep.next {
  459. currentStep = next
  460. }
  461. case .deliveryLimits:
  462. if let next = DeliveryLimitSubstep(rawValue: currentDeliverySubstep.rawValue + 1) {
  463. currentDeliverySubstep = next
  464. } else if let nextStep = currentStep.next {
  465. currentStep = nextStep
  466. currentDeliverySubstep = .maxIOB
  467. }
  468. case .autosensSettings:
  469. let steps = state.filteredAutosensSettingsSubsteps
  470. if let current = steps.firstIndex(of: currentAutosensSubstep),
  471. current + 1 < steps.count
  472. {
  473. currentAutosensSubstep = steps[current + 1]
  474. } else if let nextStep = currentStep.next {
  475. currentStep = nextStep
  476. currentAutosensSubstep = steps.first ?? .autosensMin
  477. }
  478. case .smbSettings:
  479. if let next = SMBSettingsSubstep(rawValue: currentSMBSubstep.rawValue + 1) {
  480. /// If user has activated setting `.enableSMBAlways`, when navigating forward
  481. /// skip other redundant "Enable SMB"-settings and go straight to `.allowSMBWithHighTempTarget`
  482. /// from current substep `.enableSMBAlways`.
  483. if state.enableSMBAlways, currentSMBSubstep == .enableSMBAlways {
  484. currentSMBSubstep = .allowSMBWithHighTempTarget
  485. } else {
  486. currentSMBSubstep = next
  487. }
  488. } else if let nextStep = currentStep.next {
  489. currentStep = nextStep
  490. currentSMBSubstep = .enableSMBAlways
  491. }
  492. case .targetBehavior:
  493. if let next = TargetBehaviorSubstep(rawValue: currentTargetBehaviorSubstep.rawValue + 1) {
  494. currentTargetBehaviorSubstep = next
  495. } else if let nextStep = currentStep.next {
  496. currentStep = nextStep
  497. currentTargetBehaviorSubstep = .highTempTargetRaisesSensitivity
  498. }
  499. case .notifications:
  500. currentTargetBehaviorSubstep = .halfBasalTarget
  501. if let next = currentStep.next {
  502. state.notificationsManager.getNotificationSettings { notificationSettings in
  503. switch notificationSettings.authorizationStatus {
  504. case .notDetermined:
  505. state.notificationsManager.requestNotificationPermissions { granted in
  506. state.hasNotificationsGranted = granted
  507. currentStep = next
  508. }
  509. case .denied:
  510. state.shouldDisplayCustomNotificationAlert = true
  511. case .authorized,
  512. .ephemeral,
  513. .provisional:
  514. currentStep = next
  515. break
  516. @unknown default:
  517. currentStep = next
  518. }
  519. }
  520. }
  521. case .bluetooth:
  522. state.shouldDisplayBluetoothRequestAlert = true
  523. case .completed:
  524. state.saveOnboardingData()
  525. onboardingManager.completeOnboarding()
  526. Foundation.NotificationCenter.default.post(name: .onboardingCompleted, object: nil)
  527. default:
  528. if let next = currentStep.next {
  529. currentStep = next
  530. }
  531. }
  532. }
  533. }
  534. struct Onboarding_Preview: PreviewProvider {
  535. static var previews: some View {
  536. Group {
  537. let resolver = TrioApp.resolver
  538. let onboardingManager = OnboardingManager()
  539. Onboarding.RootView(resolver: resolver, onboardingManager: onboardingManager)
  540. .previewDisplayName("Onboarding Flow")
  541. }
  542. }
  543. }