OnboardingRootView.swift 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. private var shouldDisableNextButton: Bool {
  33. (currentStep == .nightscout && didSelectNightscoutSetupOption)
  34. ||
  35. (currentStep == .nightscout && hasValidNightscoutConnection)
  36. ||
  37. (currentStep == .nightscout && didSelectNightscoutImportOption)
  38. }
  39. var body: some View {
  40. NavigationView {
  41. ZStack {
  42. // Background gradient
  43. LinearGradient(
  44. gradient: Gradient(colors: [Color.bgDarkBlue, Color.bgDarkerDarkBlue]),
  45. startPoint: .top,
  46. endPoint: .bottom
  47. )
  48. .ignoresSafeArea()
  49. VStack(spacing: 0) {
  50. if (nonInfoOnboardingSteps + [OnboardingStep.overview, OnboardingStep.completed]).contains(currentStep) {
  51. // Progress bar
  52. OnboardingProgressBar(
  53. currentStep: currentStep,
  54. currentSubstep: {
  55. switch currentStep {
  56. case .deliveryLimits: return currentDeliverySubstep.rawValue
  57. case .nightscout: return currentNightscoutSubstep.rawValue
  58. case .autosensSettings: return currentAutosensSubstep.rawValue
  59. case .smbSettings: return currentSMBSubstep.rawValue
  60. case .targetBehavior: return currentTargetBehaviorSubstep.rawValue
  61. default: return nil
  62. }
  63. }(),
  64. stepsWithSubsteps: [
  65. .nightscout: NightscoutSubstep.allCases.count,
  66. .deliveryLimits: DeliveryLimitSubstep.allCases.count,
  67. .autosensSettings: AutosensSettingsSubstep.allCases.count,
  68. .smbSettings: SMBSettingsSubstep.allCases.count,
  69. .targetBehavior: TargetBehaviorSubstep.allCases.count
  70. ],
  71. nightscoutSetupOption: state.nightscoutSetupOption
  72. )
  73. .padding(.top)
  74. }
  75. OnboardingStepContent(
  76. currentStep: $currentStep,
  77. currentNightscoutSubstep: $currentNightscoutSubstep,
  78. currentDeliverySubstep: $currentDeliverySubstep,
  79. currentAutosensSubstep: $currentAutosensSubstep,
  80. currentSMBSubstep: $currentSMBSubstep,
  81. currentTargetBehaviorSubstep: $currentTargetBehaviorSubstep,
  82. state: state,
  83. navigationDirection: navigationDirection
  84. )
  85. Spacer()
  86. OnboardingNavigationButtons(
  87. currentStep: $currentStep,
  88. currentNightscoutSubstep: $currentNightscoutSubstep,
  89. currentDeliverySubstep: $currentDeliverySubstep,
  90. currentAutosensSubstep: $currentAutosensSubstep,
  91. currentSMBSubstep: $currentSMBSubstep,
  92. currentTargetBehaviorSubstep: $currentTargetBehaviorSubstep,
  93. onboardingManager: onboardingManager,
  94. state: state,
  95. shouldDisableNextButton: shouldDisableNextButton,
  96. navigationDirectionChanged: { navigationDirection = $0 }
  97. )
  98. }
  99. }
  100. .navigationBarHidden(true)
  101. }
  102. .onChange(of: currentStep) { _, _ in
  103. // Reset animation when step changes
  104. animationScale = 0.9
  105. animationOpacity = 0
  106. isAnimating = false
  107. // Start new animation
  108. DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
  109. withAnimation(.easeInOut(duration: 0.7)) {
  110. animationOpacity = 1
  111. animationScale = 1.0
  112. }
  113. isAnimating = true
  114. }
  115. }
  116. .onAppear(perform: configureView)
  117. }
  118. }
  119. }
  120. /// A progress bar that shows the user's progress through the onboarding process.
  121. struct OnboardingProgressBar: View {
  122. let currentStep: OnboardingStep
  123. let currentSubstep: Int?
  124. let stepsWithSubsteps: [OnboardingStep: Int]
  125. let nightscoutSetupOption: NightscoutSetupOption
  126. var body: some View {
  127. HStack(spacing: 4) {
  128. ForEach(renderedSteps, id: \.id) { step in
  129. ZStack(alignment: .leading) {
  130. Rectangle()
  131. .fill(Color.gray.opacity(0.3))
  132. .frame(height: 4)
  133. .cornerRadius(2)
  134. GeometryReader { geo in
  135. Rectangle()
  136. .fill(Color.blue)
  137. .frame(
  138. width: geo.size.width * fillFraction(for: step.step, totalSubsteps: step.substeps),
  139. height: 4
  140. )
  141. .cornerRadius(2)
  142. }
  143. }
  144. .frame(height: 4)
  145. }
  146. }
  147. .padding(.horizontal)
  148. }
  149. private var renderedSteps: [(id: String, step: OnboardingStep, substeps: Int?)] {
  150. nonInfoOnboardingSteps.map {
  151. (id: "\($0.rawValue)", step: $0, substeps: stepsWithSubsteps[$0])
  152. }
  153. }
  154. private func fillFraction(for step: OnboardingStep, totalSubsteps: Int?) -> CGFloat {
  155. // If currentStep is .completed, fill everything
  156. if currentStep == .completed { return 1.0 }
  157. if let currentIndex = nonInfoOnboardingSteps.firstIndex(of: currentStep),
  158. let stepIndex = nonInfoOnboardingSteps.firstIndex(of: step),
  159. stepIndex < currentIndex
  160. {
  161. return 1.0
  162. }
  163. if step == currentStep {
  164. if let total = totalSubsteps, let current = currentSubstep {
  165. return CGFloat(current + 1) / CGFloat(total)
  166. } else {
  167. return 1.0
  168. }
  169. }
  170. // Handle special case: Nightscout was skipped
  171. if step == .nightscout,
  172. nightscoutSetupOption == .skipNightscoutSetup,
  173. let currentIndex = nonInfoOnboardingSteps.firstIndex(of: currentStep),
  174. let nightscoutIndex = nonInfoOnboardingSteps.firstIndex(of: .nightscout),
  175. currentIndex > nightscoutIndex
  176. {
  177. return 1.0
  178. }
  179. return 0.0
  180. }
  181. }
  182. struct OnboardingStepContent: View {
  183. @Binding var currentStep: OnboardingStep
  184. @Binding var currentNightscoutSubstep: NightscoutSubstep
  185. @Binding var currentDeliverySubstep: DeliveryLimitSubstep
  186. @Binding var currentAutosensSubstep: AutosensSettingsSubstep
  187. @Binding var currentSMBSubstep: SMBSettingsSubstep
  188. @Binding var currentTargetBehaviorSubstep: TargetBehaviorSubstep
  189. @Bindable var state: Onboarding.StateModel
  190. var navigationDirection: OnboardingNavigationDirection
  191. var body: some View {
  192. ScrollViewReader { scrollProxy in
  193. ScrollView(.vertical, showsIndicators: true) {
  194. VStack(alignment: .leading, spacing: 20) {
  195. Color.clear.frame(height: 0).id("top")
  196. if currentStep != .welcome && currentStep != .completed {
  197. HStack {
  198. if currentStep == .nightscout {
  199. Image(currentStep.iconName)
  200. .resizable()
  201. .scaledToFit()
  202. .frame(width: 60, height: 60)
  203. } else {
  204. Image(systemName: currentStep.iconName)
  205. .font(.system(size: 40))
  206. .foregroundColor(currentStep.accentColor)
  207. .frame(width: 60, height: 60)
  208. .background(
  209. Circle()
  210. .fill(currentStep.accentColor.opacity(0.2))
  211. )
  212. }
  213. VStack(alignment: .leading) {
  214. Text(currentStep.title)
  215. .font(.title)
  216. .fontWeight(.bold)
  217. .foregroundColor(.primary)
  218. Text(currentStep.description)
  219. .font(.subheadline)
  220. .foregroundColor(.secondary)
  221. .fixedSize(horizontal: false, vertical: true)
  222. }
  223. }
  224. .padding([.horizontal, .top])
  225. }
  226. Group {
  227. switch currentStep {
  228. case .welcome:
  229. WelcomeStepView()
  230. case .startupGuide:
  231. StartupGuideStepView()
  232. case .overview:
  233. OverviewStepView()
  234. case .diagnostics:
  235. DiagnosticsStepView(state: state)
  236. case .nightscout:
  237. switch currentNightscoutSubstep {
  238. case .setupSelection:
  239. NightscoutSetupStepView(state: state)
  240. case .connectToNightscout:
  241. NightscoutLoginStepView(state: state)
  242. case .importFromNightscout:
  243. NightscoutImportStepView(state: state)
  244. }
  245. case .unitSelection:
  246. UnitSelectionStepView(state: state)
  247. case .glucoseTarget:
  248. GlucoseTargetStepView(state: state)
  249. case .basalRates:
  250. BasalProfileStepView(state: state)
  251. case .carbRatio:
  252. CarbRatioStepView(state: state)
  253. case .insulinSensitivity:
  254. InsulinSensitivityStepView(state: state)
  255. case .deliveryLimits:
  256. DeliveryLimitsStepView(state: state, substep: currentDeliverySubstep)
  257. case .algorithmSettings:
  258. AlgorithmSettingsStepView(state: state)
  259. case .autosensSettings:
  260. AlgorithmSubstepView(state: state, substep: currentAutosensSubstep)
  261. case .smbSettings:
  262. AlgorithmSubstepView(state: state, substep: currentSMBSubstep)
  263. case .targetBehavior:
  264. AlgorithmSubstepView(state: state, substep: currentTargetBehaviorSubstep)
  265. case .completed:
  266. CompletedStepView()
  267. }
  268. }
  269. .transition(
  270. navigationDirection == .forward
  271. ? .asymmetric(insertion: .move(edge: .trailing), removal: .move(edge: .leading))
  272. : .asymmetric(insertion: .move(edge: .leading), removal: .move(edge: .trailing))
  273. )
  274. .padding(.horizontal)
  275. .id(currentStep.id)
  276. }
  277. .padding(.bottom, 80)
  278. }
  279. .onChange(of: currentStep) { _, _ in scrollProxy.scrollTo("top", anchor: .top) }
  280. .onChange(of: currentNightscoutSubstep) { _, _ in scrollProxy.scrollTo("top", anchor: .top) }
  281. .onChange(of: currentDeliverySubstep) { _, _ in scrollProxy.scrollTo("top", anchor: .top) }
  282. }
  283. }
  284. }
  285. struct OnboardingNavigationButtons: View {
  286. @Binding var currentStep: OnboardingStep
  287. @Binding var currentNightscoutSubstep: NightscoutSubstep
  288. @Binding var currentDeliverySubstep: DeliveryLimitSubstep
  289. @Binding var currentAutosensSubstep: AutosensSettingsSubstep
  290. @Binding var currentSMBSubstep: SMBSettingsSubstep
  291. @Binding var currentTargetBehaviorSubstep: TargetBehaviorSubstep
  292. let onboardingManager: OnboardingManager
  293. @Bindable var state: Onboarding.StateModel
  294. var shouldDisableNextButton: Bool
  295. var navigationDirectionChanged: (OnboardingNavigationDirection) -> Void
  296. var body: some View {
  297. HStack {
  298. if currentStep != .welcome {
  299. Button(action: {
  300. navigationDirectionChanged(.backward)
  301. withAnimation {
  302. handleBackNavigation()
  303. }
  304. }) {
  305. HStack {
  306. Image(systemName: "chevron.left")
  307. Text("Back")
  308. }
  309. .padding()
  310. .foregroundColor(.primary)
  311. }
  312. }
  313. Spacer()
  314. Button(action: {
  315. navigationDirectionChanged(.forward)
  316. withAnimation {
  317. handleNextNavigation()
  318. }
  319. }) {
  320. HStack {
  321. Text(currentStep == .completed ? "Get Started" : "Next")
  322. Image(systemName: "chevron.right")
  323. }
  324. .padding()
  325. .foregroundColor(.white)
  326. .background(Capsule().fill(!shouldDisableNextButton ? Color.blue : Color(.systemGray)))
  327. }
  328. .disabled(shouldDisableNextButton)
  329. }
  330. .padding(.horizontal)
  331. .padding(.bottom)
  332. }
  333. // MARK: - Navigation Logic
  334. private func handleBackNavigation() {
  335. switch currentStep {
  336. case .completed:
  337. currentStep = .targetBehavior
  338. currentTargetBehaviorSubstep = .halfBasalTarget
  339. case .nightscout:
  340. if currentNightscoutSubstep == .setupSelection,
  341. let previous = currentStep.previous
  342. {
  343. currentStep = previous
  344. currentNightscoutSubstep = .setupSelection
  345. } else {
  346. currentNightscoutSubstep = NightscoutSubstep(rawValue: currentNightscoutSubstep.rawValue - 1)!
  347. }
  348. case .deliveryLimits:
  349. if let previousSub = DeliveryLimitSubstep(rawValue: currentDeliverySubstep.rawValue - 1) {
  350. currentDeliverySubstep = previousSub
  351. } else if let previous = currentStep.previous {
  352. currentStep = previous
  353. currentDeliverySubstep = .minimumSafetyThreshold
  354. }
  355. case .algorithmSettings:
  356. if let previous = currentStep.previous {
  357. currentStep = previous
  358. currentDeliverySubstep = .minimumSafetyThreshold
  359. currentAutosensSubstep = .autosensMin
  360. }
  361. case .autosensSettings:
  362. if let previous = AutosensSettingsSubstep(rawValue: currentAutosensSubstep.rawValue - 1) {
  363. currentAutosensSubstep = previous
  364. } else if let previousStep = currentStep.previous {
  365. currentStep = previousStep
  366. currentAutosensSubstep = .autosensMin
  367. }
  368. case .smbSettings:
  369. if let previous = SMBSettingsSubstep(rawValue: currentSMBSubstep.rawValue - 1) {
  370. currentSMBSubstep = previous
  371. } else if let previousStep = currentStep.previous {
  372. currentStep = previousStep
  373. currentSMBSubstep = .enableSMBAlways
  374. currentAutosensSubstep = .rewindResetsAutosens
  375. }
  376. case .targetBehavior:
  377. if let previous = TargetBehaviorSubstep(rawValue: currentTargetBehaviorSubstep.rawValue - 1) {
  378. currentTargetBehaviorSubstep = previous
  379. } else if let previousStep = currentStep.previous {
  380. currentStep = previousStep
  381. currentTargetBehaviorSubstep = .highTempTargetRaisesSensitivity
  382. currentSMBSubstep = .maxDeltaGlucoseThreshold
  383. }
  384. default:
  385. if let previous = currentStep.previous {
  386. currentStep = previous
  387. }
  388. }
  389. }
  390. private func handleNextNavigation() {
  391. switch currentStep {
  392. case .completed:
  393. state.saveOnboardingData()
  394. onboardingManager.completeOnboarding()
  395. Foundation.NotificationCenter.default.post(name: .onboardingCompleted, object: nil)
  396. case .nightscout:
  397. if currentNightscoutSubstep != .importFromNightscout {
  398. if currentNightscoutSubstep == .setupSelection,
  399. state.nightscoutSetupOption == .skipNightscoutSetup,
  400. let next = currentStep.next
  401. {
  402. currentStep = next
  403. } else {
  404. currentNightscoutSubstep = NightscoutSubstep(rawValue: currentNightscoutSubstep.rawValue + 1)!
  405. }
  406. } else if currentNightscoutSubstep == .importFromNightscout,
  407. state.nightscoutImportOption == .useImport
  408. {
  409. Task {
  410. await state.importSettingsFromNightscout(currentStep: $currentStep)
  411. }
  412. } else if let next = currentStep.next {
  413. currentStep = next
  414. }
  415. case .deliveryLimits:
  416. if let next = DeliveryLimitSubstep(rawValue: currentDeliverySubstep.rawValue + 1) {
  417. currentDeliverySubstep = next
  418. } else if let nextStep = currentStep.next {
  419. currentStep = nextStep
  420. currentDeliverySubstep = .maxIOB
  421. }
  422. case .autosensSettings:
  423. if let next = AutosensSettingsSubstep(rawValue: currentAutosensSubstep.rawValue + 1) {
  424. currentAutosensSubstep = next
  425. } else if let nextStep = currentStep.next {
  426. currentStep = nextStep
  427. currentAutosensSubstep = .autosensMin
  428. }
  429. case .smbSettings:
  430. if let next = SMBSettingsSubstep(rawValue: currentSMBSubstep.rawValue + 1) {
  431. currentSMBSubstep = next
  432. } else if let nextStep = currentStep.next {
  433. currentStep = nextStep
  434. currentSMBSubstep = .enableSMBAlways
  435. }
  436. case .targetBehavior:
  437. if let next = TargetBehaviorSubstep(rawValue: currentTargetBehaviorSubstep.rawValue + 1) {
  438. currentTargetBehaviorSubstep = next
  439. } else if let nextStep = currentStep.next {
  440. currentStep = nextStep
  441. currentTargetBehaviorSubstep = .highTempTargetRaisesSensitivity
  442. }
  443. default:
  444. if let next = currentStep.next {
  445. currentStep = next
  446. }
  447. }
  448. }
  449. }
  450. struct Onboarding_Preview: PreviewProvider {
  451. static var previews: some View {
  452. Group {
  453. let resolver = TrioApp.resolver
  454. let onboardingManager = OnboardingManager()
  455. Onboarding.RootView(resolver: resolver, onboardingManager: onboardingManager)
  456. .previewDisplayName("Onboarding Flow")
  457. }
  458. }
  459. }