import SwiftUI #if canImport(UIKit) import UIKit #endif /// First run: name the team, show the code, approve in the browser. One /// action per screen. The wizard's later steps (model account, first /// project) stay on the web for now. public struct PairingView: View { @Bindable var model: EquipoAppModel @State private var team = "" @Environment(\.openURL) private var openURL public init(model: EquipoAppModel) { self.model = model } public var body: some View { let pairing = model.pairing VStack(alignment: .leading, spacing: 20) { Spacer(minLength: 12) AppGlyph(size: 56) switch pairing.step { case .enterTeam, .failed, .expired: enterTeam(pairing) case .requesting: title(String(localized: "pairing.title", bundle: .module), String(localized: "pairing.requesting", bundle: .module)) ProgressView() case .waiting(let code, let verification, _): title(String(localized: "pairing.title", bundle: .module), String(localized: "pairing.approve_lede", bundle: .module)) codePanel(code) HStack(spacing: 10) { ProgressView().controlSize(.small) Text(String(localized: "pairing.waiting", bundle: .module)).font(Typo.secondary).foregroundStyle(Palette.secondary) } Spacer() RosterButton(title: String(localized: "pairing.open_browser", bundle: .module), primary: true, expanded: true) { openURL(verification) } QuietAction(title: String(localized: "pairing.cancel", bundle: .module)) { pairing.cancel() } case .approved(let teamName): title(String(localized: "pairing.approved_title", bundle: .module), String(localized: "pairing.approved \(teamName)", bundle: .module)) Spacer() RosterButton(title: String(localized: "pairing.continue", bundle: .module), primary: true, expanded: true) { model.paired() } } Spacer(minLength: 12) } .padding(24) .frame(maxWidth: 520, maxHeight: .infinity, alignment: .topLeading) .frame(maxWidth: .infinity) .background(Palette.background.ignoresSafeArea()) .tint(Palette.accent) } @ViewBuilder private func enterTeam(_ pairing: PairingModel) -> some View { title(String(localized: "pairing.welcome_title", bundle: .module), String(localized: "pairing.welcome_lede", bundle: .module)) if let ended = model.endedTeam { Text(String(localized: "pairing.ended \(ended)", bundle: .module)).font(Typo.secondary).foregroundStyle(Palette.warning) } VStack(alignment: .leading, spacing: 6) { Text(String(localized: "pairing.team_label", bundle: .module)).font(Typo.captionStrong).foregroundStyle(Palette.secondary) TextField(String(localized: "pairing.team_placeholder", bundle: .module), text: $team) // Plain: the rounded box below is the field's only outline (the // Mac's default style drew a second one inside it). .textFieldStyle(.plain) .textContentType(.URL) #if os(iOS) .keyboardType(.URL).textInputAutocapitalization(.never) #endif .autocorrectionDisabled() .font(Typo.body) .padding(.horizontal, 12).frame(minHeight: Metric.target) .background(Palette.controlFace, in: RoundedRectangle(cornerRadius: Metric.control, style: .continuous)) .overlay(RoundedRectangle(cornerRadius: Metric.control, style: .continuous).strokeBorder(Palette.controlBorder, lineWidth: 1)) .onSubmit { start(pairing) } Text(String(localized: "pairing.team_hint", bundle: .module)).font(Typo.caption).foregroundStyle(Palette.secondary) } if case .failed(let message) = pairing.step { Text(message).font(Typo.secondary).foregroundStyle(Palette.negative) } if case .expired = pairing.step { Text(String(localized: "pairing.expired", bundle: .module)).font(Typo.secondary).foregroundStyle(Palette.negative) } Spacer() RosterButton(title: String(localized: "pairing.pair", bundle: .module), primary: true, expanded: true) { start(pairing) } .disabled(PairingModel.origin(from: team) == nil) } private func start(_ pairing: PairingModel) { guard let origin = PairingModel.origin(from: team) else { return } pairing.start(origin: origin, label: Self.deviceLabel) } static var deviceLabel: String { #if canImport(UIKit) return UIDevice.current.name #else return Host.current().localizedName ?? String(localized: "device.mac", bundle: .module) #endif } private func title(_ title: String, _ lede: String) -> some View { VStack(alignment: .leading, spacing: 8) { Text(title).font(.largeTitle.weight(.bold)).foregroundStyle(Palette.text) Text(lede).font(Typo.body).foregroundStyle(Palette.secondary).fixedSize(horizontal: false, vertical: true) } } private func codePanel(_ code: String) -> some View { Panel { VStack(spacing: 8) { Text(String(localized: "pairing.your_code", bundle: .module)).font(Typo.caption).foregroundStyle(Palette.secondary) Text(code).font(.system(size: 40, weight: .semibold, design: .monospaced)).foregroundStyle(Palette.text) .textSelection(.enabled).accessibilityLabel(code.map(String.init).joined(separator: " ")) Text(String(localized: "pairing.shown_as \(Self.deviceLabel)", bundle: .module)).font(Typo.caption).foregroundStyle(Palette.secondary) }.frame(maxWidth: .infinity).padding(.vertical, 8) } } }