import SnapKit import UIKit /// 任务页的本地画面演示组件;连接、断流与任务结果分别展示。 @MainActor final class DroneShootingTaskFeedView: UIView { /// 用户请求查看本地静帧大图。 var onExpand: (() -> Void)? /// 用户请求重连手机或画面,具体意图由当前连接状态决定。 var onReconnect: (() -> Void)? private let imageView = UIImageView(image: UIImage(named: "drone_demo_tourists")) private let captionLabel = DroneDockStyle.label("演示画面 · 本地静帧", size: 11, color: .white) private let expandButton = UIButton(type: .system) private let interruptionView = UIView() private let interruptionIcon = UIImageView() private let interruptionTitle = DroneDockStyle.label(size: 16, weight: .semibold) private let interruptionMessage = DroneDockStyle.label(size: 12, color: DroneDockStyle.secondary) private let reconnectButton = UIButton(type: .system) /// 创建 16:9 本地图片画面,不请求网络,也不播放视频。 override init(frame: CGRect) { super.init(frame: frame) setupUI() setupConstraints() } @available(*, unavailable) required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } /// 独立应用手机连接与画面连接;完成任务只改变静帧说明。 func apply(phone: DroneShootingConnectionState, feed: DroneShootingConnectionState, isFlightFinished: Bool) { let phoneUnavailable = phone != .connected let feedUnavailable = feed != .connected interruptionView.isHidden = !phoneUnavailable && !feedUnavailable expandButton.isHidden = phoneUnavailable || feedUnavailable captionLabel.text = isFlightFinished ? "上次任务画面 · 本地演示" : "演示画面 · 本地静帧" if phoneUnavailable { interruptionIcon.image = UIImage(systemName: "wifi.slash") switch phone { case .disconnected: interruptionTitle.text = "手机网络连接中断" interruptionMessage.text = "状态暂未更新,请恢复连接后同步当前任务" configureReconnect(title: "重新连接", enabled: true) case .reconnecting: interruptionTitle.text = "正在重新连接" interruptionMessage.text = "等待本地模拟连接回执" configureReconnect(title: "正在连接", enabled: false) case .synchronizing: interruptionTitle.text = "正在同步当前任务" interruptionMessage.text = "同步确认前请勿重复发送指令" configureReconnect(title: "正在同步", enabled: false) case .connected: break } } else if feedUnavailable { interruptionIcon.image = UIImage(systemName: "video.slash") interruptionTitle.text = feed == .disconnected ? "演示画面连接中断" : "正在重新连接画面" interruptionMessage.text = "仅画面中断,任务与控制状态分别更新" configureReconnect(title: feed == .disconnected ? "重新连接画面" : "正在连接画面", enabled: feed == .disconnected) } } private func setupUI() { backgroundColor = UIColor(hex: 0xE9EEF6) clipsToBounds = true imageView.contentMode = .scaleAspectFill imageView.clipsToBounds = true imageView.accessibilityLabel = "游客取景示意,本地演示静帧" addSubview(imageView) let captionBackground = UIView() captionBackground.backgroundColor = UIColor.black.withAlphaComponent(0.38) captionBackground.layer.cornerRadius = 5 captionBackground.addSubview(captionLabel) addSubview(captionBackground) captionBackground.snp.makeConstraints { make in make.leading.top.equalToSuperview().inset(12) } captionLabel.snp.makeConstraints { make in make.edges.equalToSuperview().inset(UIEdgeInsets(top: 5, left: 7, bottom: 5, right: 7)) } expandButton.setImage(UIImage(systemName: "arrow.up.left.and.arrow.down.right"), for: .normal) expandButton.tintColor = .white expandButton.backgroundColor = UIColor.black.withAlphaComponent(0.35) expandButton.layer.cornerRadius = 18 expandButton.accessibilityLabel = "放大本地演示画面" expandButton.accessibilityIdentifier = "drone.task.feed.expand" expandButton.addTarget(self, action: #selector(expandTapped), for: .touchUpInside) addSubview(expandButton) interruptionView.backgroundColor = UIColor(hex: 0xE9EEF6) addSubview(interruptionView) interruptionIcon.contentMode = .scaleAspectFit interruptionIcon.tintColor = UIColor(hex: 0x8992A3) interruptionTitle.textAlignment = .center interruptionMessage.textAlignment = .center interruptionMessage.numberOfLines = 0 interruptionView.addSubview(interruptionIcon) interruptionView.addSubview(interruptionTitle) interruptionView.addSubview(interruptionMessage) interruptionView.addSubview(reconnectButton) reconnectButton.accessibilityIdentifier = "drone.task.feed.reconnect" reconnectButton.addTarget(self, action: #selector(reconnectTapped), for: .touchUpInside) } private func setupConstraints() { imageView.snp.makeConstraints { make in make.edges.equalToSuperview() } expandButton.snp.makeConstraints { make in make.trailing.bottom.equalToSuperview().inset(12) make.size.equalTo(36) } interruptionView.snp.makeConstraints { make in make.edges.equalToSuperview() } interruptionIcon.snp.makeConstraints { make in make.centerX.equalToSuperview() make.centerY.equalToSuperview().offset(-49) make.size.equalTo(30) } interruptionTitle.snp.makeConstraints { make in make.top.equalTo(interruptionIcon.snp.bottom).offset(11) make.leading.trailing.equalToSuperview().inset(16) } interruptionMessage.snp.makeConstraints { make in make.top.equalTo(interruptionTitle.snp.bottom).offset(5) make.leading.trailing.equalToSuperview().inset(18) } reconnectButton.snp.makeConstraints { make in make.top.equalTo(interruptionMessage.snp.bottom).offset(11) make.centerX.equalToSuperview() make.height.equalTo(34) } } private func configureReconnect(title: String, enabled: Bool) { var configuration = UIButton.Configuration.bordered() configuration.title = title configuration.baseForegroundColor = DroneDockStyle.primary configuration.background.strokeColor = DroneDockStyle.primary.withAlphaComponent(0.5) configuration.background.strokeWidth = 1 configuration.background.backgroundColor = .clear configuration.cornerStyle = .medium configuration.contentInsets = NSDirectionalEdgeInsets(top: 6, leading: 16, bottom: 6, trailing: 16) configuration.titleTextAttributesTransformer = UIConfigurationTextAttributesTransformer { incoming in var outgoing = incoming outgoing.font = .systemFont(ofSize: 12, weight: .medium) return outgoing } reconnectButton.configuration = configuration reconnectButton.isEnabled = enabled } @objc private func expandTapped() { onExpand?() } @objc private func reconnectTapped() { onReconnect?() } } /// 任务页六项遥测格;手机断网或读数缺失时保留未知值。 @MainActor final class DroneShootingTaskTelemetryView: UIView { private let stackView = UIStackView() private let staleLabel = DroneDockStyle.label("ⓘ 状态暂未更新", size: 11, color: DroneDockStyle.secondary) private var cells: [DroneShootingTaskTelemetryCell] = [] private var rowHeights: [Constraint] = [] private var contentEdges: Constraint? private var usesFixedLayout = false /// 以两行三列固定展示电量、温度、风速、雨量、信号与搜星。 override init(frame: CGRect) { super.init(frame: frame) backgroundColor = DroneDockStyle.background stackView.axis = .vertical stackView.spacing = 6 stackView.alignment = .fill addSubview(stackView) stackView.addArrangedSubview(staleLabel) let definitions = [ ("电池电量", "battery.75percent"), ("环境温度", "thermometer.medium"), ("风速", "wind"), ("雨量", "cloud.rain"), ("信号状态", "cellularbars"), ("搜星状态", "location.north.circle") ] for rowIndex in 0..<2 { let row = UIStackView() row.axis = .horizontal row.distribution = .fillEqually row.spacing = 6 for columnIndex in 0..<3 { let definition = definitions[rowIndex * 3 + columnIndex] let cell = DroneShootingTaskTelemetryCell(title: definition.0, symbol: definition.1) cells.append(cell) row.addArrangedSubview(cell) } stackView.addArrangedSubview(row) row.snp.makeConstraints { make in rowHeights.append(make.height.equalTo(55).constraint) } } stackView.snp.makeConstraints { make in contentEdges = make.edges.equalToSuperview().inset(10).constraint } } @available(*, unavailable) required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } /// 固定任务页不增减状态提示行;小屏仅压缩格子间距与留白,六项参数位置保持稳定。 func configureFixedLayout(compact: Bool) { usesFixedLayout = true staleLabel.isHidden = true rowHeights.forEach { $0.update(offset: compact ? 46 : 55) } stackView.spacing = compact ? 4 : 6 contentEdges?.update(inset: compact ? 8 : 10) } /// 新快照覆盖全部读数;不保留断网前的值冒充有效数据。 func apply(telemetry: DroneDockTelemetry, isStale: Bool) { apply(values: [telemetry.batteryText, telemetry.temperatureText, telemetry.windText, telemetry.rainText, telemetry.signalText, telemetry.satelliteText], isStale: isStale) } /// 展示后端已格式化的六项读数,保留单位;缺项与过期读数显示破折号。 func apply(values: [String], isStale: Bool, accessibilityPrefix: String? = nil) { staleLabel.isHidden = usesFixedLayout || !isStale let keys = ["battery", "temperature", "wind", "rain", "signal", "satellite"] for (index, cell) in cells.enumerated() { let value = !isStale && values.indices.contains(index) ? DroneDockHomeMetric.usableText(values[index]) ?? "—" : "—" cell.apply(value: value, isStale: isStale) if let accessibilityPrefix { cell.accessibilityIdentifier = "\(accessibilityPrefix).\(keys[index])" } } } } /// 单项遥测读数,标题与图标保持稳定位置。 @MainActor private final class DroneShootingTaskTelemetryCell: UIView { private let valueLabel = DroneDockStyle.label(size: 16, weight: .semibold) private let iconView: UIImageView /// 创建标题、图标与数值的紧凑展示。 init(title: String, symbol: String) { iconView = UIImageView(image: UIImage(systemName: symbol)) super.init(frame: .zero) backgroundColor = .white layer.cornerRadius = 7 let titleLabel = DroneDockStyle.label(title, size: 11, color: DroneDockStyle.secondary) titleLabel.textAlignment = .center iconView.contentMode = .scaleAspectFit let valueRow = UIStackView(arrangedSubviews: [iconView, valueLabel]) valueRow.axis = .horizontal valueRow.spacing = 6 valueRow.alignment = .center addSubview(titleLabel) addSubview(valueRow) iconView.snp.makeConstraints { make in make.size.equalTo(17) } titleLabel.snp.makeConstraints { make in make.top.equalToSuperview().offset(7) make.leading.trailing.equalToSuperview().inset(4) } valueRow.snp.makeConstraints { make in make.centerX.equalToSuperview() make.top.equalTo(titleLabel.snp.bottom).offset(4) make.leading.greaterThanOrEqualToSuperview().offset(4) make.trailing.lessThanOrEqualToSuperview().offset(-4) } valueLabel.adjustsFontSizeToFitWidth = true valueLabel.minimumScaleFactor = 0.8 isAccessibilityElement = true accessibilityLabel = title } @available(*, unavailable) required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } /// 更新数值和有效性,不根据电量自行推导执行许可。 func apply(value: String, isStale: Bool) { valueLabel.text = value valueLabel.textColor = isStale ? DroneDockStyle.secondary : DroneDockStyle.text iconView.tintColor = isStale ? DroneDockStyle.secondary : DroneDockStyle.primary accessibilityValue = value } } /// 安全控制按钮,双行文案分别表达指令及当前执行状态。 @MainActor final class DroneShootingTaskSafetyButton: UIButton { private let controlTitleLabel = DroneDockStyle.label(size: 15, weight: .semibold) private let controlSubtitleLabel = DroneDockStyle.label(size: 10) private let controlIconView: UIImageView /// 创建暂停或返航按钮;返回状态只由 ViewModel 决定。 init(symbol: String, color: UIColor, filled: Bool) { controlIconView = UIImageView(image: UIImage(systemName: symbol)) super.init(frame: .zero) layer.cornerRadius = 9 layer.borderWidth = filled ? 0 : 1 layer.borderColor = color.withAlphaComponent(0.6).cgColor backgroundColor = filled ? color : color.withAlphaComponent(0.05) controlTitleLabel.textColor = filled ? .white : color controlSubtitleLabel.textColor = filled ? UIColor.white.withAlphaComponent(0.85) : DroneDockStyle.secondary controlIconView.tintColor = filled ? .white : color controlIconView.contentMode = .scaleAspectFit let titles = UIStackView(arrangedSubviews: [controlTitleLabel, controlSubtitleLabel]) titles.axis = .vertical titles.spacing = 3 titles.alignment = .leading let content = UIStackView(arrangedSubviews: [controlIconView, titles]) content.axis = .horizontal content.spacing = 10 content.alignment = .center content.isUserInteractionEnabled = false addSubview(content) content.snp.makeConstraints { make in make.center.equalToSuperview() make.leading.greaterThanOrEqualToSuperview().offset(9) make.trailing.lessThanOrEqualToSuperview().offset(-9) } controlIconView.snp.makeConstraints { make in make.size.equalTo(27) } controlTitleLabel.adjustsFontSizeToFitWidth = true controlTitleLabel.minimumScaleFactor = 0.8 controlSubtitleLabel.adjustsFontSizeToFitWidth = true controlSubtitleLabel.minimumScaleFactor = 0.8 } @available(*, unavailable) required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } /// 应用语义明确的按钮标题、辅助说明及防重复点击状态。 func apply(title: String, subtitle: String, enabled: Bool) { controlTitleLabel.text = title controlSubtitleLabel.text = subtitle isEnabled = enabled alpha = enabled ? 1 : 0.42 accessibilityLabel = title accessibilityValue = subtitle } } /// 任务本地静帧的全屏放大页,明确标记画面来源。 @MainActor final class DroneShootingTaskExpandedFeedViewController: UIViewController { /// 显示同一张本地静帧;不建立视频连接。 override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = UIColor(hex: 0x141923) let imageView = UIImageView(image: UIImage(named: "drone_demo_tourists")) imageView.contentMode = .scaleAspectFill imageView.clipsToBounds = true imageView.accessibilityLabel = "游客取景示意,本地演示静帧" let caption = DroneDockStyle.label("演示画面 · 本地静帧 · 16:9", size: 12, color: UIColor(hex: 0xA7AFBE)) caption.textAlignment = .center let close = UIButton(type: .system) close.setImage(UIImage(systemName: "xmark"), for: .normal) close.tintColor = .white close.accessibilityLabel = "关闭放大画面" close.accessibilityIdentifier = "drone.task.closeExpandedFeed" close.addTarget(self, action: #selector(closeTapped), for: .touchUpInside) view.addSubview(imageView) view.addSubview(caption) view.addSubview(close) imageView.snp.makeConstraints { make in make.leading.trailing.centerY.equalToSuperview() make.height.equalTo(imageView.snp.width).multipliedBy(9.0 / 16.0) } caption.snp.makeConstraints { make in make.top.equalTo(imageView.snp.bottom).offset(16) make.leading.trailing.equalToSuperview().inset(20) } close.snp.makeConstraints { make in make.top.trailing.equalTo(view.safeAreaLayoutGuide).inset(12) make.size.equalTo(44) } } @objc private func closeTapped() { dismiss(animated: true) } }