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 deviceLabel = DroneDockStyle.label(size: 12, color: DroneDockStyle.secondary) private let actionRow = UIView() private let primaryButton = UIButton(type: .system) private let detailsButton = UIButton(type: .system) private let failureView = DroneDockTaskFailureView() private var presentation: DroneDockTaskPresentation? 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 let isFailure = presentation.phase == .failed contentStack.isHidden = isFailure failureView.isHidden = !isFailure if isFailure { failureView.apply(presentation) 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() } /// 尺寸改变时仅重排内部内容,保留至少 44 点操作区域,不使用 transform 缩放。 override func layoutSubviews() { if presentation?.phase == .failed { super.layoutSubviews() failureView.layoutIfNeeded() effectiveScale = failureView.effectiveScale usesCompactLayout = failureView.usesCompactLayout return } updateContentLayout() super.layoutSubviews() } private func setupUI() { backgroundColor = .white addSubview(failureView) failureView.isHidden = true failureView.onDetails = { [weak self] in self?.onDetails?() } contentStack.axis = .vertical contentStack.alignment = .fill contentStack.spacing = 10 addSubview(contentStack) contentStack.addArrangedSubview(titleRow) contentStack.addArrangedSubview(messageLabel) 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" deviceLabel.numberOfLines = 1 deviceLabel.lineBreakMode = .byTruncatingTail deviceLabel.accessibilityIdentifier = "drone.task.remote.deviceStatus" actionRow.addSubview(primaryButton) actionRow.addSubview(detailsButton) 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) detailsButton.setTitle("详情", for: .normal) detailsButton.setTitleColor(DroneDockStyle.primary, for: .normal) detailsButton.titleLabel?.font = .systemFont(ofSize: 13, weight: .medium) detailsButton.backgroundColor = DroneDockStyle.background detailsButton.layer.cornerRadius = 9 detailsButton.accessibilityIdentifier = "drone.task.remote.details" detailsButton.accessibilityLabel = "查看完整任务状态" detailsButton.addTarget(self, action: #selector(detailsTapped), for: .touchUpInside) } private func setupConstraints() { 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 } deviceLabel.snp.makeConstraints { make in deviceHeight = make.height.equalTo(16).priority(999).constraint } actionRow.snp.makeConstraints { make in make.height.equalTo(44) } detailsButton.snp.makeConstraints { make in make.top.trailing.bottom.equalToSuperview() make.width.equalTo(64) } primaryButton.snp.makeConstraints { make in make.top.leading.bottom.equalToSuperview() make.trailing.equalTo(detailsButton.snp.leading).offset(-8) } } private func updateContentLayout() { guard let presentation, bounds.width > 0, bounds.height > 0 else { return } let isExtreme = bounds.height < 120 let horizontalInset: CGFloat = isExtreme ? 12 : 16 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) 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 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: 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 ? 4 : 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 + deviceTextHeight + 44 + CGFloat(1 + (messageLabel.isHidden ? 0 : 1) + (deviceLabel.isHidden ? 0 : 1)) * 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 .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?() } @objc private func detailsTapped() { onDetails?() } }