import SnapKit import UIKit /// 准备页只提出查看任务的导航请求,导航栈由机巢入口统一维护。 @MainActor protocol DroneShootingPreparationViewControllerDelegate: AnyObject { /// 召唤已明确确认,或工作人员主动查看本订单已有任务。 func shootingPreparationViewController(_ controller: DroneShootingPreparationViewController, didRequestTaskFor orderID: String) } /// 拍摄准备页:选择点位与航线,展示订单额度和设备执行许可。 @MainActor final class DroneShootingPreparationViewController: BaseViewController { /// 由入口注入的共享订单与准备状态。 let viewModel: DroneShootingPreparationViewModel /// 将任务导航交回机巢入口,避免页面自行创建任务控制器。 weak var delegate: DroneShootingPreparationViewControllerDelegate? /// 点位使用横向分区,航线使用纵向分区。 private enum Section: Int, CaseIterable { case order, points, routes } /// 稳定标识让选择和许可刷新时保留点位的横向滚动位置。 private enum Item: Hashable { case order, point(String), route(String), emptyPoints, emptyRoutes } private lazy var collectionView = UICollectionView(frame: .zero, collectionViewLayout: makeLayout()) private var dataSource: UICollectionViewDiffableDataSource! private let footerView = DronePreparationFooterView() private let refreshControl = UIRefreshControl() /// 准备页与订单页、任务页读取同一个本地状态源。 init(viewModel: DroneShootingPreparationViewModel) { self.viewModel = viewModel super.init(nibName: nil, bundle: nil) hidesBottomBarWhenPushed = true } @available(*, unavailable) required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } /// 返回页面时刷新配额、选择与设备状态,不提交任何飞行操作。 override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) viewModel.refresh() if dataSource != nil { applySnapshot(animated: false) } } override func setupNavigationBar() { title = "拍摄准备" navigationItem.backButtonDisplayMode = .minimal #if DEBUG let scenarios = DroneShootingPreparationScenario.allCases.map { scenario in UIAction(title: scenario.title) { [weak self] _ in guard let self else { return } self.viewModel.applyScenario(scenario) self.applySnapshot(animated: true) } } let confirmAction = UIAction(title: "模拟召唤确认", image: UIImage(systemName: "checkmark.circle")) { [weak self] _ in guard let self else { return } let confirmed = self.viewModel.confirmSummon() self.applySnapshot(animated: true) if confirmed { self.delegate?.shootingPreparationViewController(self, didRequestTaskFor: self.viewModel.orderID) } } let menu = UIMenu(title: "拍摄准备验收状态", children: [ UIMenu(options: .displayInline, children: scenarios), UIMenu(options: .displayInline, children: [confirmAction]) ]) let item = UIBarButtonItem(image: UIImage(systemName: "slider.horizontal.3"), menu: menu) item.accessibilityLabel = "拍摄准备验收状态" item.accessibilityIdentifier = "drone.preparation.scenarios" navigationItem.rightBarButtonItem = item #endif } override func setupUI() { view.backgroundColor = DroneDockStyle.background collectionView.backgroundColor = .white collectionView.alwaysBounceVertical = true collectionView.showsVerticalScrollIndicator = false collectionView.delegate = self collectionView.accessibilityIdentifier = "drone.preparation.list" collectionView.register(DronePreparationOrderCell.self, forCellWithReuseIdentifier: "order") collectionView.register(DronePreparationPointCell.self, forCellWithReuseIdentifier: "point") collectionView.register(DronePreparationRouteCell.self, forCellWithReuseIdentifier: "route") collectionView.register(DronePreparationEmptyCell.self, forCellWithReuseIdentifier: "empty") collectionView.register(DronePreparationHeadingView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "heading") refreshControl.tintColor = DroneDockStyle.primary collectionView.refreshControl = refreshControl view.addSubview(collectionView) view.addSubview(footerView) configureDataSource() applySnapshot(animated: false) } override func setupConstraints() { collectionView.snp.makeConstraints { make in make.top.equalTo(view.safeAreaLayoutGuide) make.leading.trailing.equalToSuperview() make.bottom.equalTo(footerView.snp.top) } footerView.snp.makeConstraints { make in make.leading.trailing.bottom.equalToSuperview() } } override func bindActions() { refreshControl.addTarget(self, action: #selector(refreshTriggered), for: .valueChanged) footerView.onPrimaryTap = { [weak self] in guard let self, self.viewModel.primaryButtonEnabled else { return } let shouldShowTask = self.viewModel.summon() self.applySnapshot(animated: true) if shouldShowTask { self.delegate?.shootingPreparationViewController(self, didRequestTaskFor: self.viewModel.orderID) } } footerView.onRefreshTap = { [weak self] in guard let self else { return } self.viewModel.refreshSummonStatus() self.applySnapshot(animated: true) } } private func makeLayout() -> UICollectionViewLayout { UICollectionViewCompositionalLayout { [weak self] sectionIndex, _ in guard let self, let section = Section(rawValue: sectionIndex) else { return nil } let height: NSCollectionLayoutDimension let width: NSCollectionLayoutDimension switch section { case .order: width = .fractionalWidth(1) height = .estimated(90) case .points: width = self.viewModel.points.isEmpty ? .fractionalWidth(1) : .absolute(144) height = self.viewModel.points.isEmpty ? .absolute(124) : .absolute(80) case .routes: width = .fractionalWidth(1) height = self.viewModel.routes.isEmpty ? .absolute(200) : .absolute(64) } let item = NSCollectionLayoutItem(layoutSize: NSCollectionLayoutSize( widthDimension: .fractionalWidth(1), heightDimension: section == .order ? .estimated(90) : .fractionalHeight(1) )) let group = NSCollectionLayoutGroup.vertical( layoutSize: NSCollectionLayoutSize(widthDimension: width, heightDimension: height), subitems: [item] ) let layoutSection = NSCollectionLayoutSection(group: group) layoutSection.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 16, bottom: section == .routes ? 16 : 0, trailing: 16) if section == .points { layoutSection.orthogonalScrollingBehavior = self.viewModel.points.isEmpty ? .none : .continuous layoutSection.interGroupSpacing = 8 } if section != .order { let headerHeight: CGFloat = section == .points ? 46 : 64 layoutSection.boundarySupplementaryItems = [NSCollectionLayoutBoundarySupplementaryItem( layoutSize: NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .absolute(headerHeight)), elementKind: UICollectionView.elementKindSectionHeader, alignment: .top )] } return layoutSection } } private func configureDataSource() { dataSource = UICollectionViewDiffableDataSource(collectionView: collectionView) { [weak self] list, indexPath, item in guard let self else { return nil } switch item { case .order: let cell = list.dequeueReusableCell(withReuseIdentifier: "order", for: indexPath) as! DronePreparationOrderCell cell.apply(order: self.viewModel.order) return cell case .point(let id): let cell = list.dequeueReusableCell(withReuseIdentifier: "point", for: indexPath) as! DronePreparationPointCell if let point = self.viewModel.points.first(where: { $0.id == id }) { cell.apply(point: point, selected: self.viewModel.selectedPoint?.id == id) } return cell case .route(let id): let cell = list.dequeueReusableCell(withReuseIdentifier: "route", for: indexPath) as! DronePreparationRouteCell if let route = self.viewModel.routes.first(where: { $0.id == id }) { cell.apply(route: route, selected: self.viewModel.selectedRoute?.id == id) } return cell case .emptyPoints, .emptyRoutes: let cell = list.dequeueReusableCell(withReuseIdentifier: "empty", for: indexPath) as! DronePreparationEmptyCell cell.apply(isPointEmpty: item == .emptyPoints) return cell } } dataSource.supplementaryViewProvider = { [weak self] list, kind, indexPath in guard let self, kind == UICollectionView.elementKindSectionHeader else { return nil } let header = list.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "heading", for: indexPath) as! DronePreparationHeadingView self.applyHeading(header, section: Section(rawValue: indexPath.section)) return header } } private func applySnapshot(animated: Bool) { guard isViewLoaded, dataSource != nil else { return } let previous = dataSource.snapshot() let existingItems = Set(previous.itemIdentifiers) var snapshot = NSDiffableDataSourceSnapshot() snapshot.appendSections(Section.allCases) snapshot.appendItems([.order], toSection: .order) snapshot.appendItems(viewModel.points.isEmpty ? [.emptyPoints] : viewModel.points.map { .point($0.id) }, toSection: .points) snapshot.appendItems(viewModel.routes.isEmpty ? [.emptyRoutes] : viewModel.routes.map { .route($0.id) }, toSection: .routes) snapshot.reconfigureItems(snapshot.itemIdentifiers.filter { existingItems.contains($0) }) footerView.apply(viewModel: viewModel) dataSource.apply(snapshot, animatingDifferences: animated) { [weak self] in guard let self else { return } let kind = UICollectionView.elementKindSectionHeader for indexPath in self.collectionView.indexPathsForVisibleSupplementaryElements(ofKind: kind) { guard let header = self.collectionView.supplementaryView(forElementKind: kind, at: indexPath) as? DronePreparationHeadingView else { continue } self.applyHeading(header, section: Section(rawValue: indexPath.section)) } } let pointEmptinessChanged = previous.itemIdentifiers.contains(.emptyPoints) != snapshot.itemIdentifiers.contains(.emptyPoints) let routeEmptinessChanged = previous.itemIdentifiers.contains(.emptyRoutes) != snapshot.itemIdentifiers.contains(.emptyRoutes) if pointEmptinessChanged || routeEmptinessChanged { collectionView.collectionViewLayout.invalidateLayout() } } private func applyHeading(_ header: DronePreparationHeadingView, section: Section?) { switch section { case .points: header.apply(title: "拍摄点", detail: nil) case .routes: let title = viewModel.selectedPoint.map { "\($0.name)的航线" } ?? "拍摄航线" header.apply(title: title, detail: "共 \(viewModel.routes.count) 条") default: header.apply(title: "", detail: nil) } } @objc private func refreshTriggered() { viewModel.refresh() applySnapshot(animated: true) refreshControl.endRefreshing() } } extension DroneShootingPreparationViewController: UICollectionViewDelegate { /// 召唤处理中保留滚动浏览,但冻结已提交的点位与航线选择。 func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool { !viewModel.hasPendingSummon } /// 整卡点位与整行航线均可选择;实际有效性和选择恢复由 ViewModel 处理。 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { guard !viewModel.hasPendingSummon, let item = dataSource.itemIdentifier(for: indexPath) else { return } switch item { case .point(let id): viewModel.selectPoint(id: id) case .route(let id): viewModel.selectRoute(id: id) default: return } applySnapshot(animated: true) } }