import SnapKit import UIKit /// 已到达待开拍面板:蓝色到达提示、分级文案及整行开拍按钮,倒计时只参与显示。 @MainActor final class DroneDockTaskReadyView: 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(image: UIImage(systemName: "checkmark.circle.fill")) private let titleLabel = DroneDockStyle.label(size: 23, weight: .semibold, color: UIColor(hex: 0x101523)) private let promptLabel = DroneDockStyle.label(size: 18, weight: .medium, color: UIColor(hex: 0x101523)) private let messageLabel = DroneDockStyle.label(size: 14, color: UIColor(hex: 0x6A79A0)) private let actionButton = UIButton(type: .system) private let activityIndicator = UIActivityIndicatorView(style: .medium) private var presentation: DroneDockTaskPresentation? private var countdownSeconds: Int? 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 promptTop: Constraint? private var promptHeight: Constraint? private var messageTop: Constraint? private var messageHeight: Constraint? private var actionTop: Constraint? private var actionHeight: 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, isPrimaryActionLoading: Bool) { self.presentation = presentation self.isPrimaryActionLoading = isPrimaryActionLoading titleLabel.text = presentation.title messageLabel.text = presentation.message actionButton.isHidden = presentation.action == .none || DroneDockHomeMetric.usableText(presentation.actionTitle) == nil actionButton.isEnabled = presentation.isActionEnabled && !actionButton.isHidden && !isRefreshing && !isPrimaryActionLoading actionButton.configuration?.image = UIImage(systemName: presentation.action == .refresh ? "arrow.clockwise" : "camera") countdownSeconds = presentation.action == .start ? presentation.countdownSeconds.map { max(0, $0) } : nil updateActionTitle() if isPrimaryActionLoading { activityIndicator.startAnimating() } else { activityIndicator.stopAnimating() } actionButton.accessibilityValue = isPrimaryActionLoading ? "处理中" : nil setIdentifiersActive(identifiersActive) actionButton.setNeedsUpdateConfiguration() setNeedsLayout() } /// 更新已有按钮的数字,不改变权限、尺寸或触发任何业务回调。 func updateCountdown(seconds: Int?) { countdownSeconds = presentation?.action == .start ? seconds.map { max(0, $0) } : nil updateActionTitle() } /// 仅当前可见面板暴露公共标识,隐藏时清除数字及加载动画。 func setIdentifiersActive(_ active: Bool) { identifiersActive = active if !active { activityIndicator.stopAnimating() updateCountdown(seconds: nil) } titleLabel.accessibilityIdentifier = active ? "drone.task.remote.statusTitle" : nil promptLabel.accessibilityIdentifier = active ? "drone.task.remote.readyPrompt" : nil messageLabel.accessibilityIdentifier = active ? "drone.task.remote.statusMessage" : nil actionButton.accessibilityIdentifier = active ? (presentation?.action == .refresh ? "drone.task.remote.refresh" : "drone.task.remote.primary") : nil activityIndicator.accessibilityIdentifier = active ? "drone.task.remote.primary.loading" : nil } /// 在固定区域内压缩排版,始终保留至少 44 点的主按钮。 override func layoutSubviews() { updateContentLayout() super.layoutSubviews() } private func setupUI() { accessibilityIdentifier = "drone.task.remote.ready" backgroundColor = .white addSubview(content) [header, actionButton].forEach(content.addSubview) [iconBackground, titleLabel, promptLabel, messageLabel].forEach(header.addSubview) iconBackground.addSubview(icon) iconBackground.accessibilityIdentifier = "drone.task.remote.readyIcon" iconBackground.backgroundColor = UIColor(hex: 0xE2EDFF) iconBackground.layer.cornerRadius = 20.5 icon.tintColor = UIColor(hex: 0x0671FF) icon.contentMode = .scaleAspectFit titleLabel.accessibilityTraits = .header titleLabel.numberOfLines = 1 promptLabel.text = "是否开始拍摄?" promptLabel.numberOfLines = 1 messageLabel.numberOfLines = 2 messageLabel.lineBreakMode = .byTruncatingTail var configuration = UIButton.Configuration.filled() configuration.imagePadding = 12 configuration.preferredSymbolConfigurationForImage = UIImage.SymbolConfiguration(pointSize: 24, weight: .regular) 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 foreground = UIColor.white configuration.background.backgroundColor = UIColor(hex: 0x0671FF) .withAlphaComponent(button.isEnabled || self.isPrimaryActionLoading ? 1 : 0.35) configuration.baseForegroundColor = foreground configuration.imageColorTransformer = UIConfigurationColorTransformer { _ in foreground } configuration.titleTextAttributesTransformer = UIConfigurationTextAttributesTransformer { attributes in var result = attributes result.font = .monospacedDigitSystemFont(ofSize: self.usesCompactLayout ? 16 : 18, weight: .semibold) result.foregroundColor = foreground return result } button.configuration = configuration } actionButton.addTarget(self, action: #selector(primaryTapped), for: .touchUpInside) activityIndicator.color = .white activityIndicator.hidesWhenStopped = true activityIndicator.isUserInteractionEnabled = false actionButton.addSubview(activityIndicator) } 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(86).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(30).constraint } promptLabel.snp.makeConstraints { make in promptTop = make.top.equalTo(titleLabel.snp.bottom).offset(4).constraint make.leading.trailing.equalTo(titleLabel) promptHeight = make.height.equalTo(26).constraint } messageLabel.snp.makeConstraints { make in messageTop = make.top.equalTo(promptLabel.snp.bottom).offset(3).constraint make.leading.trailing.equalTo(titleLabel) messageHeight = make.height.equalTo(23).constraint } actionButton.snp.makeConstraints { make in actionTop = make.top.equalTo(header.snp.bottom).offset(10).constraint make.leading.trailing.bottom.equalToSuperview() actionHeight = make.height.equalTo(50).constraint } activityIndicator.snp.makeConstraints { make in make.centerY.equalToSuperview() make.trailing.equalToSuperview().inset(12) make.size.equalTo(20) } } private func updateActionTitle() { guard let presentation else { return } var title = DroneDockHomeMetric.usableText(presentation.actionTitle) if let seconds = countdownSeconds, let base = title { title = "\(base)(\(seconds)s)" } UIView.performWithoutAnimation { actionButton.setTitle(title, for: .normal) actionButton.configuration?.title = title actionButton.layoutIfNeeded() } } private func updateContentLayout() { guard let presentation, bounds.width > 0, bounds.height > 0 else { return } let wasCompact = usesCompactLayout usesCompactLayout = bounds.height < 190 let compact = usesCompactLayout let extreme = bounds.height < 120 let diameter: CGFloat = extreme ? 24 : compact ? 32 : 41 let iconGap: CGFloat = extreme ? 8 : compact ? 10 : 12 let titleSize: CGFloat = extreme ? 17 : compact ? 20 : 23 let promptSize: CGFloat = extreme ? 15 : compact ? 17 : 18 let messageSize: CGFloat = compact ? 13 : 14 let buttonHeight: CGFloat = compact ? 44 : 50 let buttonGap: CGFloat = extreme ? 6 : 10 let available = max(1, bounds.height - (extreme ? 8 : 12)) titleLabel.font = .systemFont(ofSize: titleSize, weight: .semibold) promptLabel.font = .systemFont(ofSize: promptSize, weight: .medium) messageLabel.font = .systemFont(ofSize: messageSize) let title = max(ceil(titleLabel.font.lineHeight), compact ? 0 : 30) var prompt = max(ceil(promptLabel.font.lineHeight), compact ? 0 : 26) var firstGap: CGFloat = compact ? 2 : 4 let secondGap: CGFloat = 3 let textWidth = max(1, bounds.width - 36 - diameter - iconGap) let paragraph = NSMutableParagraphStyle() paragraph.minimumLineHeight = messageSize * 1.6 paragraph.maximumLineHeight = messageSize * 1.6 let attributes: [NSAttributedString.Key: Any] = [ .font: messageLabel.font as Any, .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 var lines = extreme || presentation.message.isEmpty ? 0 : 2 var body = lines == 0 ? 0 : ceil(min(measured, paragraph.maximumLineHeight * CGFloat(lines))) while title + firstGap + prompt + (lines > 0 ? secondGap + body : 0) + buttonGap + buttonHeight > available, lines > 0 { lines -= 1 body = lines == 0 ? 0 : ceil(min(measured, paragraph.maximumLineHeight * CGFloat(lines))) } if title + firstGap + prompt + buttonGap + buttonHeight > available { prompt = 0 firstGap = 0 } promptLabel.isHidden = prompt == 0 messageLabel.isHidden = lines == 0 messageLabel.numberOfLines = max(1, lines) effectiveScale = compact ? 0.8 : 1 headerHeight?.update(offset: max(diameter, title + firstGap + prompt + (lines > 0 ? secondGap + body : 0))) iconSize?.update(offset: diameter) iconBackground.layer.cornerRadius = diameter / 2 textLeading?.update(offset: iconGap) titleHeight?.update(offset: title) promptTop?.update(offset: firstGap) promptHeight?.update(offset: prompt) messageTop?.update(offset: lines > 0 ? secondGap : 0) messageHeight?.update(offset: body) actionTop?.update(offset: buttonGap) actionHeight?.update(offset: buttonHeight) if wasCompact != usesCompactLayout { actionButton.setNeedsUpdateConfiguration() } } @objc private func primaryTapped() { guard !isHidden, actionButton.isEnabled, !actionButton.isHidden else { return } onPrimaryAction?() } }