import SnapKit import UIKit /// 拍摄订单页仅发出导航意图,由工作台协调器处理尚未搭建的下级页面。 @MainActor protocol DroneShootingOrdersViewControllerDelegate: AnyObject { /// 用户请求开始、继续、重新准备拍摄,或查看当前任务。 func shootingOrdersViewController(_ controller: DroneShootingOrdersViewController, didSelectShooting order: DroneShootingOrder) /// 用户请求查看该订单的原素材。 func shootingOrdersViewController(_ controller: DroneShootingOrdersViewController, didSelectMaterials order: DroneShootingOrder) /// 用户请求交付成片;当前阶段只提供外观及导航回调。 func shootingOrdersViewController(_ controller: DroneShootingOrdersViewController, didSelectDelivery order: DroneShootingOrder) } /// 当前用户在当前景区的拍摄订单页面;使用本地 ViewModel 展示四种履约分类。 final class DroneShootingOrdersViewController: BaseViewController { /// 由入口页协调拍摄、素材和交付导航。 weak var delegate: (any DroneShootingOrdersViewControllerDelegate)? private let viewModel: DroneShootingOrdersViewModel private let descriptionLabel = UILabel() private let filterView = DroneShootingOrderFilterView() private let refreshControl = UIRefreshControl() private let emptyView = DroneShootingOrdersEmptyView() private var collectionView: UICollectionView! private var dataSource: UICollectionViewDiffableDataSource? private var ordersByID: [String: DroneShootingOrder] = [:] /// 注入共享演示数据及初始分类;从支付成功页进入时由 ViewModel 指定待拍摄。 init(viewModel: DroneShootingOrdersViewModel) { 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() applySnapshot(animatingDifferences: false) } override func setupNavigationBar() { title = "我的拍摄订单" } override func setupUI() { view.backgroundColor = DroneDockStyle.background descriptionLabel.text = "分配给我的全部订单,含已完成订单" descriptionLabel.font = .systemFont(ofSize: 12) descriptionLabel.textColor = UIColor(hex: 0x8992A3) descriptionLabel.textAlignment = .center descriptionLabel.backgroundColor = .white view.addSubview(descriptionLabel) view.addSubview(filterView) collectionView = UICollectionView(frame: .zero, collectionViewLayout: makeLayout()) collectionView.backgroundColor = .clear collectionView.alwaysBounceVertical = true collectionView.showsVerticalScrollIndicator = false collectionView.accessibilityIdentifier = "drone.orders.list" collectionView.refreshControl = refreshControl collectionView.register(DroneShootingOrderCell.self, forCellWithReuseIdentifier: DroneShootingOrderCell.reuseIdentifier) collectionView.register( DroneShootingOrdersHeadingView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: DroneShootingOrdersHeadingView.reuseIdentifier ) collectionView.register( DroneShootingOrdersFooterView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: DroneShootingOrdersFooterView.reuseIdentifier ) view.addSubview(collectionView) configureDataSource() } override func setupConstraints() { descriptionLabel.snp.makeConstraints { make in make.top.equalTo(view.safeAreaLayoutGuide) make.leading.trailing.equalToSuperview() make.height.equalTo(32) } filterView.snp.makeConstraints { make in make.top.equalTo(descriptionLabel.snp.bottom) make.leading.trailing.equalToSuperview() make.height.equalTo(50) } collectionView.snp.makeConstraints { make in make.top.equalTo(filterView.snp.bottom) make.leading.trailing.bottom.equalToSuperview() } } override func bindActions() { filterView.onFilterSelected = { [weak self] filter in guard let self, filter != self.viewModel.selectedFilter else { return } self.viewModel.selectFilter(filter) self.applySnapshot(animatingDifferences: true) self.collectionView.setContentOffset(.zero, animated: false) } refreshControl.addTarget(self, action: #selector(refreshTriggered), for: .valueChanged) } private func makeLayout() -> UICollectionViewLayout { let item = NSCollectionLayoutItem(layoutSize: NSCollectionLayoutSize( widthDimension: .fractionalWidth(1), heightDimension: .estimated(280) )) let group = NSCollectionLayoutGroup.vertical( layoutSize: NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .estimated(280)), subitems: [item] ) let section = NSCollectionLayoutSection(group: group) section.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 16, bottom: 8, trailing: 16) section.interGroupSpacing = 12 section.boundarySupplementaryItems = [ NSCollectionLayoutBoundarySupplementaryItem( layoutSize: NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .absolute(54)), elementKind: UICollectionView.elementKindSectionHeader, alignment: .top ), NSCollectionLayoutBoundarySupplementaryItem( layoutSize: NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .absolute(48)), elementKind: UICollectionView.elementKindSectionFooter, alignment: .bottom ) ] return UICollectionViewCompositionalLayout(section: section) } private func configureDataSource() { dataSource = UICollectionViewDiffableDataSource(collectionView: collectionView) { [weak self] list, indexPath, orderID in guard let self, let order = self.ordersByID[orderID], let cell = list.dequeueReusableCell(withReuseIdentifier: DroneShootingOrderCell.reuseIdentifier, for: indexPath) as? DroneShootingOrderCell else { return nil } cell.cardView.apply(order: order) cell.cardView.onShootingTap = { [weak self] in guard let self else { return } self.delegate?.shootingOrdersViewController(self, didSelectShooting: order) } cell.cardView.onMaterialsTap = { [weak self] in guard let self else { return } self.delegate?.shootingOrdersViewController(self, didSelectMaterials: order) } cell.cardView.onDeliveryTap = { [weak self] in guard let self else { return } self.delegate?.shootingOrdersViewController(self, didSelectDelivery: order) } return cell } dataSource?.supplementaryViewProvider = { [weak self] list, kind, indexPath in guard let self else { return nil } if kind == UICollectionView.elementKindSectionHeader { let header = list.dequeueReusableSupplementaryView( ofKind: kind, withReuseIdentifier: DroneShootingOrdersHeadingView.reuseIdentifier, for: indexPath ) as? DroneShootingOrdersHeadingView header?.apply(title: self.viewModel.selectedFilter.listTitle, count: self.viewModel.orders.count) return header } let footer = list.dequeueReusableSupplementaryView( ofKind: kind, withReuseIdentifier: DroneShootingOrdersFooterView.reuseIdentifier, for: indexPath ) as? DroneShootingOrdersFooterView footer?.apply(count: self.viewModel.orders.count) return footer } } private func applySnapshot(animatingDifferences: Bool) { guard isViewLoaded, let dataSource else { return } let orders = viewModel.orders ordersByID = Dictionary(uniqueKeysWithValues: orders.map { ($0.id, $0) }) filterView.apply(selectedFilter: viewModel.selectedFilter) emptyView.apply(filter: viewModel.selectedFilter) collectionView.backgroundView = orders.isEmpty ? emptyView : nil let existingIDs = Set(dataSource.snapshot().itemIdentifiers) var snapshot = NSDiffableDataSourceSnapshot() if !orders.isEmpty { snapshot.appendSections([0]) snapshot.appendItems(orders.map(\.id)) snapshot.reconfigureItems(orders.map(\.id).filter { existingIDs.contains($0) }) } dataSource.apply(snapshot, animatingDifferences: animatingDifferences) { [weak self] in self?.updateVisibleSupplementaryViews() } } private func updateVisibleSupplementaryViews() { for case let header as DroneShootingOrdersHeadingView in collectionView.visibleSupplementaryViews(ofKind: UICollectionView.elementKindSectionHeader) { header.apply(title: viewModel.selectedFilter.listTitle, count: viewModel.orders.count) } for case let footer as DroneShootingOrdersFooterView in collectionView.visibleSupplementaryViews(ofKind: UICollectionView.elementKindSectionFooter) { footer.apply(count: viewModel.orders.count) } } @objc private func refreshTriggered() { viewModel.refresh() applySnapshot(animatingDifferences: true) refreshControl.endRefreshing() } } /// 列表上方的订单分类标题与数量。 private final class DroneShootingOrdersHeadingView: UICollectionReusableView { /// Supplementary view 复用标识。 static let reuseIdentifier = "DroneShootingOrdersHeadingView" private let titleLabel = UILabel() override init(frame: CGRect) { super.init(frame: frame) titleLabel.font = .systemFont(ofSize: 16, weight: .semibold) titleLabel.accessibilityTraits = .header addSubview(titleLabel) titleLabel.snp.makeConstraints { make in make.leading.trailing.centerY.equalToSuperview() } } @available(*, unavailable) required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } /// 分类标题和数量使用不同字重,便于快速扫视。 func apply(title: String, count: Int) { let text = NSMutableAttributedString(string: title, attributes: [.foregroundColor: UIColor(hex: 0x172033)]) text.append(NSAttributedString( string: " \(count)", attributes: [.foregroundColor: UIColor(hex: 0x8992A3), .font: UIFont.systemFont(ofSize: 14)] )) titleLabel.attributedText = text } } /// 演示订单全部展示完成后的底部文案,不伪装接口分页。 private final class DroneShootingOrdersFooterView: UICollectionReusableView { /// Supplementary view 复用标识。 static let reuseIdentifier = "DroneShootingOrdersFooterView" private let label = UILabel() override init(frame: CGRect) { super.init(frame: frame) label.font = .systemFont(ofSize: 12) label.textColor = UIColor(hex: 0xA0A6B2) label.textAlignment = .center addSubview(label) label.snp.makeConstraints { make in make.edges.equalToSuperview() } } @available(*, unavailable) required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } /// 显示当前本地快照的订单数量。 func apply(count: Int) { label.text = "已显示全部 \(count) 个订单" } } /// 没有订单时保留四个筛选入口,并给出当前分类的空状态。 private final class DroneShootingOrdersEmptyView: UIView { private let titleLabel = UILabel() override init(frame: CGRect) { super.init(frame: frame) let icon = UIImageView(image: UIImage(systemName: "doc.text.magnifyingglass")) icon.tintColor = UIColor(hex: 0xBBC4D4) icon.contentMode = .scaleAspectFit titleLabel.font = .systemFont(ofSize: 16, weight: .medium) titleLabel.textColor = UIColor(hex: 0x697487) titleLabel.textAlignment = .center let subtitle = UILabel() subtitle.text = "当前景区暂无分配给你的此类订单" subtitle.font = .systemFont(ofSize: 13) subtitle.textColor = UIColor(hex: 0xA0A8B7) subtitle.textAlignment = .center subtitle.numberOfLines = 0 addSubview(icon) addSubview(titleLabel) addSubview(subtitle) icon.snp.makeConstraints { make in make.centerX.equalToSuperview() make.centerY.equalToSuperview().offset(-65) make.size.equalTo(54) } titleLabel.snp.makeConstraints { make in make.top.equalTo(icon.snp.bottom).offset(20) make.leading.trailing.equalToSuperview().inset(24) } subtitle.snp.makeConstraints { make in make.top.equalTo(titleLabel.snp.bottom).offset(10) make.leading.trailing.equalToSuperview().inset(24) } } @available(*, unavailable) required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } /// 根据已选分类生成空列表标题。 func apply(filter: DroneShootingOrderFilter) { titleLabel.text = filter == .all ? "暂无拍摄订单" : "暂无\(filter.title)订单" } }