import SnapKit import UIKit /// 对齐体验网页的任务失败组合:警示、原因、排查提示、重召占位及设备提示。 @MainActor final class DroneDockTaskFailureView: UIView { /// 点击原因区域查看完整后端说明,页面无需额外占用一个详情按钮。 var onDetails: (() -> Void)? /// 内部排版参考比例,不对视图或点击区域使用整体缩放。 private(set) var effectiveScale: CGFloat = 1 /// 当前是否使用适配固定小高度的紧凑排版。 private(set) var usesCompactLayout = false private let content = UIStackView() private let notice = UIButton(type: .custom) private let iconBackground = UIView() private let icon = UIImageView(image: UIImage(systemName: "exclamationmark.circle.fill")) private let body = UIStackView() private let titleLabel = DroneDockStyle.label(size: 23, weight: .semibold) private let reasonContainer = UIView() private let reasonLabel = DroneDockStyle.label(size: 14, color: UIColor(hex: 0xF53242)) private let adviceLabel = DroneDockStyle.label("请排查错误后再次召唤无人机。", size: 14, color: UIColor(hex: 0x6A79A0)) private let retryButton = UIButton(type: .system) private let noteLabel = DroneDockStyle.label(size: 12, color: UIColor(hex: 0x7E8BA9)) private var presentation: DroneDockTaskPresentation? private var contentTop: Constraint? private var noticeHeight: Constraint? private var iconSize: Constraint? private var bodyLeading: Constraint? private var titleHeight: Constraint? private var reasonHeight: Constraint? private var adviceHeight: Constraint? private var buttonHeight: 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(_ value: DroneDockTaskPresentation) { presentation = value titleLabel.text = value.title let reason = DroneDockHomeMetric.usableText(value.message).flatMap { $0 == "—" ? nil : $0 } ?? "服务端暂未提供具体原因" reasonLabel.text = reason.hasPrefix("失败原因:") ? reason : "失败原因:\(reason)" noteLabel.text = value.actionNote ?? "暂不可重新召唤,请返回准备页确认设备状态" notice.accessibilityLabel = [value.title, reasonLabel.text, adviceLabel.text].compactMap { $0 }.joined(separator: "。") setNeedsLayout() } /// 只收紧内部字号、行数和间距;主按钮保留至少 44 点高度。 override func layoutSubviews() { updateLayout() super.layoutSubviews() } private func setupUI() { accessibilityIdentifier = "drone.task.remote.failure" content.axis = .vertical content.spacing = 10 addSubview(content) content.addArrangedSubview(notice) content.addArrangedSubview(retryButton) content.addArrangedSubview(noteLabel) notice.accessibilityIdentifier = "drone.task.remote.failureDetails" notice.accessibilityHint = "查看完整失败原因" notice.addTarget(self, action: #selector(showDetails), for: .touchUpInside) notice.addSubview(iconBackground) iconBackground.backgroundColor = UIColor(hex: 0xFFEDED) iconBackground.isUserInteractionEnabled = false iconBackground.addSubview(icon) icon.tintColor = UIColor(hex: 0xFF303B) icon.contentMode = .scaleAspectFit body.axis = .vertical body.spacing = 5 body.isUserInteractionEnabled = false notice.addSubview(body) body.addArrangedSubview(titleLabel) body.addArrangedSubview(reasonContainer) body.addArrangedSubview(adviceLabel) titleLabel.textColor = UIColor(hex: 0x101523) titleLabel.accessibilityIdentifier = "drone.task.remote.failureTitle" reasonContainer.backgroundColor = UIColor(hex: 0xFFF0F2) reasonContainer.layer.cornerRadius = 6 reasonContainer.addSubview(reasonLabel) reasonLabel.accessibilityIdentifier = "drone.task.remote.failureReason" adviceLabel.accessibilityIdentifier = "drone.task.remote.failureAdvice" noteLabel.accessibilityIdentifier = "drone.task.remote.failureNote" noteLabel.textAlignment = .center [titleLabel, reasonLabel, adviceLabel, noteLabel].forEach { $0.lineBreakMode = .byTruncatingTail } var configuration = UIButton.Configuration.filled() configuration.title = "重新召唤无人机" configuration.image = UIImage(systemName: "arrow.triangle.2.circlepath") configuration.imagePadding = 12 configuration.preferredSymbolConfigurationForImage = UIImage.SymbolConfiguration(pointSize: 21, weight: .regular) configuration.background.backgroundColor = UIColor(hex: 0xE8EBF1) configuration.background.cornerRadius = 8 configuration.baseForegroundColor = UIColor(hex: 0x939CB0) configuration.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 8, bottom: 0, trailing: 8) retryButton.configuration = configuration retryButton.configurationUpdateHandler = { button in button.configuration?.baseForegroundColor = UIColor(hex: 0x939CB0) button.configuration?.background.backgroundColor = UIColor(hex: 0xE8EBF1) } retryButton.isEnabled = false retryButton.accessibilityIdentifier = "drone.task.remote.failureRetry" } private func setupConstraints() { content.snp.makeConstraints { make in contentTop = make.top.equalToSuperview().offset(16).constraint make.leading.trailing.equalToSuperview().inset(18) } notice.snp.makeConstraints { make in noticeHeight = make.height.equalTo(90).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.equalToSuperview().multipliedBy(24.0 / 41.0) } body.snp.makeConstraints { make in make.top.trailing.equalToSuperview() bodyLeading = make.leading.equalToSuperview().offset(53).constraint } titleLabel.snp.makeConstraints { make in titleHeight = make.height.equalTo(30).constraint } reasonLabel.snp.makeConstraints { make in make.leading.trailing.equalToSuperview().inset(8); make.centerY.equalToSuperview() } reasonContainer.snp.makeConstraints { make in reasonHeight = make.height.equalTo(30).priority(999).constraint } adviceLabel.snp.makeConstraints { make in adviceHeight = make.height.equalTo(22).priority(999).constraint } retryButton.snp.makeConstraints { make in buttonHeight = make.height.equalTo(50).constraint } noteLabel.snp.makeConstraints { make in noteHeight = make.height.equalTo(18).constraint } } private func updateLayout() { guard let presentation, bounds.width > 0, bounds.height > 0 else { return } let width = max(1, bounds.width - 36) let naturalReason = textHeight(reasonLabel.text, font: .systemFont(ofSize: 14), width: width - 53 - 16, lines: 3) + 10 let naturalHeight: CGFloat = 30 + naturalReason + 22 + 10 + 50 + 18 + 14 + 32 effectiveScale = min(1, max(0.8, bounds.height / naturalHeight)) usesCompactLayout = bounds.height < 190 let compact = usesCompactLayout let extreme = bounds.height < 120 let scale = compact ? 0.8 : effectiveScale let top: CGFloat = compact ? 6 : 16 * scale let gap: CGFloat = compact ? 3 : 5 * scale let diameter: CGFloat = compact ? 32 : 41 * scale let iconGap: CGFloat = compact ? 8 : 12 * scale let textWidth = max(1, width - diameter - iconGap) titleLabel.font = .systemFont(ofSize: compact ? 20 : max(20, 23 * scale), weight: .semibold) reasonLabel.font = .systemFont(ofSize: max(12, 14 * scale)) adviceLabel.font = .systemFont(ofSize: max(12, 14 * scale)) titleLabel.numberOfLines = 1 reasonLabel.numberOfLines = compact ? 1 : 3 adviceLabel.numberOfLines = 1 noteLabel.numberOfLines = 1 adviceLabel.isHidden = extreme let title = textHeight(presentation.title, font: titleLabel.font, width: textWidth, lines: 1) let reasonPadding: CGFloat = compact ? 6 : 10 * scale var reason = textHeight(reasonLabel.text, font: reasonLabel.font, width: textWidth - 16, lines: reasonLabel.numberOfLines) + reasonPadding let advice = extreme ? 0 : textHeight(adviceLabel.text, font: adviceLabel.font, width: textWidth, lines: 1) let note = textHeight(noteLabel.text, font: noteLabel.font, width: width, lines: 1) let actionHeight: CGFloat = compact ? 44 : max(44, 50 * scale) let actionGap: CGFloat = compact ? 5 : 10 * scale let noteGap: CGFloat = compact ? 3 : 4 var bodyHeight: CGFloat { title + reason + advice + gap * (extreme ? 1 : 2) } // 长原因先限制可见行数,完整文本仍可点开;不挤压固定页脚或按钮。 while top * 2 + max(44, bodyHeight) + actionGap + actionHeight + noteGap + note > bounds.height, reasonLabel.numberOfLines > 1 { reasonLabel.numberOfLines -= 1 reason = textHeight(reasonLabel.text, font: reasonLabel.font, width: textWidth - 16, lines: reasonLabel.numberOfLines) + reasonPadding } contentTop?.update(offset: top) noticeHeight?.update(offset: max(44, bodyHeight)) iconSize?.update(offset: diameter) iconBackground.layer.cornerRadius = diameter / 2 bodyLeading?.update(offset: diameter + iconGap) titleHeight?.update(offset: title) reasonHeight?.update(offset: reason) adviceHeight?.update(offset: max(1, advice)) buttonHeight?.update(offset: actionHeight) noteHeight?.update(offset: note) body.spacing = gap content.setCustomSpacing(actionGap, after: notice) content.setCustomSpacing(noteGap, after: retryButton) retryButton.configuration?.titleTextAttributesTransformer = UIConfigurationTextAttributesTransformer { attributes in var result = attributes result.font = .systemFont(ofSize: compact ? 16 : max(16, 18 * scale), weight: .semibold) return result } } private func textHeight(_ text: String?, font: UIFont, width: CGFloat, lines: Int) -> CGFloat { 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(max(font.lineHeight, height), font.lineHeight * CGFloat(lines))) } @objc private func showDetails() { onDetails?() } }