| 1 | import SwiftUI |
| 2 | #if canImport(UIKit) |
| 3 | import UIKit |
| 4 | #endif |
| 5 | |
| 6 | /// First run: name the team, show the code, approve in the browser. One |
| 7 | /// action per screen. The wizard's later steps (model account, first |
| 8 | /// project) stay on the web for now. |
| 9 | public struct PairingView: View { |
| 10 | @Bindable var model: EquipoAppModel |
| 11 | @State private var team = "" |
| 12 | @Environment(\.openURL) private var openURL |
| 13 | public init(model: EquipoAppModel) { self.model = model } |
| 14 | |
| 15 | public var body: some View { |
| 16 | let pairing = model.pairing |
| 17 | VStack(alignment: .leading, spacing: 20) { |
| 18 | Spacer(minLength: 12) |
| 19 | AppGlyph(size: 56) |
| 20 | switch pairing.step { |
| 21 | case .enterTeam, .failed, .expired: |
| 22 | enterTeam(pairing) |
| 23 | case .requesting: |
| 24 | title(String(localized: "pairing.title", bundle: .module), String(localized: "pairing.requesting", bundle: .module)) |
| 25 | ProgressView() |
| 26 | case .waiting(let code, let verification, _): |
| 27 | title(String(localized: "pairing.title", bundle: .module), String(localized: "pairing.approve_lede", bundle: .module)) |
| 28 | codePanel(code) |
| 29 | HStack(spacing: 10) { |
| 30 | ProgressView().controlSize(.small) |
| 31 | Text(String(localized: "pairing.waiting", bundle: .module)).font(Typo.secondary).foregroundStyle(Palette.secondary) |
| 32 | } |
| 33 | Spacer() |
| 34 | RosterButton(title: String(localized: "pairing.open_browser", bundle: .module), primary: true, expanded: true) { openURL(verification) } |
| 35 | QuietAction(title: String(localized: "pairing.cancel", bundle: .module)) { pairing.cancel() } |
| 36 | case .approved(let teamName): |
| 37 | title(String(localized: "pairing.approved_title", bundle: .module), String(localized: "pairing.approved \(teamName)", bundle: .module)) |
| 38 | Spacer() |
| 39 | RosterButton(title: String(localized: "pairing.continue", bundle: .module), primary: true, expanded: true) { model.paired() } |
| 40 | } |
| 41 | Spacer(minLength: 12) |
| 42 | } |
| 43 | .padding(24) |
| 44 | .frame(maxWidth: 520, maxHeight: .infinity, alignment: .topLeading) |
| 45 | .frame(maxWidth: .infinity) |
| 46 | .background(Palette.background.ignoresSafeArea()) |
| 47 | .tint(Palette.accent) |
| 48 | } |
| 49 | |
| 50 | @ViewBuilder private func enterTeam(_ pairing: PairingModel) -> some View { |
| 51 | title(String(localized: "pairing.welcome_title", bundle: .module), String(localized: "pairing.welcome_lede", bundle: .module)) |
| 52 | if let ended = model.endedTeam { |
| 53 | Text(String(localized: "pairing.ended \(ended)", bundle: .module)).font(Typo.secondary).foregroundStyle(Palette.warning) |
| 54 | } |
| 55 | VStack(alignment: .leading, spacing: 6) { |
| 56 | Text(String(localized: "pairing.team_label", bundle: .module)).font(Typo.captionStrong).foregroundStyle(Palette.secondary) |
| 57 | TextField(String(localized: "pairing.team_placeholder", bundle: .module), text: $team) |
| 58 | // Plain: the rounded box below is the field's only outline (the |
| 59 | // Mac's default style drew a second one inside it). |
| 60 | .textFieldStyle(.plain) |
| 61 | .textContentType(.URL) |
| 62 | #if os(iOS) |
| 63 | .keyboardType(.URL).textInputAutocapitalization(.never) |
| 64 | #endif |
| 65 | .autocorrectionDisabled() |
| 66 | .font(Typo.body) |
| 67 | .padding(.horizontal, 12).frame(minHeight: Metric.target) |
| 68 | .background(Palette.controlFace, in: RoundedRectangle(cornerRadius: Metric.control, style: .continuous)) |
| 69 | .overlay(RoundedRectangle(cornerRadius: Metric.control, style: .continuous).strokeBorder(Palette.controlBorder, lineWidth: 1)) |
| 70 | .onSubmit { start(pairing) } |
| 71 | Text(String(localized: "pairing.team_hint", bundle: .module)).font(Typo.caption).foregroundStyle(Palette.secondary) |
| 72 | } |
| 73 | if case .failed(let message) = pairing.step { |
| 74 | Text(message).font(Typo.secondary).foregroundStyle(Palette.negative) |
| 75 | } |
| 76 | if case .expired = pairing.step { |
| 77 | Text(String(localized: "pairing.expired", bundle: .module)).font(Typo.secondary).foregroundStyle(Palette.negative) |
| 78 | } |
| 79 | Spacer() |
| 80 | RosterButton(title: String(localized: "pairing.pair", bundle: .module), primary: true, expanded: true) { start(pairing) } |
| 81 | .disabled(PairingModel.origin(from: team) == nil) |
| 82 | } |
| 83 | |
| 84 | private func start(_ pairing: PairingModel) { |
| 85 | guard let origin = PairingModel.origin(from: team) else { return } |
| 86 | pairing.start(origin: origin, label: Self.deviceLabel) |
| 87 | } |
| 88 | |
| 89 | static var deviceLabel: String { |
| 90 | #if canImport(UIKit) |
| 91 | return UIDevice.current.name |
| 92 | #else |
| 93 | return Host.current().localizedName ?? String(localized: "device.mac", bundle: .module) |
| 94 | #endif |
| 95 | } |
| 96 | |
| 97 | private func title(_ title: String, _ lede: String) -> some View { |
| 98 | VStack(alignment: .leading, spacing: 8) { |
| 99 | Text(title).font(.largeTitle.weight(.bold)).foregroundStyle(Palette.text) |
| 100 | Text(lede).font(Typo.body).foregroundStyle(Palette.secondary).fixedSize(horizontal: false, vertical: true) |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | private func codePanel(_ code: String) -> some View { |
| 105 | Panel { |
| 106 | VStack(spacing: 8) { |
| 107 | Text(String(localized: "pairing.your_code", bundle: .module)).font(Typo.caption).foregroundStyle(Palette.secondary) |
| 108 | Text(code).font(.system(size: 40, weight: .semibold, design: .monospaced)).foregroundStyle(Palette.text) |
| 109 | .textSelection(.enabled).accessibilityLabel(code.map(String.init).joined(separator: " ")) |
| 110 | Text(String(localized: "pairing.shown_as \(Self.deviceLabel)", bundle: .module)).font(Typo.caption).foregroundStyle(Palette.secondary) |
| 111 | }.frame(maxWidth: .infinity).padding(.vertical, 8) |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |