Files
suixinkan_uikit/suixinkan/UI/ScenicMaterials/ScenicMaterialComponents.swift
T

361 lines
17 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
/// 景区素材模块的局部样式,可独立于机巢业务使用。
@MainActor
enum ScenicMaterialsStyle {
static let primary = UIColor(hex: 0x3164FF)
static let background = UIColor.white
static let text = UIColor(hex: 0x20242C)
static let secondary = UIColor(hex: 0x9297A1)
static let border = UIColor(hex: 0xECEEF2)
static let success = UIColor(hex: 0x17986A)
static let warning = UIColor(hex: 0xD98926)
/// 创建模块内使用的系统字体标签。
static func label(_ text: String? = nil, size: CGFloat = 14, weight: UIFont.Weight = .regular,
color: UIColor? = nil) -> UILabel {
let label = UILabel()
label.text = text
label.font = .systemFont(ofSize: size, weight: weight)
label.textColor = color ?? ScenicMaterialsStyle.text
return label
}
}
/// 视频封面底部渐变,只用于保证白色元信息在照片上的对比度。
@MainActor
private final class ScenicMaterialGradientView: UIView {
override class var layerClass: AnyClass { CAGradientLayer.self }
override init(frame: CGRect) {
super.init(frame: frame)
isUserInteractionEnabled = false
if let gradient = layer as? CAGradientLayer {
gradient.colors = [UIColor.clear.cgColor, UIColor.black.withAlphaComponent(0.68).cgColor]
gradient.locations = [0, 1]
}
}
@available(*, unavailable)
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
}
/// 景区空镜片段卡,预览和选择分别命中,下载期间仍可预览。
@MainActor
final class ScenicMaterialClipCell: UICollectionViewCell {
/// 请求预览当前片段,不改变选择状态。
var onPreview: (() -> Void)?
/// 请求切换当前片段的勾选状态。
var onToggleSelection: (() -> Void)?
private let cover = UIImageView()
private let previewButton = UIButton(type: .custom)
private let selectionButton = UIButton(type: .custom)
private let ordinalLabel = ScenicMaterialsStyle.label(size: 11, color: .white)
private let metadataLabel = ScenicMaterialsStyle.label(size: 11, color: .white)
private let statusLabel = ScenicMaterialsStyle.label(size: 10, weight: .medium, color: .white)
private let progressView = UIProgressView(progressViewStyle: .bar)
/// 创建圆角图片卡和独立的四十四点选择命中区。
override init(frame: CGRect) {
super.init(frame: frame)
setupUI()
setupConstraints()
}
@available(*, unavailable)
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
override func prepareForReuse() {
super.prepareForReuse()
onPreview = nil
onToggleSelection = nil
cover.image = nil
progressView.isHidden = true
statusLabel.isHidden = true
}
/// 显示片段及本机演示传输状态;首段大图额外显示分辨率和体积。
func configure(clip: ScenicMaterialClip, selected: Bool, isSelectionEnabled: Bool,
statusText: String? = nil, progress: Double? = nil, isHero: Bool = false) {
cover.image = UIImage(named: clip.coverAssetName)
ordinalLabel.text = String(format: "片段 %02d", clip.ordinal)
metadataLabel.text = isHero ? "\(clip.durationText) · \(clip.resolution) · \(clip.sizeText)" : clip.durationText
metadataLabel.font = .monospacedDigitSystemFont(ofSize: isHero ? 11 : 10, weight: .regular)
selectionButton.setImage(UIImage(systemName: selected ? "checkmark.circle.fill" : "circle",
withConfiguration: UIImage.SymbolConfiguration(pointSize: 22, weight: .regular)), for: .normal)
selectionButton.tintColor = selected ? ScenicMaterialsStyle.primary : .white
selectionButton.isEnabled = isSelectionEnabled
selectionButton.alpha = isSelectionEnabled ? 1 : 0.45
selectionButton.accessibilityIdentifier = "scenic.materials.select.\(clip.id)"
selectionButton.accessibilityLabel = "\(selected ? "取消选择" : "选择")片段\(clip.ordinal)"
selectionButton.accessibilityTraits = selected ? [.button, .selected] : .button
if !isSelectionEnabled { selectionButton.accessibilityTraits.insert(.notEnabled) }
previewButton.accessibilityIdentifier = "scenic.materials.preview.\(clip.id)"
previewButton.accessibilityLabel = "预览片段\(clip.ordinal),\(clip.durationText)"
previewButton.isEnabled = clip.isAvailable
statusLabel.text = statusText.map { " \($0) " }
statusLabel.isHidden = statusText == nil
statusLabel.backgroundColor = statusText?.contains("失败") == true
? ScenicMaterialsStyle.warning.withAlphaComponent(0.95)
: ScenicMaterialsStyle.primary.withAlphaComponent(0.88)
progressView.isHidden = progress == nil
progressView.progress = Float(min(max(progress ?? 0, 0), 1))
accessibilityIdentifier = "scenic.materials.clip.\(clip.id)"
}
private func setupUI() {
contentView.backgroundColor = UIColor(hex: 0xE9EDF4)
contentView.layer.cornerRadius = 10
contentView.layer.cornerCurve = .continuous
contentView.clipsToBounds = true
cover.contentMode = .scaleAspectFill
cover.clipsToBounds = true
previewButton.setImage(UIImage(systemName: "play.fill", withConfiguration: UIImage.SymbolConfiguration(pointSize: 24)), for: .normal)
previewButton.tintColor = .white
previewButton.addTarget(self, action: #selector(previewTapped), for: .touchUpInside)
selectionButton.addTarget(self, action: #selector(selectionTapped), for: .touchUpInside)
statusLabel.layer.cornerRadius = 5
statusLabel.clipsToBounds = true
statusLabel.setContentCompressionResistancePriority(.required, for: .horizontal)
ordinalLabel.setContentCompressionResistancePriority(.required, for: .horizontal)
metadataLabel.textAlignment = .right
metadataLabel.minimumScaleFactor = 0.85
metadataLabel.adjustsFontSizeToFitWidth = true
progressView.progressTintColor = ScenicMaterialsStyle.primary
progressView.trackTintColor = .white.withAlphaComponent(0.4)
let gradient = ScenicMaterialGradientView()
gradient.tag = 701
[cover, previewButton, gradient, ordinalLabel, metadataLabel, statusLabel, selectionButton, progressView].forEach(contentView.addSubview)
}
private func setupConstraints() {
cover.snp.makeConstraints { $0.edges.equalToSuperview() }
previewButton.snp.makeConstraints { $0.edges.equalToSuperview() }
contentView.viewWithTag(701)?.snp.makeConstraints {
$0.leading.trailing.bottom.equalToSuperview()
$0.height.equalTo(55)
}
selectionButton.snp.makeConstraints { $0.top.leading.equalToSuperview(); $0.size.equalTo(44) }
statusLabel.snp.makeConstraints {
$0.top.trailing.equalToSuperview().inset(9)
$0.leading.greaterThanOrEqualTo(selectionButton.snp.trailing).offset(2)
$0.height.equalTo(22)
}
ordinalLabel.snp.makeConstraints { $0.leading.bottom.equalToSuperview().inset(9) }
metadataLabel.snp.makeConstraints {
$0.trailing.bottom.equalToSuperview().inset(9)
$0.leading.greaterThanOrEqualTo(ordinalLabel.snp.trailing).offset(5)
}
progressView.snp.makeConstraints { $0.leading.trailing.bottom.equalToSuperview(); $0.height.equalTo(3) }
}
@objc private func previewTapped() { onPreview?() }
@objc private func selectionTapped() { onToggleSelection?() }
}
/// 航线与拍摄批次标题,整组勾选由 ViewModel 决定完整片段范围。
@MainActor
final class ScenicMaterialGroupHeaderView: UICollectionReusableView {
/// 请求勾选或取消本组全部可选片段。
var onToggleGroup: (() -> Void)?
private let routeLabel = ScenicMaterialsStyle.label(size: 17, weight: .semibold)
private let timeLabel = ScenicMaterialsStyle.label(size: 11, color: ScenicMaterialsStyle.secondary)
private let groupButton = UIButton(type: .system)
/// 创建可复用的航线标题和批次选择行。
override init(frame: CGRect) {
super.init(frame: frame)
routeLabel.numberOfLines = 2
routeLabel.accessibilityTraits = .header
timeLabel.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
groupButton.addTarget(self, action: #selector(groupTapped), for: .touchUpInside)
addSubview(routeLabel)
addSubview(timeLabel)
addSubview(groupButton)
routeLabel.snp.makeConstraints {
$0.leading.trailing.equalToSuperview()
$0.top.equalToSuperview().offset(10)
$0.bottom.lessThanOrEqualTo(groupButton.snp.top).offset(-4)
}
timeLabel.snp.makeConstraints {
$0.leading.equalToSuperview()
$0.centerY.equalTo(groupButton)
$0.trailing.lessThanOrEqualTo(groupButton.snp.leading).offset(-6)
}
groupButton.snp.makeConstraints {
$0.trailing.equalToSuperview()
$0.bottom.equalToSuperview()
$0.height.equalTo(44)
$0.width.greaterThanOrEqualTo(88)
}
}
@available(*, unavailable)
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
override func prepareForReuse() { super.prepareForReuse(); onToggleGroup = nil }
/// 首组显示航线标题,后续同航线批次仅显示时间和数量。
func configure(groupID: String, routeName: String?, timeTitle: String, count: Int,
isSelected: Bool, canSelect: Bool) {
routeLabel.text = routeName
routeLabel.isHidden = routeName == nil
routeLabel.snp.remakeConstraints {
$0.leading.trailing.equalToSuperview()
if routeName == nil {
$0.top.equalToSuperview()
$0.height.equalTo(0)
} else {
$0.top.equalToSuperview().offset(10)
$0.bottom.lessThanOrEqualTo(groupButton.snp.top).offset(-4)
}
}
timeLabel.text = "\(timeTitle) · \(count) 段素材"
var configuration = UIButton.Configuration.plain()
configuration.title = "选择本组"
configuration.image = UIImage(systemName: isSelected ? "checkmark.circle.fill" : "circle")
configuration.imagePadding = 5
configuration.contentInsets = .zero
configuration.baseForegroundColor = isSelected ? ScenicMaterialsStyle.primary : ScenicMaterialsStyle.secondary
configuration.preferredSymbolConfigurationForImage = UIImage.SymbolConfiguration(pointSize: 16)
configuration.titleTextAttributesTransformer = UIConfigurationTextAttributesTransformer { input in
var output = input
output.font = .systemFont(ofSize: 11)
return output
}
groupButton.configuration = configuration
groupButton.isEnabled = canSelect
groupButton.accessibilityIdentifier = "scenic.materials.group.\(groupID)"
groupButton.accessibilityLabel = "\(isSelected ? "取消选择本组" : "选择本组"),\(timeTitle),\(count)段"
groupButton.accessibilityTraits = isSelected ? [.button, .selected] : .button
if !canSelect { groupButton.accessibilityTraits.insert(.notEnabled) }
}
@objc private func groupTapped() { onToggleGroup?() }
}
/// 展开或收起同一拍摄批次中默认折叠的片段。
@MainActor
final class ScenicMaterialExpandCell: UICollectionViewCell {
/// 切换本组片段展示范围,选择范围不随折叠改变。
var onToggleExpanded: (() -> Void)?
private let button = UIButton(type: .system)
/// 创建居中的轻量展开按钮。
override init(frame: CGRect) {
super.init(frame: frame)
button.tintColor = ScenicMaterialsStyle.secondary
button.titleLabel?.font = .systemFont(ofSize: 12)
button.addTarget(self, action: #selector(tapped), for: .touchUpInside)
contentView.addSubview(button)
button.snp.makeConstraints { $0.edges.equalToSuperview() }
}
@available(*, unavailable)
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
override func prepareForReuse() { super.prepareForReuse(); onToggleExpanded = nil }
/// 显示展开余量或收起文案。
func configure(groupID: String, title: String) {
button.setTitle(title, for: .normal)
button.setImage(UIImage(systemName: title.contains("收起") ? "chevron.up" : "chevron.down"), for: .normal)
button.semanticContentAttribute = .forceRightToLeft
button.accessibilityIdentifier = "scenic.materials.expand.\(groupID)"
}
@objc private func tapped() { onToggleExpanded?() }
}
/// 固定底部的选择/下载摘要,不直接持有 ViewModel 或执行文件操作。
@MainActor
final class ScenicMaterialsFooterView: UIView {
/// 当前阶段的主要操作,例如下载、继续或重试。
var onPrimaryTap: (() -> Void)?
/// 当前阶段的次要操作,例如取消或继续选择。
var onSecondaryTap: (() -> Void)?
private let titleLabel = ScenicMaterialsStyle.label(size: 14, weight: .medium)
private let detailLabel = ScenicMaterialsStyle.label(size: 10, color: ScenicMaterialsStyle.secondary)
private let primaryButton = UIButton(type: .custom)
private let secondaryButton = UIButton(type: .custom)
private let progressView = UIProgressView(progressViewStyle: .bar)
/// 创建随安全区延伸的白色底栏。
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .white
titleLabel.numberOfLines = 2
detailLabel.numberOfLines = 2
primaryButton.titleLabel?.font = .systemFont(ofSize: 14, weight: .semibold)
primaryButton.titleLabel?.numberOfLines = 2
primaryButton.titleLabel?.textAlignment = .center
primaryButton.setTitleColor(.white, for: .normal)
primaryButton.setTitleColor(.white, for: .disabled)
primaryButton.layer.cornerRadius = 10
primaryButton.clipsToBounds = true
primaryButton.accessibilityIdentifier = "scenic.materials.primary"
primaryButton.addTarget(self, action: #selector(primaryTapped), for: .touchUpInside)
secondaryButton.tintColor = ScenicMaterialsStyle.primary
secondaryButton.setTitleColor(ScenicMaterialsStyle.primary, for: .normal)
secondaryButton.titleLabel?.font = .systemFont(ofSize: 13)
secondaryButton.accessibilityIdentifier = "scenic.materials.secondary"
secondaryButton.addTarget(self, action: #selector(secondaryTapped), for: .touchUpInside)
progressView.progressTintColor = ScenicMaterialsStyle.primary
progressView.trackTintColor = ScenicMaterialsStyle.border
let labels = UIStackView(arrangedSubviews: [titleLabel, detailLabel])
labels.axis = .vertical
labels.spacing = 5
let row = UIStackView(arrangedSubviews: [labels, primaryButton])
row.axis = .horizontal
row.spacing = 12
row.alignment = .center
let stack = UIStackView(arrangedSubviews: [row, progressView, secondaryButton])
stack.axis = .vertical
stack.spacing = 10
let separator = UIView()
separator.backgroundColor = ScenicMaterialsStyle.border
addSubview(separator)
addSubview(stack)
separator.snp.makeConstraints { $0.top.leading.trailing.equalToSuperview(); $0.height.equalTo(0.5) }
stack.snp.makeConstraints {
$0.top.equalToSuperview().offset(14)
$0.leading.trailing.equalToSuperview().inset(20)
$0.bottom.equalTo(safeAreaLayoutGuide).inset(12)
}
primaryButton.snp.makeConstraints { $0.width.equalTo(158); $0.height.equalTo(46) }
progressView.snp.makeConstraints { $0.height.equalTo(3).priority(999) }
secondaryButton.snp.makeConstraints { $0.height.equalTo(32).priority(999) }
}
@available(*, unavailable)
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
/// 由当前页面状态显式刷新底栏,始终保留禁用按钮的可读标题。
func configure(title: String, detail: String?, primaryTitle: String, primaryEnabled: Bool,
secondaryTitle: String? = nil, progress: Float? = nil) {
UIView.performWithoutAnimation {
titleLabel.text = title
detailLabel.text = detail
detailLabel.isHidden = detail == nil
if primaryButton.title(for: .normal) != primaryTitle {
primaryButton.setTitle(primaryTitle, for: .normal)
primaryButton.setTitle(primaryTitle, for: .disabled)
}
primaryButton.backgroundColor = primaryEnabled ? ScenicMaterialsStyle.primary : ScenicMaterialsStyle.primary.withAlphaComponent(0.25)
primaryButton.isEnabled = primaryEnabled
if secondaryButton.title(for: .normal) != secondaryTitle {
secondaryButton.setTitle(secondaryTitle, for: .normal)
}
secondaryButton.isHidden = secondaryTitle == nil
progressView.isHidden = progress == nil
progressView.progress = min(max(progress ?? 0, 0), 1)
layoutIfNeeded()
}
}
@objc private func primaryTapped() { onPrimaryTap?() }
@objc private func secondaryTapped() { onSecondaryTap?() }
}