Files
suixinkan_uikit/suixinkan/UI/DroneDock/DroneShootingOrderCardView.swift
T
lujiuyin 7862d208ce feat(drone-dock): 完成交付成片UI及首页入口适配
包含全部剩余改动:交付上传与游客确认流程、相关真机测试、drone_dock入口、后端接口与原型复核文档、截图及周报。
2026-10-26 15:43:27 +08:00

337 lines
15 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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?
/// 创建可以按内容自行撑高的白色圆角订单卡。
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
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(order: order)
applyMaterialSummary(order: order)
applyActions(order: order)
accessibilityIdentifier = "drone.order.\(order.id)"
}
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) }
}
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(order: DroneShootingOrder) {
let foreground: UIColor
let background: UIColor
switch order.statusTone {
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 applyMaterialSummary(order: DroneShootingOrder) {
materialRow.isHidden = !order.hasMaterials && order.deliveredVideoCount == 0
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)"
let showsDelivery = order.deliveredVideoCount > 0
deliveryControl.isHidden = !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
}
@objc private func secondaryTapped() {
guard let displayedOrder else { return }
if displayedOrder.primaryAction == .none {
onMaterialsTap?()
} else {
onDeliveryTap?()
}
}
@objc private func primaryTapped() {
guard let displayedOrder else { return }
if displayedOrder.primaryAction == .none {
onDeliveryTap?()
} else {
onShootingTap?()
}
}
@objc private func materialsTapped() { onMaterialsTap?() }
@objc private func confirmationTapped() { 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.onShootingTap = nil
cardView.onMaterialsTap = nil
cardView.onDeliveryTap = nil
}
}