feat: 完善机巢任务安全控制与状态诊断

同步新版飞控与 WebSocket 回执合同,完善独立指令等待、暂停继续和返航状态;增加真机诊断并优化倒计时与素材库展示。
This commit is contained in:
2026-11-04 17:34:28 +08:00
parent 1c766ff12f
commit e3979e9e32
44 changed files with 4575 additions and 581 deletions
@@ -18,13 +18,13 @@ final class DroneDockTaskStatusView: 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 let controlStateView = DroneDockTaskControlStateView()
private var presentation: DroneDockTaskPresentation?
private var countdownSeconds: Int?
private var contentTop: Constraint?
@@ -36,6 +36,35 @@ final class DroneDockTaskStatusView: UIView {
private var messageHeight: Constraint?
private var deviceHeight: Constraint?
/// 读取当前可见控件的实际文案和许可,用于对照录屏;不改变布局或任务状态。
var diagnosticFields: [String: Any] {
func descendants(_ root: UIView) -> [UIView] { [root] + root.subviews.flatMap(descendants) }
func isVisible(_ view: UIView) -> Bool {
if view === self { return !view.isHidden }
return !view.isHidden && (view.superview.map(isVisible) ?? false)
}
let visible = descendants(self).filter(isVisible)
let buttonIDs = ["drone.task.remote.primary", "drone.task.remote.refresh", "drone.task.remote.failureRetry",
"drone.task.remote.shooting.indicator"]
let button = visible.compactMap { $0 as? UIButton }.first { buttonIDs.contains($0.accessibilityIdentifier ?? "") }
?? visible.compactMap { $0 as? UIButton }.first
let label = visible.compactMap { $0 as? UILabel }.first {
["drone.task.remote.statusTitle", "drone.task.remote.failureTitle"].contains($0.accessibilityIdentifier ?? "")
}
let message = visible.compactMap { $0 as? UILabel }.first {
["drone.task.remote.statusMessage", "drone.task.remote.failureReason"].contains($0.accessibilityIdentifier ?? "")
}
let countdownState = countdownSeconds == nil ? "hidden" : countdownSeconds == 0 ? "zero" : "running"
return ["phase": presentation?.phase.rawValue ?? "uninitialized",
"title": label?.text ?? presentation?.title ?? "",
"message": message?.text ?? "", "message_visible": message != nil,
"action": presentation.map { String(describing: $0.action) } ?? "none",
"action_title": button?.configuration?.title ?? button?.title(for: .normal) ?? "",
"action_enabled": button?.isEnabled == true && isUserInteractionEnabled,
"action_loading": visible.compactMap { $0 as? UIActivityIndicatorView }.contains { $0.isAnimating },
"interactive": isUserInteractionEnabled, "countdown": countdownState]
}
/// 创建固定状态槽,完成、失败与进行中使用各自的内容布局。
override init(frame: CGRect) {
super.init(frame: frame)
@@ -52,20 +81,29 @@ final class DroneDockTaskStatusView: UIView {
}
/// 更新真实状态与动作许可;刷新期间保留内容,只暂时禁用主操作。
func apply(_ presentation: DroneDockTaskPresentation, isRefreshing: Bool = false) {
func apply(_ presentation: DroneDockTaskPresentation, isRefreshing: Bool = false, isPrimaryActionLoading: 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
let isControlState = [.paused, .returning, .returned].contains(presentation.phase)
contentStack.isHidden = isFailure || isCompleted || isShooting || isControlState
failureView.isHidden = !isFailure
completedView.isHidden = !isCompleted
shootingView.isHidden = !isShooting
controlStateView.isHidden = !isControlState
completedView.setIdentifiersActive(isCompleted)
shootingView.setIdentifiersActive(isShooting)
titleLabel.accessibilityIdentifier = isCompleted || isShooting ? nil : "drone.task.remote.statusTitle"
messageLabel.accessibilityIdentifier = isCompleted || isShooting ? nil : "drone.task.remote.statusMessage"
controlStateView.setIdentifiersActive(isControlState)
titleLabel.accessibilityIdentifier = isCompleted || isShooting || isControlState ? nil : "drone.task.remote.statusTitle"
messageLabel.accessibilityIdentifier = isCompleted || isShooting || isControlState ? nil : "drone.task.remote.statusMessage"
if isControlState {
primaryButton.accessibilityIdentifier = nil
controlStateView.apply(presentation, isRefreshing: isRefreshing, isPrimaryActionLoading: isPrimaryActionLoading)
setNeedsLayout()
return
}
if isShooting {
primaryButton.accessibilityIdentifier = nil
shootingView.apply(presentation)
@@ -91,7 +129,7 @@ final class DroneDockTaskStatusView: UIView {
iconView.tintColor = style.color
let actionTitle = DroneDockHomeMetric.usableText(presentation.actionTitle)
primaryButton.isHidden = presentation.action == .none || actionTitle == nil
primaryButton.setTitle(actionTitle, for: .normal)
updatePrimaryButtonTitle()
primaryButton.isEnabled = presentation.isActionEnabled && !primaryButton.isHidden && !isRefreshing
primaryButton.backgroundColor = primaryButton.isEnabled
? DroneDockStyle.primary : DroneDockStyle.primary.withAlphaComponent(0.35)
@@ -100,19 +138,41 @@ final class DroneDockTaskStatusView: UIView {
setNeedsLayout()
}
/// 只刷新已到达阶段的剩余秒数,不变更主操作、服务端阶段或执行任何回调。
/// 只刷新开拍按钮中的剩余秒数,不变更操作许可、服务端阶段或执行任何回调。
func updateCountdown(seconds: Int?) {
let value = presentation?.phase == .ready ? seconds.map { max(0, $0) } : nil
let value = presentation?.phase == .ready && presentation?.action == .start
? 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() }
updatePrimaryButtonTitle()
}
private func updatePrimaryButtonTitle() {
guard let presentation else { return }
let title = DroneDockHomeMetric.usableText(presentation.actionTitle)
let displayedTitle: String?
if presentation.phase == .ready, presentation.action == .start,
let title, let countdownSeconds {
displayedTitle = "\(title)(\(countdownSeconds)s)"
} else {
displayedTitle = title
}
// 秒数直接替换,避免系统按钮的标题过渡暂时保留旧数字。
UIView.performWithoutAnimation {
primaryButton.setTitle(displayedTitle, for: .normal)
primaryButton.layoutIfNeeded()
}
}
/// 尺寸改变时仅重排内部内容,保留至少 44 点操作区域,不使用 transform 缩放。
override func layoutSubviews() {
if let phase = presentation?.phase, [.paused, .returning, .returned].contains(phase) {
super.layoutSubviews()
controlStateView.layoutIfNeeded()
effectiveScale = controlStateView.effectiveScale
usesCompactLayout = controlStateView.usesCompactLayout
return
}
if presentation?.phase == .shooting {
super.layoutSubviews()
shootingView.layoutIfNeeded()
@@ -140,6 +200,9 @@ final class DroneDockTaskStatusView: UIView {
private func setupUI() {
backgroundColor = .white
addSubview(controlStateView)
controlStateView.isHidden = true
controlStateView.onPrimaryAction = { [weak self] in self?.onPrimaryAction?() }
addSubview(shootingView)
shootingView.isHidden = true
addSubview(completedView)
@@ -155,7 +218,6 @@ final class DroneDockTaskStatusView: UIView {
addSubview(contentStack)
contentStack.addArrangedSubview(titleRow)
contentStack.addArrangedSubview(messageLabel)
contentStack.addArrangedSubview(countdownLabel)
contentStack.addArrangedSubview(deviceLabel)
contentStack.addArrangedSubview(actionRow)
titleRow.addSubview(iconView)
@@ -168,16 +230,12 @@ final class DroneDockTaskStatusView: UIView {
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?.font = .monospacedDigitSystemFont(ofSize: 15, weight: .semibold)
primaryButton.titleLabel?.lineBreakMode = .byTruncatingTail
primaryButton.setTitleColor(.white, for: .normal)
primaryButton.setTitleColor(.white.withAlphaComponent(0.9), for: .disabled)
@@ -185,6 +243,7 @@ final class DroneDockTaskStatusView: UIView {
}
private func setupConstraints() {
controlStateView.snp.makeConstraints { $0.edges.equalToSuperview() }
shootingView.snp.makeConstraints { $0.edges.equalToSuperview() }
completedView.snp.makeConstraints { $0.edges.equalToSuperview() }
failureView.snp.makeConstraints { make in make.edges.equalToSuperview() }
@@ -203,7 +262,6 @@ final class DroneDockTaskStatusView: UIView {
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
@@ -214,9 +272,8 @@ final class DroneDockTaskStatusView: UIView {
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 verticalInset: CGFloat = isExtreme ? 6 : 12
let width = max(1, bounds.width - horizontalInset * 2)
let availableHeight = max(1, bounds.height - verticalInset * 2)
let message = DroneDockHomeMetric.usableText(presentation.message)
@@ -228,20 +285,18 @@ final class DroneDockTaskStatusView: UIView {
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
let naturalGaps = CGFloat(1 + (hasMessage ? 1 : 0) + (hasDevice ? 1 : 0)) * 10
let naturalHeight = naturalTitle + naturalMessage + 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)
titleLabel.font = .systemFont(ofSize: 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 gap: CGFloat = isExtreme ? 4 : max(4, 10 * effectiveScale)
let titleGap: CGFloat = usesCompactLayout ? 8 : 10 * effectiveScale
titleLabel.numberOfLines = usesCompactLayout ? 1 : 2
messageLabel.numberOfLines = usesCompactLayout ? 2 : 3
@@ -255,8 +310,8 @@ final class DroneDockTaskStatusView: UIView {
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
titleHeight + bodyHeight + deviceTextHeight + 44
+ CGFloat(1 + (messageLabel.isHidden ? 0 : 1) + (deviceLabel.isHidden ? 0 : 1)) * gap
}
while contentHeight > availableHeight && messageLineCount > 1 && !messageLabel.isHidden {
messageLineCount -= 1
@@ -301,6 +356,7 @@ final class DroneDockTaskStatusView: UIView {
case .postShootUploadFailed: ("exclamationmark.circle.fill", DroneDockStyle.warning)
case .paused: ("pause.circle.fill", DroneDockStyle.warning)
case .returning: ("arrow.uturn.backward.circle.fill", DroneDockStyle.warning)
case .returned: ("house.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)