import SnapKit import UIKit /// 暂停、返航及回巢面板,使用本地状态文案及整行主按钮,不显示设备摘要。 @MainActor final class DroneDockTaskControlStateView: UIView { /// 用户点击已允许的主操作时交给任务页面执行。 var onPrimaryAction: (() -> Void)? /// 当前固定区域使用的排版比例,不通过 transform 缩小点击区域。 private(set) var effectiveScale: CGFloat = 1 /// 可用高度不足时收紧图标、字号与间距。 private(set) var usesCompactLayout = false private let content = UIView() private let header = UIView() private let iconBackground = UIView() private let icon = UIImageView() private let titleLabel = DroneDockStyle.label(size: 22, weight: .semibold, color: UIColor(hex: 0x101523)) private let messageLabel = DroneDockStyle.label(size: 14, color: UIColor(hex: 0x6A79A0)) private let actionButton = UIButton(type: .system) private let actionActivityIndicator = UIActivityIndicatorView(style: .medium) private let noteLabel = DroneDockStyle.label(size: 12, color: DroneDockStyle.secondary) private var presentation: DroneDockTaskPresentation? private var identifiersActive = false private var isPrimaryActionLoading = false private var headerHeight: Constraint? private var iconSize: Constraint? private var textLeading: Constraint? private var titleHeight: Constraint? private var messageTop: Constraint? private var messageHeight: Constraint? private var actionTop: Constraint? private var actionHeight: Constraint? private var noteTop: Constraint? private var noteHeight: Constraint? /// 创建与任务固定状态区域配合的控制状态视图。 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(_ presentation: DroneDockTaskPresentation, isRefreshing: Bool = false, isPrimaryActionLoading: Bool = false) { self.presentation = presentation self.isPrimaryActionLoading = isPrimaryActionLoading titleLabel.text = presentation.title messageLabel.text = presentation.message noteLabel.text = presentation.actionNote switch presentation.phase { case .paused: icon.image = UIImage(systemName: "pause.fill") case .returning: icon.image = UIImage(systemName: "arrow.uturn.backward") case .returned: icon.image = UIImage(systemName: "house.fill") default: icon.image = UIImage(systemName: "clock") } actionButton.isEnabled = presentation.action != .none && presentation.isActionEnabled && !isRefreshing && !isPrimaryActionLoading actionButton.configuration?.title = presentation.actionTitle let actionSymbol: String? = switch presentation.action { case .resume: "play.fill" case .retry: "paperplane.fill" case .refresh: "arrow.clockwise" default: nil } actionButton.configuration?.image = actionSymbol.flatMap { UIImage(systemName: $0) } if isPrimaryActionLoading { actionActivityIndicator.startAnimating() } else { actionActivityIndicator.stopAnimating() } actionButton.accessibilityValue = isPrimaryActionLoading ? "处理中" : nil setIdentifiersActive(identifiersActive) actionButton.setNeedsUpdateConfiguration() setNeedsLayout() } /// 只有可见面板暴露通用标识,状态切换后不会匹配到隐藏的旧按钮。 func setIdentifiersActive(_ active: Bool) { identifiersActive = active if !active { actionActivityIndicator.stopAnimating() } titleLabel.accessibilityIdentifier = active ? "drone.task.remote.statusTitle" : nil messageLabel.accessibilityIdentifier = active ? "drone.task.remote.statusMessage" : nil actionButton.accessibilityIdentifier = active ? (presentation?.action == .refresh ? "drone.task.remote.refresh" : "drone.task.remote.primary") : nil actionActivityIndicator.accessibilityIdentifier = active ? "drone.task.remote.primary.loading" : nil } /// 在固定区域内收紧排版,主按钮始终至少 44 点高。 override func layoutSubviews() { updateContentLayout() super.layoutSubviews() } private func setupUI() { accessibilityIdentifier = "drone.task.remote.controlState" backgroundColor = .white addSubview(content) [header, actionButton, noteLabel].forEach(content.addSubview) [iconBackground, titleLabel, messageLabel].forEach(header.addSubview) iconBackground.addSubview(icon) iconBackground.backgroundColor = UIColor(hex: 0xFFF3E0) iconBackground.layer.cornerRadius = 20.5 icon.contentMode = .scaleAspectFit icon.tintColor = UIColor(hex: 0xF39819) titleLabel.accessibilityTraits = .header titleLabel.numberOfLines = 1 titleLabel.lineBreakMode = .byTruncatingTail messageLabel.numberOfLines = 3 messageLabel.lineBreakMode = .byTruncatingTail noteLabel.numberOfLines = 1 noteLabel.textAlignment = .center noteLabel.accessibilityIdentifier = "drone.task.remote.controlNote" var configuration = UIButton.Configuration.filled() configuration.imagePadding = 10 configuration.preferredSymbolConfigurationForImage = UIImage.SymbolConfiguration(pointSize: 20, weight: .semibold) configuration.background.cornerRadius = 8 configuration.cornerStyle = .fixed configuration.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 8, bottom: 0, trailing: 8) actionButton.configuration = configuration actionButton.configurationUpdateHandler = { [weak self] button in guard let self, var configuration = button.configuration else { return } let emphasizesAction = button.isEnabled || self.isPrimaryActionLoading let foreground = emphasizesAction ? UIColor.white : UIColor(hex: 0x929BAE) let font = UIFont.systemFont(ofSize: self.usesCompactLayout ? 16 : 18, weight: .semibold) configuration.background.backgroundColor = emphasizesAction ? DroneDockStyle.primary : UIColor(hex: 0xEDF0F5) configuration.baseForegroundColor = foreground configuration.imageColorTransformer = UIConfigurationColorTransformer { _ in foreground } // loading 会禁用按钮;显式设置最终文字色,避免系统禁用态再次把白字变暗。 configuration.titleTextAttributesTransformer = UIConfigurationTextAttributesTransformer { attributes in var result = attributes result.font = font result.foregroundColor = foreground return result } button.configuration = configuration } actionActivityIndicator.color = .white actionActivityIndicator.hidesWhenStopped = true actionActivityIndicator.isUserInteractionEnabled = false actionButton.addSubview(actionActivityIndicator) actionButton.addTarget(self, action: #selector(primaryTapped), for: .touchUpInside) } private func setupConstraints() { content.snp.makeConstraints { make in make.leading.trailing.equalToSuperview().inset(18) make.centerY.equalToSuperview() } header.snp.makeConstraints { make in make.top.leading.trailing.equalToSuperview() headerHeight = make.height.equalTo(81).constraint } iconBackground.snp.makeConstraints { make in make.top.leading.equalToSuperview() iconSize = make.size.equalTo(41).constraint } icon.snp.makeConstraints { make in make.center.equalToSuperview() make.size.equalTo(iconBackground.snp.width).multipliedBy(24.0 / 41.0) } titleLabel.snp.makeConstraints { make in make.top.trailing.equalToSuperview() textLeading = make.leading.equalTo(iconBackground.snp.trailing).offset(12).constraint titleHeight = make.height.equalTo(28).constraint } messageLabel.snp.makeConstraints { make in messageTop = make.top.equalTo(titleLabel.snp.bottom).offset(6).constraint make.leading.trailing.equalTo(titleLabel) messageHeight = make.height.equalTo(45).constraint } actionButton.snp.makeConstraints { make in actionTop = make.top.equalTo(header.snp.bottom).offset(16).constraint make.leading.trailing.equalToSuperview() actionHeight = make.height.equalTo(50).constraint } actionActivityIndicator.snp.makeConstraints { make in make.centerY.equalToSuperview() make.trailing.equalToSuperview().inset(14) make.size.equalTo(20) } noteLabel.snp.makeConstraints { make in noteTop = make.top.equalTo(actionButton.snp.bottom).offset(6).constraint make.leading.trailing.bottom.equalToSuperview() noteHeight = make.height.equalTo(16).constraint } } private func updateContentLayout() { guard let presentation, bounds.width > 0, bounds.height > 0 else { return } let wasCompact = usesCompactLayout usesCompactLayout = bounds.height < 190 let extreme = bounds.height < 120 let compact = usesCompactLayout let diameter: CGFloat = extreme ? 24 : compact ? 32 : 41 let iconGap: CGFloat = extreme ? 8 : compact ? 10 : 12 let titleSize: CGFloat = extreme ? 17 : compact ? 20 : 22 let messageSize: CGFloat = compact ? 13 : 14 let textGap: CGFloat = extreme ? 3 : compact ? 4 : 6 let buttonGap: CGFloat = extreme ? 8 : compact ? 12 : 16 let buttonHeight: CGFloat = compact ? 44 : 50 let available = max(1, bounds.height - (extreme ? 8 : 12)) let textWidth = max(1, bounds.width - 36 - diameter - iconGap) titleLabel.font = .systemFont(ofSize: titleSize, weight: .semibold) let title = ceil(titleLabel.font.lineHeight) let paragraph = NSMutableParagraphStyle() paragraph.minimumLineHeight = messageSize * 1.5 paragraph.maximumLineHeight = messageSize * 1.5 let attributes: [NSAttributedString.Key: Any] = [ .font: UIFont.systemFont(ofSize: messageSize), .foregroundColor: UIColor(hex: 0x6A79A0), .paragraphStyle: paragraph ] messageLabel.attributedText = NSAttributedString(string: presentation.message, attributes: attributes) let measured = (presentation.message as NSString).boundingRect( with: CGSize(width: textWidth, height: .greatestFiniteMagnitude), options: [.usesLineFragmentOrigin, .usesFontLeading], attributes: attributes, context: nil).height let hasNote = !extreme && DroneDockHomeMetric.usableText(presentation.actionNote) != nil let note = hasNote ? ceil(noteLabel.font.lineHeight) : 0 let noteGap: CGFloat = hasNote ? 6 : 0 var lines = extreme || presentation.message.isEmpty ? 0 : 3 var body = lines == 0 ? 0 : ceil(min(measured, paragraph.maximumLineHeight * CGFloat(lines))) while max(diameter, title + (lines > 0 ? textGap + body : 0)) + buttonGap + buttonHeight + note + noteGap > available, lines > 0 { lines -= 1 body = lines == 0 ? 0 : ceil(min(measured, paragraph.maximumLineHeight * CGFloat(lines))) } messageLabel.numberOfLines = max(1, lines) messageLabel.isHidden = lines == 0 noteLabel.isHidden = !hasNote effectiveScale = compact ? 0.8 : 1 headerHeight?.update(offset: max(diameter, title + (lines > 0 ? textGap + body : 0))) iconSize?.update(offset: diameter) iconBackground.layer.cornerRadius = diameter / 2 textLeading?.update(offset: iconGap) titleHeight?.update(offset: title) messageTop?.update(offset: lines > 0 ? textGap : 0) messageHeight?.update(offset: body) actionTop?.update(offset: buttonGap) actionHeight?.update(offset: buttonHeight) noteTop?.update(offset: noteGap) noteHeight?.update(offset: note) if wasCompact != usesCompactLayout { actionButton.setNeedsUpdateConfiguration() } } @objc private func primaryTapped() { guard !isHidden, actionButton.isEnabled else { return } onPrimaryAction?() } }