ConnectImportStepView.swift 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // LoopFollow
  2. // ConnectImportStepView.swift
  3. import SwiftUI
  4. /// The "copy from another phone" connect path: scan a QR code exported from an
  5. /// already-configured LoopFollow and apply those settings. Reuses the same
  6. /// scanner, preview, and import logic as Settings → Import/Export.
  7. struct ConnectImportStepView: View {
  8. @ObservedObject var viewModel: OnboardingViewModel
  9. @StateObject private var importVM = ImportExportSettingsViewModel()
  10. private var importSucceeded: Bool {
  11. importVM.qrCodeErrorMessage.contains("Successfully imported")
  12. }
  13. var body: some View {
  14. VStack(spacing: 0) {
  15. form
  16. OnboardingNavFooter(
  17. continueEnabled: viewModel.didImportSettings,
  18. showBack: viewModel.canGoBack,
  19. onBack: { viewModel.goBack() },
  20. onContinue: { viewModel.advance() }
  21. )
  22. }
  23. }
  24. private var form: some View {
  25. Form {
  26. Section {
  27. EmptyView()
  28. } header: {
  29. OnboardingStepHeader(
  30. systemImage: "qrcode",
  31. title: "Copy from another phone",
  32. subtitle: "On your other phone, open Settings → Nightscout (or Dexcom Share) → Import/Export and export to a QR code. Then scan it here."
  33. )
  34. .textCase(nil)
  35. .padding(.bottom, 8)
  36. }
  37. .listRowInsets(EdgeInsets())
  38. .listRowBackground(Color.clear)
  39. Section {
  40. Button {
  41. importVM.isShowingQRCodeScanner = true
  42. } label: {
  43. HStack {
  44. Image(systemName: "qrcode.viewfinder")
  45. .foregroundColor(.accentColor)
  46. Text(viewModel.didImportSettings ? "Scan a different code" : "Scan QR Code")
  47. }
  48. }
  49. }
  50. if viewModel.didImportSettings {
  51. Section {
  52. Label("Settings imported — you're connected.", systemImage: "checkmark.circle.fill")
  53. .foregroundColor(.green)
  54. }
  55. } else if !importVM.qrCodeErrorMessage.isEmpty {
  56. Section {
  57. Text(importVM.qrCodeErrorMessage)
  58. .font(.caption)
  59. .foregroundColor(.red)
  60. }
  61. }
  62. }
  63. .sheet(isPresented: $importVM.isShowingQRCodeScanner) {
  64. SimpleQRCodeScannerView { result in
  65. importVM.handleQRCodeScanResult(result)
  66. }
  67. }
  68. .sheet(isPresented: $importVM.showImportConfirmation) {
  69. ImportConfirmationView(viewModel: importVM)
  70. }
  71. .onChange(of: importVM.qrCodeErrorMessage) { message in
  72. if message.contains("Successfully imported") {
  73. viewModel.didImportSettings = true
  74. }
  75. }
  76. }
  77. }