import SnapKit import UIKit /// 机巢工作台与拍摄订单共用的订单卡片,展示次数、任务状态与素材入口。 final class DroneShootingOrderCardView: UIView { /// 用户点击开始、继续或查看当前任务时触发。 var onShootingTap: (() -> Void)? /// 用户点击素材入口时触发。 var onMaterialsTap: (() -> Void)? /// 用户点击交付入口时触发;具体交付业务由外部协调。 var onDeliveryTap: (() -> Void)? /// 用户点击整单游客确认或历史记录摘要时触发,与追加上传分开。 var onConfirmationTap: (() -> Void)? private let contentStack = UIStackView() private let customerLabel = UILabel() private let statusContainer = UIView() private let statusLabel = UILabel() private let metadataLabel = UILabel() private let packageLabel = UILabel() private let countsLabel = UILabel() private let remainingLabel = UILabel() private let progressView = UIProgressView(progressViewStyle: .bar) private let materialRow = UIStackView() private let materialControl = UIControl() private let materialTitleLabel = UILabel() private let materialStateLabel = UILabel() private let materialChevron = UIImageView(image: UIImage(systemName: "chevron.right")) private let deliveryControl = UIControl() private let deliveryTitleLabel = UILabel() private let deliveryStateLabel = UILabel() private let deliveryChevron = UIImageView(image: UIImage(systemName: "chevron.right")) private let actionStack = UIStackView() private let secondaryButton = UIButton(type: .system) private let primaryButton = UIButton(type: .system) private var displayedOrder: DroneShootingOrder? private var displayedHomeOrder: DroneDockHomeOrder? /// 创建可以按内容自行撑高的白色圆角订单卡。 override init(frame: CGRect) { super.init(frame: frame) setupUI() } @available(*, unavailable) required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } /// 应用完整订单快照;卡片不推断或修改订单及任务状态。 func apply(order: DroneShootingOrder) { displayedOrder = order displayedHomeOrder = nil materialControl.isEnabled = true materialControl.accessibilityIdentifier = nil deliveryControl.isEnabled = true deliveryChevron.isHidden = false actionStack.isHidden = false primaryButton.isHidden = false primaryButton.isEnabled = true primaryButton.backgroundColor = DroneDockStyle.primary secondaryButton.isHidden = false secondaryButton.isEnabled = true customerLabel.text = order.customerName metadataLabel.text = "\(order.orderNumber) · \(order.createdAtText)" packageLabel.text = order.packageSummary statusLabel.text = order.statusTitle countsLabel.attributedText = countsText(order: order) remainingLabel.attributedText = remainingText(order: order) progressView.setProgress(Float(order.progress), animated: false) progressView.accessibilityLabel = "已拍摄 \(order.successfulShootingCount) 次,共 \(order.totalShootingCount) 次" applyStatusAppearance(tone: order.statusTone) applyMaterialSummary(order: order) applyActions(order: order) accessibilityIdentifier = "drone.order.\(order.id)" } /// 展示张工订单摘要中的真实字段和操作权限,不推导飞行任务或交付完成。 func apply(homeOrder order: DroneDockHomeOrder) { displayedOrder = nil displayedHomeOrder = order let identifier = "drone.orders.order.\(order.orderID)" customerLabel.text = order.touristTitle let createdAt = DroneDockHomeMetric.usableText(order.createdAt) ?? "创建时间待确认" metadataLabel.text = "订单号 \(order.orderNumberText) · \(createdAt)" packageLabel.text = order.packageTitle statusLabel.text = order.statusTitle let tone: DroneDockStatusTone = switch order.shootStatus { case "pending": .blue case "processing": .orange case "completed": .green default: .gray } applyStatusAppearance(tone: tone) let used = order.shotUsed.map(String.init) ?? "—" let total = order.shotTotal.map(String.init) ?? "—" let remaining = order.shotRemain.map(String.init) ?? "—" countsLabel.attributedText = countText(prefix: "已拍摄 ", value: used, suffix: " / \(total) 次") remainingLabel.attributedText = countText(prefix: "剩余 ", value: remaining, suffix: " 次") if let used = order.shotUsed, let total = order.shotTotal, total > 0 { progressView.setProgress(Float(min(Double(used) / Double(total), 1)), animated: false) progressView.accessibilityLabel = "已拍摄 \(used) 次,共 \(total) 次" } else { progressView.setProgress(0, animated: false) progressView.accessibilityLabel = "拍摄进度待确认" } applyRemoteSummary(order: order, identifier: identifier) applyRemoteActions(order: order, identifier: identifier) accessibilityIdentifier = identifier } /// 清除复用卡片中的订单及所有动作,避免点击到上一笔订单。 func resetForReuse() { displayedOrder = nil displayedHomeOrder = nil onShootingTap = nil onMaterialsTap = nil onDeliveryTap = nil onConfirmationTap = nil } private func setupUI() { DroneDockStyle.applyCard(to: self) contentStack.axis = .vertical contentStack.spacing = 15 addSubview(contentStack) contentStack.snp.makeConstraints { make in make.edges.equalToSuperview().inset(16) } customerLabel.font = .systemFont(ofSize: 18, weight: .bold) customerLabel.textColor = UIColor(hex: 0x172033) customerLabel.adjustsFontSizeToFitWidth = true customerLabel.minimumScaleFactor = 0.85 customerLabel.setContentHuggingPriority(.defaultLow, for: .horizontal) customerLabel.setContentCompressionResistancePriority(.defaultLow, for: .horizontal) customerLabel.accessibilityTraits = .header statusLabel.font = .systemFont(ofSize: 11, weight: .medium) statusLabel.setContentHuggingPriority(.required, for: .horizontal) statusLabel.setContentCompressionResistancePriority(.required, for: .horizontal) statusContainer.layer.cornerRadius = 6 statusContainer.addSubview(statusLabel) statusLabel.snp.makeConstraints { make in make.edges.equalToSuperview().inset(UIEdgeInsets(top: 5, left: 8, bottom: 5, right: 8)) } let header = UIStackView(arrangedSubviews: [customerLabel, statusContainer]) header.axis = .horizontal header.alignment = .center header.spacing = 8 contentStack.addArrangedSubview(header) metadataLabel.font = .systemFont(ofSize: 11) metadataLabel.textColor = UIColor(hex: 0x8992A3) metadataLabel.numberOfLines = 2 contentStack.addArrangedSubview(metadataLabel) contentStack.setCustomSpacing(17, after: metadataLabel) packageLabel.font = .systemFont(ofSize: 14) packageLabel.textColor = UIColor(hex: 0x474D59) packageLabel.numberOfLines = 0 contentStack.addArrangedSubview(packageLabel) countsLabel.font = .systemFont(ofSize: 12) remainingLabel.font = .systemFont(ofSize: 12) remainingLabel.setContentHuggingPriority(.required, for: .horizontal) let countsRow = UIStackView(arrangedSubviews: [countsLabel, remainingLabel]) countsRow.axis = .horizontal countsRow.distribution = .equalSpacing countsRow.spacing = 8 let progressStack = UIStackView(arrangedSubviews: [countsRow, progressView]) progressStack.axis = .vertical progressStack.spacing = 9 progressView.tintColor = DroneDockStyle.primary progressView.trackTintColor = UIColor(hex: 0xE9EBF2) progressView.layer.cornerRadius = 2 progressView.clipsToBounds = true progressView.snp.makeConstraints { make in make.height.equalTo(4) } contentStack.addArrangedSubview(progressStack) setupSummaryControl( materialControl, titleLabel: materialTitleLabel, stateLabel: materialStateLabel, chevron: materialChevron ) setupSummaryControl( deliveryControl, titleLabel: deliveryTitleLabel, stateLabel: deliveryStateLabel, chevron: deliveryChevron ) materialControl.addTarget(self, action: #selector(materialsTapped), for: .touchUpInside) deliveryControl.addTarget(self, action: #selector(confirmationTapped), for: .touchUpInside) materialRow.axis = .horizontal materialRow.spacing = 12 materialRow.distribution = .fillEqually materialRow.addArrangedSubview(materialControl) materialRow.addArrangedSubview(deliveryControl) materialRow.snp.makeConstraints { make in // 隐藏摘要行时,允许 UIStackView 的零高度约束覆盖该展示高度。 make.height.equalTo(40).priority(999) } contentStack.addArrangedSubview(materialRow) actionStack.axis = .horizontal actionStack.spacing = 10 actionStack.distribution = .fillEqually actionStack.addArrangedSubview(secondaryButton) actionStack.addArrangedSubview(primaryButton) [secondaryButton, primaryButton].forEach { button in button.layer.cornerRadius = 10 button.layer.cornerCurve = .continuous button.titleLabel?.font = .systemFont(ofSize: 14, weight: .semibold) button.titleLabel?.adjustsFontSizeToFitWidth = true button.titleLabel?.minimumScaleFactor = 0.85 button.snp.makeConstraints { make in make.height.equalTo(44).priority(999) } } secondaryButton.backgroundColor = UIColor(hex: 0xECF3FF) secondaryButton.setTitleColor(DroneDockStyle.primary, for: .normal) primaryButton.backgroundColor = DroneDockStyle.primary primaryButton.setTitleColor(.white, for: .normal) secondaryButton.addTarget(self, action: #selector(secondaryTapped), for: .touchUpInside) primaryButton.addTarget(self, action: #selector(primaryTapped), for: .touchUpInside) contentStack.addArrangedSubview(actionStack) } private func setupSummaryControl( _ control: UIControl, titleLabel: UILabel, stateLabel: UILabel, chevron: UIImageView ) { titleLabel.font = .systemFont(ofSize: 13, weight: .semibold) titleLabel.textColor = UIColor(hex: 0x232936) titleLabel.setContentCompressionResistancePriority(.defaultLow, for: .horizontal) stateLabel.font = .systemFont(ofSize: 11) stateLabel.textColor = UIColor(hex: 0x8992A3) stateLabel.numberOfLines = 1 chevron.tintColor = UIColor(hex: 0x5A79B5) chevron.contentMode = .scaleAspectFit control.addSubview(titleLabel) control.addSubview(stateLabel) control.addSubview(chevron) titleLabel.snp.makeConstraints { make in make.top.leading.equalToSuperview() make.trailing.lessThanOrEqualTo(chevron.snp.leading).offset(-5) } chevron.snp.makeConstraints { make in make.leading.equalTo(titleLabel.snp.trailing).offset(6) make.centerY.equalTo(titleLabel) make.width.equalTo(6) make.height.equalTo(10) make.trailing.lessThanOrEqualToSuperview() } stateLabel.snp.makeConstraints { make in make.leading.trailing.equalToSuperview() make.top.equalTo(titleLabel.snp.bottom).offset(6) } control.isAccessibilityElement = true control.accessibilityTraits = .button } private func applyStatusAppearance(tone: DroneDockStatusTone) { let foreground: UIColor let background: UIColor switch tone { case .blue: foreground = DroneDockStyle.primary background = UIColor(hex: 0xECF3FF) case .green: foreground = UIColor(hex: 0x169B6B) background = UIColor(hex: 0xEDF8F3) case .gray: foreground = UIColor(hex: 0x8E97A6) background = UIColor(hex: 0xF0F2F7) case .orange: foreground = UIColor(hex: 0xD68A28) background = UIColor(hex: 0xFFF6E8) case .red: foreground = UIColor(hex: 0xCA5E57) background = UIColor(hex: 0xFFF0ED) } statusLabel.textColor = foreground statusContainer.backgroundColor = background } private func applyRemoteSummary(order: DroneDockHomeOrder, identifier: String) { materialRow.isHidden = false materialControl.isHidden = false materialControl.isEnabled = true materialTitleLabel.text = "查看素材" materialStateLabel.text = order.mediaStatusTitle ?? "素材状态待确认" materialStateLabel.textColor = order.materialStatus == 3 ? UIColor(hex: 0xC26962) : UIColor(hex: 0x8992A3) materialChevron.isHidden = false materialControl.accessibilityIdentifier = identifier + ".materials" materialControl.accessibilityLabel = "查看素材,\(materialStateLabel.text ?? "")" deliveryControl.isHidden = !order.hasCompletedShootingQuota deliveryControl.isEnabled = order.hasCompletedShootingQuota && (order.canConfirm == true || order.orderStatus == 30 || order.confirmStatus == 2) deliveryChevron.isHidden = !deliveryControl.isEnabled let confirmation: String = switch order.confirmStatus { case 0: "未发起确认" case 1: "待游客确认" case 2: "已确认完成" default: "确认状态待确认" } deliveryTitleLabel.text = order.orderStatus == 30 || order.confirmStatus == 2 ? "查看记录" : order.canConfirm == true ? "邀请确认" : confirmation deliveryStateLabel.text = "已交付 \(order.filmCount.map(String.init) ?? "—") 个视频" deliveryControl.accessibilityIdentifier = identifier + ".confirmation" deliveryControl.accessibilityLabel = "\(deliveryTitleLabel.text ?? ""),\(deliveryStateLabel.text ?? ""),\(confirmation)" } private func applyRemoteActions(order: DroneDockHomeOrder, identifier: String) { let canShoot = order.canCreateTask == true let canConfirm = order.canInviteVisitorConfirmation let canDeliver = order.canUploadFilm == true primaryButton.isHidden = !canShoot && !canConfirm && !canDeliver primaryButton.isEnabled = !primaryButton.isHidden primaryButton.backgroundColor = DroneDockStyle.primary primaryButton.setTitle(canShoot ? "开始拍摄" : canConfirm ? "邀请确认" : "交付成片", for: .normal) primaryButton.accessibilityIdentifier = identifier + ".primary" secondaryButton.isHidden = !canDeliver || (!canShoot && !canConfirm) secondaryButton.isEnabled = !secondaryButton.isHidden secondaryButton.setTitle("交付成片", for: .normal) secondaryButton.accessibilityIdentifier = identifier + ".delivery" actionStack.isHidden = primaryButton.isHidden && secondaryButton.isHidden } private func applyMaterialSummary(order: DroneShootingOrder) { let showsDelivery = order.hasCompletedShootingQuota && order.deliveredVideoCount > 0 materialRow.isHidden = !order.hasMaterials && !showsDelivery materialControl.isHidden = !order.hasMaterials let isCompleted = order.fulfillmentState == .completed && order.activeTask == nil materialTitleLabel.text = isCompleted ? "拍摄素材" : "查看素材" materialChevron.isHidden = isCompleted materialStateLabel.text = order.materialState.title materialStateLabel.textColor = order.materialState == .failed ? UIColor(hex: 0xC26962) : UIColor(hex: 0x8992A3) materialControl.accessibilityLabel = "\(materialTitleLabel.text ?? "查看素材"),\(order.materialState.title)" deliveryControl.isHidden = !showsDelivery deliveryControl.isEnabled = showsDelivery deliveryTitleLabel.text = order.visitorConfirmationState == .confirmed ? "查看记录" : "请游客确认" deliveryStateLabel.text = "已交付 \(order.deliveredVideoCount) 个视频" deliveryControl.accessibilityLabel = "\(deliveryTitleLabel.text ?? "交付成片"),\(deliveryStateLabel.text ?? "")" deliveryControl.accessibilityIdentifier = "drone.order.\(order.id).confirmation" } private func applyActions(order: DroneShootingOrder) { let isFinished = order.primaryAction == .none secondaryButton.setTitle(isFinished ? "查看素材" : "交付成片", for: .normal) if order.primaryAction == .none { primaryButton.setTitle("交付成片", for: .normal) } else { primaryButton.setTitle(order.primaryAction.title, for: .normal) } secondaryButton.accessibilityIdentifier = "drone.order.\(order.id).secondary" primaryButton.accessibilityIdentifier = "drone.order.\(order.id).primary" } private func countsText(order: DroneShootingOrder) -> NSAttributedString { let text = NSMutableAttributedString( string: "已拍摄 ", attributes: [.foregroundColor: UIColor(hex: 0x69717F)] ) text.append(NSAttributedString( string: "\(order.successfulShootingCount)", attributes: [.foregroundColor: DroneDockStyle.primary, .font: UIFont.systemFont(ofSize: 12, weight: .medium)] )) text.append(NSAttributedString( string: " / \(order.totalShootingCount) 次", attributes: [.foregroundColor: UIColor(hex: 0x69717F)] )) return text } private func remainingText(order: DroneShootingOrder) -> NSAttributedString { let text = NSMutableAttributedString( string: "剩余 ", attributes: [.foregroundColor: UIColor(hex: 0x69717F)] ) text.append(NSAttributedString( string: "\(order.remainingShootingCount)", attributes: [.foregroundColor: DroneDockStyle.primary, .font: UIFont.systemFont(ofSize: 12, weight: .medium)] )) text.append(NSAttributedString(string: " 次", attributes: [.foregroundColor: UIColor(hex: 0x69717F)])) return text } private func countText(prefix: String, value: String, suffix: String) -> NSAttributedString { let attributes: [NSAttributedString.Key: Any] = [.foregroundColor: UIColor(hex: 0x69717F)] let text = NSMutableAttributedString(string: prefix, attributes: attributes) text.append(NSAttributedString(string: value, attributes: [ .foregroundColor: DroneDockStyle.primary, .font: UIFont.systemFont(ofSize: 12, weight: .medium) ])) text.append(NSAttributedString(string: suffix, attributes: attributes)) return text } @objc private func secondaryTapped() { if let order = displayedHomeOrder { guard !secondaryButton.isHidden, secondaryButton.isEnabled, order.canUploadFilm == true else { return } onDeliveryTap?() return } guard let displayedOrder else { return } if displayedOrder.primaryAction == .none { onMaterialsTap?() } else { onDeliveryTap?() } } @objc private func primaryTapped() { if let order = displayedHomeOrder { guard !primaryButton.isHidden, primaryButton.isEnabled else { return } if order.canCreateTask == true { onShootingTap?() } else if order.canInviteVisitorConfirmation { onConfirmationTap?() } else if order.canUploadFilm == true { onDeliveryTap?() } return } guard let displayedOrder else { return } if displayedOrder.primaryAction == .none { onDeliveryTap?() } else { onShootingTap?() } } @objc private func materialsTapped() { guard displayedOrder != nil || displayedHomeOrder != nil else { return } onMaterialsTap?() } @objc private func confirmationTapped() { guard !deliveryControl.isHidden, deliveryControl.isEnabled else { return } guard displayedOrder != nil || displayedHomeOrder != nil else { return } onConfirmationTap?() } } /// Diffable 订单列表使用的自适应高度容器。 final class DroneShootingOrderCell: UICollectionViewCell { /// UICollectionView 注册与出队标识。 static let reuseIdentifier = "DroneShootingOrderCell" /// 供页面配置订单与语义明确的动作回调。 let cardView = DroneShootingOrderCardView() /// 创建完全依赖卡片内容撑高的列表单元格。 override init(frame: CGRect) { super.init(frame: frame) contentView.addSubview(cardView) cardView.snp.makeConstraints { make in make.edges.equalToSuperview() } } @available(*, unavailable) required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } /// 清除上一订单动作,避免复用时调用旧订单。 override func prepareForReuse() { super.prepareForReuse() cardView.resetForReuse() } }