188 lines
8.1 KiB
Swift
188 lines
8.1 KiB
Swift
import SnapKit
|
||
import UIKit
|
||
|
||
/// 设备遥测卡:只展示设备状态,横向浏览不会选择执行拍摄的无人机。
|
||
final class DroneDockDeviceCardView: UIView {
|
||
private let nameLabel = DroneDockStyle.label(size: 15, weight: .semibold)
|
||
private let statusLabel = DroneDockStatusLabel()
|
||
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)"
|
||
}
|
||
|
||
private func setupUI() {
|
||
[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(symbol: "sparkles", 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 metrics = UIStackView(arrangedSubviews: [
|
||
metric(value: temperatureValue, symbol: "thermometer.medium", title: "环境温度"),
|
||
metric(value: windValue, symbol: "wind", title: "风速"),
|
||
metric(value: rainValue, symbol: "cloud", title: "雨量")
|
||
])
|
||
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)
|
||
}
|
||
}
|
||
|
||
private func telemetryRow(symbol: String, title: String, value: UILabel) -> UIView {
|
||
let icon = UIImageView(image: UIImage(systemName: symbol))
|
||
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 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)
|
||
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) {
|
||
valueLabel.text = telemetry.batteryText
|
||
progress.strokeEnd = telemetry.batteryProgress ?? 0
|
||
accessibilityLabel = "电池电量 \(telemetry.batteryText)"
|
||
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") }
|
||
}
|