Files
suixinkan_uikit/suixinkan/UI/DroneDock/DroneDockDeviceCardView.swift
T
lujiuyin 40060cd883 feat(drone-dock): 接入首页真实数据并完善设备卡片
通过 airport/home 读取当前景区和门店的设备与统计,隔离失效会话回包,未知遥测显示破折号。完善状态圆点、分割线和卫星图标;订单详情等待后端测试数据。

验证:iPhone 11 上 49 项相关测试、样式调整后 9 项界面回归,以及测试样例匿名化后 20 项测试均通过。真实联调响应、日志和截图仅保留在本地。
2026-10-27 15:05:51 +08:00

261 lines
12 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import SnapKit
import UIKit
/// 设备遥测卡:只展示设备状态,横向浏览不会选择执行拍摄的无人机。
final class DroneDockDeviceCardView: UIView {
private let nameLabel = DroneDockStyle.label(size: 15, weight: .semibold)
private let statusLabel = DroneDockDeviceStatusView()
private let batteryView = DroneDockBatteryView()
private let signalValue = DroneDockStyle.label(size: 19, weight: .semibold)
private let satelliteValue = DroneDockStyle.label(size: 19, weight: .semibold)
private let temperatureValue = DroneDockStyle.label(size: 22, weight: .semibold)
private let windValue = DroneDockStyle.label(size: 22, weight: .semibold)
private let rainValue = DroneDockStyle.label(size: 22, weight: .semibold)
/// 创建六项遥测布局。
override init(frame: CGRect) {
super.init(frame: frame)
DroneDockStyle.applyCard(to: self)
layer.shadowColor = UIColor.black.cgColor
layer.shadowOpacity = 0.045
layer.shadowRadius = 8
layer.shadowOffset = CGSize(width: 0, height: 3)
setupUI()
}
@available(*, unavailable)
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
/// 未知读数由模型提供破折号,不以零代替未知。
func apply(device: DroneDockDevice) {
nameLabel.text = device.name
statusLabel.apply(title: device.state.title, tone: device.state.tone)
batteryView.apply(telemetry: device.telemetry)
signalValue.text = device.telemetry.signalText
satelliteValue.text = device.telemetry.satelliteText
temperatureValue.text = device.telemetry.temperatureText
windValue.text = device.telemetry.windText
rainValue.text = device.telemetry.rainText
accessibilityIdentifier = "drone.device.\(device.id)"
accessibilityLabel = "\(device.name),\(device.state.title)"
}
/// 展示 home 返回的机巢名称、工作状态及格式化读数,不根据展示状态推断起飞许可。
func apply(airport: DroneDockHomeAirport) {
nameLabel.text = airport.name
statusLabel.apply(title: airport.statusTitle, tone: airport.statusTone)
batteryView.apply(text: airport.metricText(for: "battery"), progress: airport.batteryProgress)
temperatureValue.text = airport.metricText(for: "env_temp")
windValue.text = airport.metricText(for: "wind_speed")
rainValue.text = airport.metricText(for: "rainfall")
signalValue.text = airport.metricText(for: "signal")
satelliteValue.text = airport.metricText(for: "satellite")
accessibilityIdentifier = "drone.device.\(airport.id)"
accessibilityLabel = "\(airport.name),\(airport.statusTitle)"
}
private func setupUI() {
temperatureValue.accessibilityIdentifier = "drone.metric.env_temp"
windValue.accessibilityIdentifier = "drone.metric.wind_speed"
rainValue.accessibilityIdentifier = "drone.metric.rainfall"
signalValue.accessibilityIdentifier = "drone.metric.signal"
satelliteValue.accessibilityIdentifier = "drone.metric.satellite"
[nameLabel, statusLabel, batteryView].forEach(addSubview)
nameLabel.snp.makeConstraints { make in
make.top.leading.equalToSuperview().inset(16)
make.trailing.lessThanOrEqualTo(statusLabel.snp.leading).offset(-8)
}
statusLabel.snp.makeConstraints { make in
make.centerY.equalTo(nameLabel)
make.trailing.equalToSuperview().inset(16)
}
batteryView.snp.makeConstraints { make in
make.leading.equalToSuperview().inset(20)
make.top.equalTo(nameLabel.snp.bottom).offset(16)
make.size.equalTo(102)
}
let signal = telemetryRow(symbol: "antenna.radiowaves.left.and.right", title: "信号状态", value: signalValue)
let satellites = telemetryRow(image: UIImage(named: "drone_telemetry_satellite"), title: "搜星状态", value: satelliteValue)
let radioStack = UIStackView(arrangedSubviews: [signal, satellites])
radioStack.axis = .vertical
radioStack.distribution = .fillEqually
radioStack.spacing = 12
addSubview(radioStack)
radioStack.snp.makeConstraints { make in
make.leading.equalTo(batteryView.snp.trailing).offset(20)
make.trailing.equalToSuperview().inset(16)
make.centerY.equalTo(batteryView)
make.height.equalTo(92)
}
let divider = UIView()
divider.backgroundColor = DroneDockStyle.border
addSubview(divider)
divider.snp.makeConstraints { make in
make.top.equalTo(batteryView.snp.bottom).offset(15)
make.leading.trailing.equalToSuperview().inset(16)
make.height.equalTo(0.5)
}
let metricColumns = [
metric(value: temperatureValue, symbol: "thermometer.medium", title: "环境温度"),
metric(value: windValue, symbol: "wind", title: "风速"),
metric(value: rainValue, symbol: "cloud.rain", title: "雨量")
]
let metrics = UIStackView(arrangedSubviews: metricColumns)
metrics.axis = .horizontal
metrics.distribution = .fillEqually
addSubview(metrics)
metrics.snp.makeConstraints { make in
make.top.equalTo(divider.snp.bottom).offset(13)
make.leading.trailing.equalToSuperview().inset(12)
make.bottom.equalToSuperview().inset(14)
}
for column in metricColumns.dropFirst() {
let separator = UIView()
separator.backgroundColor = DroneDockStyle.border
metrics.addSubview(separator)
separator.snp.makeConstraints { make in
make.leading.equalTo(column.snp.leading)
make.top.bottom.equalToSuperview()
make.width.equalTo(0.5)
}
}
}
private func telemetryRow(symbol: String, title: String, value: UILabel) -> UIView {
telemetryRow(image: UIImage(systemName: symbol), title: title, value: value)
}
private func telemetryRow(image: UIImage?, title: String, value: UILabel) -> UIView {
let icon = UIImageView(image: image?.withRenderingMode(.alwaysTemplate))
icon.tintColor = DroneDockStyle.secondary
icon.contentMode = .scaleAspectFit
let label = DroneDockStyle.label(title, size: 11, color: DroneDockStyle.secondary)
value.adjustsFontSizeToFitWidth = true
value.minimumScaleFactor = 0.7
value.textAlignment = .right
value.setContentHuggingPriority(.required, for: .horizontal)
let row = UIStackView(arrangedSubviews: [icon, label, value])
row.spacing = 5
row.alignment = .center
icon.snp.makeConstraints { make in make.width.equalTo(16); make.height.equalTo(16) }
label.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
return row
}
private func metric(value: UILabel, symbol: String, title: String) -> UIView {
value.textAlignment = .center
value.adjustsFontSizeToFitWidth = true
value.minimumScaleFactor = 0.6
let icon = UIImageView(image: UIImage(systemName: symbol))
icon.tintColor = DroneDockStyle.secondary
icon.contentMode = .scaleAspectFit
icon.snp.makeConstraints { make in make.size.equalTo(13) }
let caption = UIStackView(arrangedSubviews: [icon, DroneDockStyle.label(title, size: 11, color: DroneDockStyle.secondary)])
caption.spacing = 4
caption.alignment = .center
let column = UIStackView(arrangedSubviews: [value, caption])
column.axis = .vertical
column.alignment = .center
column.spacing = 5
return column
}
}
/// 设备专用状态标签,圆点与文字共用语义颜色,不改变其他业务标签。
private final class DroneDockDeviceStatusView: UIView {
private let dot = UIView()
private let titleLabel = DroneDockStyle.label(size: 11, weight: .medium)
override init(frame: CGRect) {
super.init(frame: frame)
layer.cornerRadius = 6
dot.layer.cornerRadius = 3
dot.snp.makeConstraints { $0.size.equalTo(6) }
let content = UIStackView(arrangedSubviews: [dot, titleLabel])
content.axis = .horizontal
content.alignment = .center
content.spacing = 5
addSubview(content)
content.snp.makeConstraints { $0.edges.equalToSuperview().inset(UIEdgeInsets(top: 4, left: 7, bottom: 4, right: 7)) }
titleLabel.setContentHuggingPriority(.required, for: .horizontal)
titleLabel.setContentCompressionResistancePriority(.required, for: .horizontal)
}
@available(*, unavailable)
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
/// 圆点、文字及浅色背景同步更新,复用时不残留上一设备的状态色。
func apply(title: String, tone: DroneDockStatusTone) {
let color = DroneDockStyle.color(for: tone)
titleLabel.text = title
titleLabel.textColor = color
dot.backgroundColor = color
backgroundColor = color.withAlphaComponent(0.09)
}
}
/// 环形电量组件,未知电量只显示轨道和占位读数。
private final class DroneDockBatteryView: UIView {
private let track = CAShapeLayer()
private let progress = CAShapeLayer()
private let valueLabel = DroneDockStyle.label(size: 28, weight: .semibold)
override init(frame: CGRect) {
super.init(frame: frame)
valueLabel.accessibilityIdentifier = "drone.metric.battery"
for layer in [track, progress] {
layer.fillColor = UIColor.clear.cgColor
layer.lineWidth = 7
self.layer.addSublayer(layer)
}
track.strokeColor = DroneDockStyle.border.cgColor
progress.strokeColor = DroneDockStyle.primary.cgColor
progress.lineCap = .round
let caption = DroneDockStyle.label("电池电量", size: 11, color: DroneDockStyle.secondary)
let stack = UIStackView(arrangedSubviews: [valueLabel, caption])
stack.axis = .vertical
stack.alignment = .center
stack.spacing = 1
addSubview(stack)
stack.snp.makeConstraints { make in make.center.equalToSuperview() }
}
@available(*, unavailable)
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
override func layoutSubviews() {
super.layoutSubviews()
let path = UIBezierPath(arcCenter: CGPoint(x: bounds.midX, y: bounds.midY), radius: min(bounds.width, bounds.height) / 2 - 5,
startAngle: -.pi / 2, endAngle: .pi * 1.5, clockwise: true).cgPath
track.path = path
progress.path = path
}
func apply(telemetry: DroneDockTelemetry) {
apply(text: telemetry.batteryText, progress: telemetry.batteryProgress)
}
/// 有效电量才绘制进度;未知值只保留环形轨道与破折号。
func apply(text: String, progress value: Double?) {
valueLabel.text = text
progress.strokeEnd = value.map { CGFloat(min(1, max(0, $0))) } ?? 0
accessibilityLabel = "电池电量 \(text)"
isAccessibilityElement = true
}
}
/// 无人机卡片的集合视图容器。
final class DroneDockDeviceCell: UICollectionViewCell {
let cardView = DroneDockDeviceCardView()
override init(frame: CGRect) {
super.init(frame: frame)
contentView.addSubview(cardView)
cardView.snp.makeConstraints { make in make.edges.equalToSuperview() }
}
@available(*, unavailable)
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
}