Files
suixinkan_uikit/suixinkan/UI/DroneDock/DroneDockCompactOrderCell.swift
T

161 lines
8.1 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 DroneDockCompactOrderCell: UICollectionViewCell {
/// 返回该订单的拍摄或当前任务入口。
var onShootingTap: (() -> Void)?
/// 返回该订单原素材入口。
var onMaterialsTap: (() -> Void)?
private let customerLabel = DroneDockStyle.label(size: 16, weight: .bold)
private let statusLabel = DroneDockStatusLabel()
private let orderNumberLabel = DroneDockStyle.label(size: 11, color: DroneDockStyle.secondary)
private let packageLabel = DroneDockStyle.label(size: 13, color: UIColor(hex: 0x666E7C))
private let countsLabel = DroneDockStyle.label(size: 11, color: DroneDockStyle.secondary)
private let actionButton = UIButton(type: .system)
private let materialButton = UIButton(type: .system)
private let footer = UIView()
/// 创建紧凑卡片及可选素材栏。
override init(frame: CGRect) {
super.init(frame: frame)
DroneDockStyle.applyCard(to: contentView)
setupUI()
}
@available(*, unavailable)
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
/// 应用数据,不展示具体点位或航线,也不在卡片自行扣次。
func apply(order: DroneShootingOrder) {
customerLabel.text = order.customerName
orderNumberLabel.isHidden = true
statusLabel.apply(title: order.statusTitle, tone: order.statusTone)
packageLabel.text = order.packageSummary
packageLabel.numberOfLines = 1
countsLabel.numberOfLines = 1
actionButton.titleLabel?.numberOfLines = 1
countsLabel.text = "已拍摄 \(order.successfulShootingCount)/\(order.totalShootingCount) 次 · 剩余 \(order.remainingShootingCount) 次"
actionButton.setTitle(order.primaryAction.title, for: .normal)
actionButton.isHidden = false
actionButton.isEnabled = order.primaryActionEnabled
actionButton.backgroundColor = DroneDockStyle.primary
actionButton.accessibilityIdentifier = "drone.home.\(order.id).primary"
footer.isHidden = !order.hasMaterials
applyMaterial(title: order.materialState.title, symbol: order.materialState.systemImageName,
tone: order.materialState.tone)
accessibilityIdentifier = "drone.home.\(order.id)"
}
/// 展示张工 orders 的真实摘要,权限决定按钮入口,点击交给页面处理。
func apply(homeOrder order: DroneDockHomeOrder) {
customerLabel.text = order.touristTitle
orderNumberLabel.isHidden = false
orderNumberLabel.text = "订单号 \(order.orderNumberText)"
statusLabel.apply(title: order.statusTitle, tone: order.statusTone)
packageLabel.text = order.packageTitle
packageLabel.numberOfLines = 0
countsLabel.numberOfLines = 0
actionButton.titleLabel?.numberOfLines = 2
countsLabel.text = order.progressTitle
actionButton.setTitle(order.actionTitle, for: .normal)
actionButton.isHidden = order.actionTitle == nil
actionButton.isEnabled = order.actionTitle != nil && order.primaryAction != "blocked"
actionButton.backgroundColor = actionButton.isEnabled ? DroneDockStyle.primary : DroneDockStyle.secondary
actionButton.accessibilityIdentifier = "drone.home.order.\(order.orderID).primary"
let materialTitle = order.cardMaterialStatusTitle ?? "素材状态待确认"
footer.isHidden = !order.showsMaterialsEntry
applyMaterial(title: materialTitle, symbol: "photo.on.rectangle.angled", tone: order.cardMaterialStatusTone)
accessibilityIdentifier = "drone.home.order.\(order.orderID)"
}
private func applyMaterial(title: String, symbol: String, tone: DroneDockStatusTone) {
var configuration = UIButton.Configuration.plain()
configuration.image = UIImage(systemName: symbol)
configuration.preferredSymbolConfigurationForImage = UIImage.SymbolConfiguration(pointSize: 15)
configuration.imagePadding = 6
configuration.title = title
configuration.baseForegroundColor = DroneDockStyle.color(for: tone)
configuration.contentInsets = .zero
configuration.titleTextAttributesTransformer = UIConfigurationTextAttributesTransformer { attributes in
var attributes = attributes
attributes.font = .systemFont(ofSize: 11)
return attributes
}
materialButton.configuration = configuration
materialButton.accessibilityLabel = "查看素材,\(title)"
}
override func prepareForReuse() {
super.prepareForReuse()
onShootingTap = nil
onMaterialsTap = nil
}
private func setupUI() {
customerLabel.adjustsFontSizeToFitWidth = true
customerLabel.minimumScaleFactor = 0.75
customerLabel.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
let titleRow = UIStackView(arrangedSubviews: [customerLabel, statusLabel])
titleRow.alignment = .center
titleRow.spacing = 8
orderNumberLabel.isHidden = true
orderNumberLabel.numberOfLines = 0
let labels = UIStackView(arrangedSubviews: [packageLabel, countsLabel])
labels.axis = .vertical
labels.spacing = 9
packageLabel.adjustsFontSizeToFitWidth = true
packageLabel.minimumScaleFactor = 0.8
labels.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
actionButton.backgroundColor = DroneDockStyle.primary
actionButton.setTitleColor(.white, for: .normal)
actionButton.titleLabel?.font = .systemFont(ofSize: 13, weight: .medium)
actionButton.titleLabel?.adjustsFontSizeToFitWidth = true
actionButton.titleLabel?.minimumScaleFactor = 0.8
actionButton.titleLabel?.textAlignment = .center
actionButton.layer.cornerRadius = 8
actionButton.addTarget(self, action: #selector(shootingTapped), for: .touchUpInside)
actionButton.snp.makeConstraints { make in
make.width.equalTo(106).priority(999)
make.height.equalTo(42)
}
let body = UIStackView(arrangedSubviews: [labels, actionButton])
body.spacing = 10
body.alignment = .center
// 让内容行紧贴文字;按钮的固有高度仍可撑高整行,避免自适应高度的舍入余量在两行间游移。
body.snp.makeConstraints { make in make.height.equalTo(labels).priority(249) }
let divider = UIView()
divider.backgroundColor = DroneDockStyle.border
footer.addSubview(divider)
footer.addSubview(materialButton)
materialButton.contentHorizontalAlignment = .leading
materialButton.addTarget(self, action: #selector(materialsTapped), for: .touchUpInside)
let viewButton = UIButton(type: .system)
viewButton.setTitle("查看素材 ›", for: .normal)
viewButton.titleLabel?.font = .systemFont(ofSize: 11)
viewButton.tintColor = DroneDockStyle.primary
viewButton.addTarget(self, action: #selector(materialsTapped), for: .touchUpInside)
footer.addSubview(viewButton)
divider.snp.makeConstraints { make in make.leading.trailing.top.equalToSuperview(); make.height.equalTo(0.5) }
materialButton.snp.makeConstraints { make in
make.leading.equalToSuperview()
make.bottom.equalToSuperview().priority(999)
make.top.equalTo(divider.snp.bottom).offset(7)
make.height.equalTo(26)
make.trailing.lessThanOrEqualTo(viewButton.snp.leading).offset(-8)
}
viewButton.snp.makeConstraints { make in make.centerY.equalTo(materialButton); make.trailing.equalToSuperview() }
let stack = UIStackView(arrangedSubviews: [titleRow, orderNumberLabel, body, footer])
stack.axis = .vertical
stack.spacing = 12
contentView.addSubview(stack)
stack.snp.makeConstraints { make in make.edges.equalToSuperview().inset(16) }
}
@objc private func shootingTapped() { onShootingTap?() }
@objc private func materialsTapped() { onMaterialsTap?() }
}