- 完善双路 RTC 接收、首帧切换及拍摄结束后的本地播放释放 - 对齐无人机状态枚举与正常拍完返航标记,新增到达后本地倒计时 - 补充准备页电量与前往回执,进入任务时移除准备页路由 - 对齐拍摄中和拍摄完成原型布局,移除详情按钮 - 补充隔离测试、接口记录与项目约定 验证:最近 157 项相关模拟器隔离测试通过,Release 无签名编译通过;未调用真实飞控或图传。
317 lines
17 KiB
Swift
317 lines
17 KiB
Swift
import SnapKit
|
|
import UIKit
|
|
|
|
/// 固定区域内组合任务状态与主操作,根据可用高度收紧内容,不滚动或改变外部尺寸。
|
|
@MainActor
|
|
final class DroneDockTaskStatusView: UIView {
|
|
/// 当前状态的主操作,由页面协调执行,不在组件中调用接口。
|
|
var onPrimaryAction: (() -> Void)?
|
|
/// 点击失败原因区域时展开完整说明,由页面协调展示。
|
|
var onDetails: (() -> Void)?
|
|
/// 当前内容采用的字号和间距参考比例,始终介于 0.8 与 1。
|
|
private(set) var effectiveScale: CGFloat = 1
|
|
/// 中间区域不足以显示常规内容时采用紧凑布局。
|
|
private(set) var usesCompactLayout = false
|
|
|
|
private let contentStack = UIStackView()
|
|
private let titleRow = UIView()
|
|
private let iconView = UIImageView()
|
|
private let titleLabel = DroneDockStyle.label(size: 22, weight: .semibold)
|
|
private let messageLabel = DroneDockStyle.label(size: 14, color: DroneDockStyle.secondary)
|
|
private let countdownLabel = DroneDockStyle.label(size: 13, weight: .medium, color: DroneDockStyle.primary)
|
|
private let deviceLabel = DroneDockStyle.label(size: 12, color: DroneDockStyle.secondary)
|
|
private let actionRow = UIView()
|
|
private let primaryButton = UIButton(type: .system)
|
|
private let failureView = DroneDockTaskFailureView()
|
|
private let completedView = DroneDockTaskCompletedView()
|
|
private let shootingView = DroneDockTaskShootingView()
|
|
private var presentation: DroneDockTaskPresentation?
|
|
private var countdownSeconds: Int?
|
|
private var contentTop: Constraint?
|
|
private var contentLeading: Constraint?
|
|
private var contentTrailing: Constraint?
|
|
private var iconSize: Constraint?
|
|
private var titleLeading: Constraint?
|
|
private var titleRowHeight: Constraint?
|
|
private var messageHeight: Constraint?
|
|
private var deviceHeight: Constraint?
|
|
|
|
/// 创建固定状态槽,完成、失败与进行中使用各自的内容布局。
|
|
override init(frame: CGRect) {
|
|
super.init(frame: frame)
|
|
setupUI()
|
|
setupConstraints()
|
|
}
|
|
|
|
@available(*, unavailable)
|
|
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
|
|
|
|
/// 外部容器决定高度;状态内容不会通过固有尺寸推高底部安全区。
|
|
override var intrinsicContentSize: CGSize {
|
|
CGSize(width: UIView.noIntrinsicMetric, height: UIView.noIntrinsicMetric)
|
|
}
|
|
|
|
/// 更新真实状态与动作许可;刷新期间保留内容,只暂时禁用主操作。
|
|
func apply(_ presentation: DroneDockTaskPresentation, isRefreshing: Bool = false) {
|
|
self.presentation = presentation
|
|
updateCountdown(seconds: presentation.countdownSeconds)
|
|
let isFailure = presentation.phase == .failed
|
|
let isCompleted = [.postShootUploading, .completed].contains(presentation.phase)
|
|
let isShooting = presentation.phase == .shooting
|
|
contentStack.isHidden = isFailure || isCompleted || isShooting
|
|
failureView.isHidden = !isFailure
|
|
completedView.isHidden = !isCompleted
|
|
shootingView.isHidden = !isShooting
|
|
completedView.setIdentifiersActive(isCompleted)
|
|
shootingView.setIdentifiersActive(isShooting)
|
|
titleLabel.accessibilityIdentifier = isCompleted || isShooting ? nil : "drone.task.remote.statusTitle"
|
|
messageLabel.accessibilityIdentifier = isCompleted || isShooting ? nil : "drone.task.remote.statusMessage"
|
|
if isShooting {
|
|
primaryButton.accessibilityIdentifier = nil
|
|
shootingView.apply(presentation)
|
|
setNeedsLayout()
|
|
return
|
|
}
|
|
if isCompleted {
|
|
primaryButton.accessibilityIdentifier = nil
|
|
completedView.apply(presentation, isRefreshing: isRefreshing)
|
|
setNeedsLayout()
|
|
return
|
|
}
|
|
if isFailure {
|
|
failureView.apply(presentation, isRefreshing: isRefreshing)
|
|
setNeedsLayout()
|
|
return
|
|
}
|
|
titleLabel.text = presentation.title
|
|
messageLabel.text = presentation.message
|
|
deviceLabel.text = presentation.deviceStatus
|
|
let style = Self.style(for: presentation.phase)
|
|
iconView.image = UIImage(systemName: style.symbol)
|
|
iconView.tintColor = style.color
|
|
let actionTitle = DroneDockHomeMetric.usableText(presentation.actionTitle)
|
|
primaryButton.isHidden = presentation.action == .none || actionTitle == nil
|
|
primaryButton.setTitle(actionTitle, for: .normal)
|
|
primaryButton.isEnabled = presentation.isActionEnabled && !primaryButton.isHidden && !isRefreshing
|
|
primaryButton.backgroundColor = primaryButton.isEnabled
|
|
? DroneDockStyle.primary : DroneDockStyle.primary.withAlphaComponent(0.35)
|
|
primaryButton.accessibilityIdentifier = presentation.action == .refresh
|
|
? "drone.task.remote.refresh" : "drone.task.remote.primary"
|
|
setNeedsLayout()
|
|
}
|
|
|
|
/// 只刷新已到达阶段的剩余秒数,不变更主操作、服务端阶段或执行任何回调。
|
|
func updateCountdown(seconds: Int?) {
|
|
let value = presentation?.phase == .ready ? seconds.map { max(0, $0) } : nil
|
|
guard value != countdownSeconds else { return }
|
|
let visibilityChanged = (value == nil) != (countdownSeconds == nil)
|
|
countdownSeconds = value
|
|
countdownLabel.text = value.map { "自动开拍倒计时:\($0) 秒" }
|
|
countdownLabel.isHidden = value == nil
|
|
if visibilityChanged { setNeedsLayout() }
|
|
}
|
|
|
|
/// 尺寸改变时仅重排内部内容,保留至少 44 点操作区域,不使用 transform 缩放。
|
|
override func layoutSubviews() {
|
|
if presentation?.phase == .shooting {
|
|
super.layoutSubviews()
|
|
shootingView.layoutIfNeeded()
|
|
effectiveScale = shootingView.effectiveScale
|
|
usesCompactLayout = shootingView.usesCompactLayout
|
|
return
|
|
}
|
|
if let phase = presentation?.phase, [.postShootUploading, .completed].contains(phase) {
|
|
super.layoutSubviews()
|
|
completedView.layoutIfNeeded()
|
|
effectiveScale = 1
|
|
usesCompactLayout = completedView.usesCompactLayout
|
|
return
|
|
}
|
|
if presentation?.phase == .failed {
|
|
super.layoutSubviews()
|
|
failureView.layoutIfNeeded()
|
|
effectiveScale = failureView.effectiveScale
|
|
usesCompactLayout = failureView.usesCompactLayout
|
|
return
|
|
}
|
|
updateContentLayout()
|
|
super.layoutSubviews()
|
|
}
|
|
|
|
private func setupUI() {
|
|
backgroundColor = .white
|
|
addSubview(shootingView)
|
|
shootingView.isHidden = true
|
|
addSubview(completedView)
|
|
completedView.isHidden = true
|
|
completedView.onViewMaterials = { [weak self] in self?.onPrimaryAction?() }
|
|
addSubview(failureView)
|
|
failureView.isHidden = true
|
|
failureView.onDetails = { [weak self] in self?.onDetails?() }
|
|
failureView.onPrimaryAction = { [weak self] in self?.onPrimaryAction?() }
|
|
contentStack.axis = .vertical
|
|
contentStack.alignment = .fill
|
|
contentStack.spacing = 10
|
|
addSubview(contentStack)
|
|
contentStack.addArrangedSubview(titleRow)
|
|
contentStack.addArrangedSubview(messageLabel)
|
|
contentStack.addArrangedSubview(countdownLabel)
|
|
contentStack.addArrangedSubview(deviceLabel)
|
|
contentStack.addArrangedSubview(actionRow)
|
|
titleRow.addSubview(iconView)
|
|
titleRow.addSubview(titleLabel)
|
|
iconView.contentMode = .scaleAspectFit
|
|
titleLabel.numberOfLines = 2
|
|
titleLabel.lineBreakMode = .byTruncatingTail
|
|
titleLabel.accessibilityTraits = .header
|
|
titleLabel.accessibilityIdentifier = "drone.task.remote.statusTitle"
|
|
messageLabel.numberOfLines = 3
|
|
messageLabel.lineBreakMode = .byTruncatingTail
|
|
messageLabel.accessibilityIdentifier = "drone.task.remote.statusMessage"
|
|
countdownLabel.font = .monospacedDigitSystemFont(ofSize: 13, weight: .medium)
|
|
countdownLabel.numberOfLines = 1
|
|
countdownLabel.isHidden = true
|
|
countdownLabel.accessibilityIdentifier = "drone.task.remote.countdown"
|
|
deviceLabel.numberOfLines = 1
|
|
deviceLabel.lineBreakMode = .byTruncatingTail
|
|
deviceLabel.accessibilityIdentifier = "drone.task.remote.deviceStatus"
|
|
actionRow.addSubview(primaryButton)
|
|
primaryButton.layer.cornerRadius = 9
|
|
primaryButton.titleLabel?.font = .systemFont(ofSize: 15, weight: .semibold)
|
|
primaryButton.titleLabel?.lineBreakMode = .byTruncatingTail
|
|
primaryButton.setTitleColor(.white, for: .normal)
|
|
primaryButton.setTitleColor(.white.withAlphaComponent(0.9), for: .disabled)
|
|
primaryButton.addTarget(self, action: #selector(primaryTapped), for: .touchUpInside)
|
|
}
|
|
|
|
private func setupConstraints() {
|
|
shootingView.snp.makeConstraints { $0.edges.equalToSuperview() }
|
|
completedView.snp.makeConstraints { $0.edges.equalToSuperview() }
|
|
failureView.snp.makeConstraints { make in make.edges.equalToSuperview() }
|
|
contentStack.snp.makeConstraints { make in
|
|
contentTop = make.top.equalToSuperview().offset(12).constraint
|
|
contentLeading = make.leading.equalToSuperview().offset(16).constraint
|
|
contentTrailing = make.trailing.equalToSuperview().inset(16).constraint
|
|
}
|
|
titleRow.snp.makeConstraints { make in titleRowHeight = make.height.equalTo(36).priority(999).constraint }
|
|
iconView.snp.makeConstraints { make in
|
|
make.leading.centerY.equalToSuperview()
|
|
iconSize = make.size.equalTo(36).constraint
|
|
}
|
|
titleLabel.snp.makeConstraints { make in
|
|
titleLeading = make.leading.equalTo(iconView.snp.trailing).offset(10).constraint
|
|
make.trailing.centerY.equalToSuperview()
|
|
}
|
|
messageLabel.snp.makeConstraints { make in messageHeight = make.height.equalTo(34).priority(999).constraint }
|
|
countdownLabel.snp.makeConstraints { make in make.height.equalTo(ceil(countdownLabel.font.lineHeight)).priority(999) }
|
|
deviceLabel.snp.makeConstraints { make in deviceHeight = make.height.equalTo(16).priority(999).constraint }
|
|
actionRow.snp.makeConstraints { make in make.height.equalTo(44) }
|
|
primaryButton.snp.makeConstraints { make in
|
|
make.edges.equalToSuperview()
|
|
}
|
|
}
|
|
|
|
private func updateContentLayout() {
|
|
guard let presentation, bounds.width > 0, bounds.height > 0 else { return }
|
|
let isExtreme = bounds.height < 120
|
|
let hasCountdown = countdownSeconds != nil
|
|
let horizontalInset: CGFloat = isExtreme ? 12 : 16
|
|
let verticalInset: CGFloat = isExtreme ? (hasCountdown ? 4 : 6) : 12
|
|
let width = max(1, bounds.width - horizontalInset * 2)
|
|
let availableHeight = max(1, bounds.height - verticalInset * 2)
|
|
let message = DroneDockHomeMetric.usableText(presentation.message)
|
|
let hasMessage = message != nil && message != DroneDockHomeMetric.usableText(presentation.title)
|
|
let device = DroneDockHomeMetric.usableText(presentation.deviceStatus)
|
|
let hasDevice = device != nil && device != "—"
|
|
|
|
let naturalTitle = max(36, Self.textHeight(presentation.title, font: .systemFont(ofSize: 22, weight: .semibold),
|
|
width: width - 46, lines: 2))
|
|
let naturalMessage = hasMessage ? Self.textHeight(message, font: .systemFont(ofSize: 14), width: width, lines: 3) : 0
|
|
let naturalDevice = hasDevice ? UIFont.systemFont(ofSize: 12).lineHeight : 0
|
|
let countdownTextHeight = hasCountdown ? ceil(countdownLabel.font.lineHeight) : 0
|
|
let naturalGaps = CGFloat(1 + (hasMessage ? 1 : 0) + (hasDevice ? 1 : 0) + (hasCountdown ? 1 : 0)) * 10
|
|
let naturalHeight = naturalTitle + naturalMessage + countdownTextHeight + naturalDevice + 44 + naturalGaps
|
|
effectiveScale = min(1, max(0.8, availableHeight / max(1, naturalHeight)))
|
|
usesCompactLayout = bounds.height < 190 || naturalHeight * effectiveScale > availableHeight
|
|
|
|
// 先收紧图标和间距;只有不足九成空间时才缩小文字。
|
|
let fontScale: CGFloat = effectiveScale >= 0.9 ? 1 : effectiveScale / 0.9
|
|
titleLabel.font = .systemFont(ofSize: isExtreme && hasCountdown ? 17 : max(17, 22 * fontScale), weight: .semibold)
|
|
messageLabel.font = .systemFont(ofSize: max(12, 14 * fontScale))
|
|
deviceLabel.font = .systemFont(ofSize: 12)
|
|
let icon: CGFloat = isExtreme ? 20 : usesCompactLayout ? 24 : 36 * effectiveScale
|
|
let gap: CGFloat = isExtreme ? (hasCountdown ? 2 : 4)
|
|
: hasCountdown && usesCompactLayout ? 6 : max(4, 10 * effectiveScale)
|
|
let titleGap: CGFloat = usesCompactLayout ? 8 : 10 * effectiveScale
|
|
titleLabel.numberOfLines = usesCompactLayout ? 1 : 2
|
|
messageLabel.numberOfLines = usesCompactLayout ? 2 : 3
|
|
messageLabel.isHidden = !hasMessage || isExtreme
|
|
deviceLabel.isHidden = !hasDevice || usesCompactLayout
|
|
let titleWidth = max(1, width - icon - titleGap)
|
|
var titleHeight = max(icon, Self.textHeight(presentation.title, font: titleLabel.font,
|
|
width: titleWidth, lines: titleLabel.numberOfLines))
|
|
var messageLineCount = messageLabel.numberOfLines
|
|
var bodyHeight = messageLabel.isHidden ? 0 : Self.textHeight(message, font: messageLabel.font,
|
|
width: width, lines: messageLineCount)
|
|
let deviceTextHeight = deviceLabel.isHidden ? 0 : ceil(deviceLabel.font.lineHeight)
|
|
var contentHeight: CGFloat {
|
|
titleHeight + bodyHeight + countdownTextHeight + deviceTextHeight + 44
|
|
+ CGFloat(1 + (messageLabel.isHidden ? 0 : 1) + (deviceLabel.isHidden ? 0 : 1) + (hasCountdown ? 1 : 0)) * gap
|
|
}
|
|
while contentHeight > availableHeight && messageLineCount > 1 && !messageLabel.isHidden {
|
|
messageLineCount -= 1
|
|
bodyHeight = Self.textHeight(message, font: messageLabel.font, width: width, lines: messageLineCount)
|
|
}
|
|
if contentHeight > availableHeight && titleLabel.numberOfLines > 1 {
|
|
titleLabel.numberOfLines = 1
|
|
titleHeight = max(icon, ceil(titleLabel.font.lineHeight))
|
|
}
|
|
if contentHeight > availableHeight && !messageLabel.isHidden {
|
|
messageLabel.isHidden = true
|
|
bodyHeight = 0
|
|
}
|
|
messageLabel.numberOfLines = messageLineCount
|
|
let top = verticalInset + max(0, (availableHeight - contentHeight) / 2)
|
|
contentTop?.update(offset: top)
|
|
contentLeading?.update(offset: horizontalInset)
|
|
contentTrailing?.update(inset: horizontalInset)
|
|
iconSize?.update(offset: icon)
|
|
titleLeading?.update(offset: titleGap)
|
|
titleRowHeight?.update(offset: titleHeight)
|
|
messageHeight?.update(offset: max(1, bodyHeight))
|
|
deviceHeight?.update(offset: max(1, deviceTextHeight))
|
|
contentStack.spacing = gap
|
|
}
|
|
|
|
private static func textHeight(_ text: String?, font: UIFont, width: CGFloat, lines: Int) -> CGFloat {
|
|
guard let text, !text.isEmpty else { return ceil(font.lineHeight) }
|
|
let height = (text as NSString).boundingRect(with: CGSize(width: max(1, width), height: .greatestFiniteMagnitude),
|
|
options: [.usesLineFragmentOrigin, .usesFontLeading],
|
|
attributes: [.font: font], context: nil).height
|
|
return ceil(min(height, font.lineHeight * CGFloat(lines)))
|
|
}
|
|
|
|
private static func style(for phase: DroneDockTaskDisplayPhase) -> (symbol: String, color: UIColor) {
|
|
switch phase {
|
|
case .loading: ("clock.arrow.circlepath", DroneDockStyle.primary)
|
|
case .navigating: ("paperplane.fill", DroneDockStyle.primary)
|
|
case .ready: ("checkmark.circle.fill", DroneDockStyle.success)
|
|
case .shooting: ("video.fill", DroneDockStyle.primary)
|
|
case .postShootUploading: ("checkmark.circle.fill", DroneDockStyle.success)
|
|
case .postShootUploadFailed: ("exclamationmark.circle.fill", DroneDockStyle.warning)
|
|
case .paused: ("pause.circle.fill", DroneDockStyle.warning)
|
|
case .returning: ("arrow.uturn.backward.circle.fill", DroneDockStyle.warning)
|
|
case .completed: ("checkmark.circle.fill", DroneDockStyle.success)
|
|
case .failed: ("xmark.circle.fill", UIColor(hex: 0xD46B62))
|
|
case .blocked, .readError: ("exclamationmark.circle.fill", DroneDockStyle.warning)
|
|
case .unknown: ("questionmark.circle.fill", DroneDockStyle.secondary)
|
|
}
|
|
}
|
|
|
|
@objc private func primaryTapped() {
|
|
guard primaryButton.isEnabled, !primaryButton.isHidden else { return }
|
|
onPrimaryAction?()
|
|
}
|
|
|
|
}
|