import SwiftUI import UniformTypeIdentifiers struct HomeView: View { /// 同一个 view 上挂多个 .fileImporter 会互相覆盖,只能有一个, /// 所以用这个枚举区分「选完文件之后要干什么」。 private enum PickPurpose: Equatable { case peerSend case uploadToPC(PairingPayload) } @StateObject private var model = TransferModel() @State private var pickPurpose: PickPurpose? var body: some View { NavigationStack { content .navigationTitle("QFT 快传") .navigationBarTitleDisplayMode(.inline) } .fullScreenCover(isPresented: isScanning) { ScannerView(onFound: { model.handleScanned($0) }, onCancel: { model.cancel() }) .ignoresSafeArea() } .fileImporter(isPresented: isPicking, allowedContentTypes: [UTType.item], allowsMultipleSelection: false) { result in let purpose = pickPurpose pickPurpose = nil guard case .success(let urls) = result, let url = urls.first else { model.reset() return } switch purpose { case .peerSend: model.startPeerSend(fileURL: url) case .uploadToPC(let pairing): model.startUpload(pairing, fileURL: url) case nil: break } } .onAppear { model.refreshReceivedFiles() } .onChange(of: model.stage) { _, stage in // 扫到 QFTPAIR2 之后才知道要选文件,所以选择器由 stage 驱动 if case .pickFileForUpload(let pairing) = stage { pickPurpose = .uploadToPC(pairing) } } } private var isScanning: Binding { Binding(get: { model.stage == .scanning }, set: { if !$0 { model.cancel() } }) } private var isPicking: Binding { Binding(get: { pickPurpose != nil }, set: { if !$0 { pickPurpose = nil } }) } @ViewBuilder private var content: some View { switch model.stage { case .idle, .scanning, .pickFileForUpload: landing case .connecting(let message): StatusCard(icon: "antenna.radiowaves.left.and.right", title: message) { ProgressView() Button("取消", role: .cancel) { model.cancel() } } case .running(let message): StatusCard(icon: "arrow.left.arrow.right", title: message) { if let progress = model.progress { ProgressView(value: progress.fraction) Text("\(progress.readableSize) · \(progress.readableRate)") .font(.footnote.monospacedDigit()) .foregroundStyle(.secondary) } else { ProgressView() } Button("取消", role: .cancel) { model.cancel() } } case .showingPeerCode(let pairing): peerCode(pairing) case .finished(let message, let url): StatusCard(icon: "checkmark.circle.fill", title: message) { if let url { Text(url.lastPathComponent).font(.footnote).foregroundStyle(.secondary) ShareLink(item: url) { Label("分享", systemImage: "square.and.arrow.up") } } Button("完成") { model.reset() }.buttonStyle(.borderedProminent) } case .failed(let message): StatusCard(icon: "exclamationmark.triangle.fill", title: "传输失败") { Text(message).font(.footnote).multilineTextAlignment(.center) .foregroundStyle(.secondary) Button("返回") { model.reset() }.buttonStyle(.borderedProminent) } } } private var landing: some View { List { Section { ActionRow(icon: "qrcode.viewfinder", title: "扫码", detail: "扫电脑上的二维码收发文件,或扫另一台 iPhone 的码接收") { model.beginScan() } ActionRow(icon: "iphone.gen3.radiowaves.left.and.right", title: "发给附近的 iPhone", detail: "选文件后显示二维码,对方扫一下即可,无需连同一个 Wi-Fi") { pickPurpose = .peerSend } } footer: { Text("与电脑互传需要手机和电脑连在同一个 Wi-Fi。iPhone 之间使用点对点直连,不经过路由器。") } if !model.receivedFiles.isEmpty { Section("已接收") { ForEach(model.receivedFiles, id: \.self) { url in ShareLink(item: url) { Label(url.lastPathComponent, systemImage: "doc") .lineLimit(1) } } } } } } private func peerCode(_ pairing: PeerPairing) -> some View { VStack(spacing: 20) { Text("让对方用 QFT 扫这个码") .font(.headline) if let image = QRRenderer.image(from: pairing.encode()) { Image(uiImage: image) .interpolation(.none) .resizable() .scaledToFit() .frame(maxWidth: 300) .padding() .background(.white) .clipShape(RoundedRectangle(cornerRadius: 16)) } VStack(spacing: 4) { Text(pairing.fileName).font(.subheadline).lineLimit(1) Text(TransferProgress.readable(pairing.fileSize)) .font(.footnote).foregroundStyle(.secondary) } if let progress = model.progress { ProgressView(value: progress.fraction) .padding(.horizontal, 40) Text("\(progress.readableSize) · \(progress.readableRate)") .font(.footnote.monospacedDigit()).foregroundStyle(.secondary) } else { Label("等待对方扫码…", systemImage: "hourglass") .font(.footnote).foregroundStyle(.secondary) } Button("取消", role: .cancel) { model.cancel() } } .padding() } } private struct ActionRow: View { let icon: String let title: String let detail: String let action: () -> Void var body: some View { Button(action: action) { HStack(spacing: 14) { Image(systemName: icon) .font(.title2) .frame(width: 34) VStack(alignment: .leading, spacing: 3) { Text(title).font(.body.weight(.medium)) Text(detail).font(.caption).foregroundStyle(.secondary) } } .padding(.vertical, 4) } .buttonStyle(.plain) } } private struct StatusCard: View { let icon: String let title: String @ViewBuilder let content: Content var body: some View { VStack(spacing: 18) { Image(systemName: icon).font(.system(size: 44)) Text(title).font(.headline).multilineTextAlignment(.center) content } .padding(32) .frame(maxWidth: .infinity, maxHeight: .infinity) } }