import SnapKit import UIKit /// 真实订单的交付方式弹窗,只展示方式选择并将云端交付导航交回入口。 @MainActor final class DroneDockDeliveryMethodViewController: BaseViewController { /// 用户选择云端上传后,由入口打开现有真实交付页。 var onCloudUpload: (() -> Void)? /// 用户关闭弹窗后,由入口统一结束当前模态流程。 var onClose: (() -> Void)? /// 闪传只显示未接入说明,不创建传输任务或连接设备。 private enum Phase: Equatable { case methods, nearby } /// 两种交付方式与闪传说明使用独立、稳定的列表身份。 private enum Item: Hashable { case nearby, cloud, nearbyMessage } private let customerTitle: String private let headerView = DroneDeliveryUploadHeaderView() private let footerView = DroneDeliveryUploadFooterView() private lazy var collectionView = UICollectionView(frame: .zero, collectionViewLayout: makeLayout()) private var dataSource: UICollectionViewDiffableDataSource! private var phase = Phase.methods private var hasRequestedCloud = false /// 游客标题由选中的订单传入,本弹窗不读取网络或改变订单状态。 init(customerTitle: String) { self.customerTitle = customerTitle super.init(nibName: nil, bundle: nil) modalPresentationStyle = .pageSheet } @available(*, unavailable) required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) hasRequestedCloud = false navigationController?.isModalInPresentation = false isModalInPresentation = false navigationController?.setNavigationBarHidden(true, animated: false) configureSheet() } override func setupNavigationBar() { title = "交付成片" navigationController?.setNavigationBarHidden(true, animated: false) } override func setupUI() { view.backgroundColor = .white collectionView.backgroundColor = .white collectionView.alwaysBounceVertical = false collectionView.showsVerticalScrollIndicator = false collectionView.accessibilityIdentifier = "drone.delivery.method.list" collectionView.delegate = self collectionView.register(DroneDeliveryUploadMethodCell.self, forCellWithReuseIdentifier: "method") collectionView.register(DroneDeliveryUploadMessageCell.self, forCellWithReuseIdentifier: "message") [headerView, collectionView, footerView].forEach(view.addSubview) configureDataSource() applySnapshot(animated: false) } override func setupConstraints() { headerView.snp.makeConstraints { make in make.top.equalTo(view.safeAreaLayoutGuide) make.leading.trailing.equalToSuperview() } collectionView.snp.makeConstraints { make in make.top.equalTo(headerView.snp.bottom) make.leading.trailing.equalToSuperview() make.bottom.equalTo(footerView.snp.top) } footerView.snp.makeConstraints { make in make.leading.trailing.bottom.equalToSuperview() } } override func bindActions() { headerView.onCloseTap = { [weak self] in self?.onClose?() } footerView.onPrimaryTap = { [weak self] in self?.requestCloudUpload() } footerView.onSecondaryTap = { [weak self] in self?.showPhase(.methods) } } private func configureSheet() { guard let sheet = navigationController?.sheetPresentationController ?? sheetPresentationController else { return } let identifier = UISheetPresentationController.Detent.Identifier("drone.delivery.method") sheet.detents = [ .custom(identifier: identifier) { context in min(410, context.maximumDetentValue) }, .large() ] sheet.selectedDetentIdentifier = identifier sheet.prefersGrabberVisible = true sheet.preferredCornerRadius = 22 sheet.prefersScrollingExpandsWhenScrolledToEdge = false } private func makeLayout() -> UICollectionViewLayout { UICollectionViewCompositionalLayout { [weak self] _, _ in guard let self else { return nil } let height: CGFloat = self.phase == .methods ? 88 : 132 let item = NSCollectionLayoutItem(layoutSize: .init(widthDimension: .fractionalWidth(1), heightDimension: .fractionalHeight(1))) let group = NSCollectionLayoutGroup.vertical( layoutSize: .init(widthDimension: .fractionalWidth(1), heightDimension: .absolute(height)), subitems: [item]) let section = NSCollectionLayoutSection(group: group) section.interGroupSpacing = self.phase == .methods ? 12 : 0 section.contentInsets = .init(top: 4, leading: 20, bottom: 4, trailing: 20) return section } } private func configureDataSource() { dataSource = UICollectionViewDiffableDataSource(collectionView: collectionView) { collection, path, item in switch item { case .nearby, .cloud: let cell = collection.dequeueReusableCell(withReuseIdentifier: "method", for: path) as! DroneDeliveryUploadMethodCell if item == .nearby { cell.apply(title: "面对面闪传", detail: "近距离传给游客 · 流程待确认", symbol: "wifi", identifier: "drone.delivery.method.nearby") } else { cell.apply(title: "云端上传", detail: "选择打卡点,上传后游客可查看", symbol: "icloud.and.arrow.up", identifier: "drone.delivery.method.cloud") } return cell case .nearbyMessage: let cell = collection.dequeueReusableCell(withReuseIdentifier: "message", for: path) as! DroneDeliveryUploadMessageCell cell.apply(title: "闪传流程待确认", detail: "热点连接与游客接收流程暂未接入。\n请使用云端上传交付成片。", symbol: "wifi") cell.accessibilityIdentifier = "drone.delivery.method.nearbyMessage" return cell } } } private func applySnapshot(animated: Bool) { guard dataSource != nil else { return } var snapshot = NSDiffableDataSourceSnapshot() snapshot.appendSections([0]) switch phase { case .methods: headerView.apply(title: "交付成片", customer: customerTitle, detail: "请选择本次交付方式") footerView.apply(primaryTitle: nil, enabled: false, identifier: "", detail: "可分批交付多个作品,不影响拍摄次数", secondaryTitle: nil) snapshot.appendItems([.nearby, .cloud]) case .nearby: headerView.apply(title: "面对面闪传", customer: customerTitle, detail: "本次仅展示交付入口") footerView.apply(primaryTitle: "改用云端上传", enabled: true, identifier: "drone.delivery.method.useCloud", detail: "", secondaryTitle: "返回交付方式") snapshot.appendItems([.nearbyMessage]) } dataSource.apply(snapshot, animatingDifferences: animated) collectionView.collectionViewLayout.invalidateLayout() } private func showPhase(_ phase: Phase) { self.phase = phase applySnapshot(animated: true) collectionView.setContentOffset(.zero, animated: false) } private func requestCloudUpload() { guard !hasRequestedCloud, let onCloudUpload else { return } hasRequestedCloud = true onCloudUpload() } } extension DroneDockDeliveryMethodViewController: UICollectionViewDelegate { /// 云端方式交回调用方,闪传方式只在当前弹窗展示待接入说明。 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { guard let item = dataSource.itemIdentifier(for: indexPath), !hasRequestedCloud else { return } switch item { case .nearby: showPhase(.nearby) case .cloud: requestCloudUpload() case .nearbyMessage: break } } }