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

456 lines
22 KiB
Swift
Raw Permalink 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 Kingfisher
import SnapKit
import UIKit
/// 景区素材模块的局部样式,可独立于机巢业务使用。
@MainActor
enum ScenicMaterialsStyle {
static let primary = UIColor(hex: 0x066BFF)
static let background = UIColor.white
static let text = UIColor(hex: 0x151515)
static let secondary = UIColor(hex: 0x8490A5)
static let border = UIColor(hex: 0xE7E9EF)
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.kf.cancelDownloadTask()
if clip.isRemote {
cover.kf.setImage(with: clip.coverURL, placeholder: UIImage(systemName: "photo"))
} else { cover.image = UIImage(named: clip.coverAssetName) }
ordinalLabel.text = clip.fileLabel ?? String(format: "片段 %02d", clip.ordinal)
metadataLabel.text = isHero ? "\(clip.durationText) · \(clip.sizeText)" : clip.durationText
metadataLabel.font = .monospacedDigitSystemFont(ofSize: isHero ? 11 : 10, weight: .regular)
selectionButton.setImage(MaterialSelectionIcon.scenicImage(selected: selected, size: 25,
tint: selected ? MaterialSelectionIcon.primary : .white), for: .normal)
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 = MaterialSelectionIcon.scenicImage(selected: isSelected, size: 21,
tint: isSelected ? MaterialSelectionIcon.primary : UIColor(hex: 0x687084))
configuration.imagePadding = 5
configuration.contentInsets = .zero
configuration.baseForegroundColor = isSelected ? ScenicMaterialsStyle.primary : ScenicMaterialsStyle.secondary
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?() }
}
/// 固定底部的选择/下载摘要,由页面显式传入布局和状态,不执行文件操作。
@MainActor
final class ScenicMaterialsFooterView: UIView {
/// 选择态左右排列;传输态显示摘要、进度和通栏按钮;结果态显示摘要和操作行。
enum Layout { case selection, progress, result }
/// 当前阶段的主要操作,例如下载、继续或重试。
var onPrimaryTap: (() -> Void)?
/// 当前阶段的次要操作,例如取消或继续选择。
var onSecondaryTap: (() -> Void)?
private let titleLabel = ScenicMaterialsStyle.label(size: 14)
private let detailLabel = ScenicMaterialsStyle.label(size: 11, color: ScenicMaterialsStyle.secondary)
private let primaryButton = UIButton(type: .custom)
private let secondaryButton = UIButton(type: .custom)
private let progressView = UIProgressView(progressViewStyle: .bar)
private let activity = UIActivityIndicatorView(style: .medium)
private let summaryRow = UIStackView()
private let actionRow = UIStackView()
private let stack = UIStackView()
private var currentLayout: Layout?
private var hasSecondaryAction = false
/// 创建随安全区延伸的白色底栏。
override init(frame: CGRect) {
super.init(frame: frame)
setupUI()
setupConstraints()
}
@available(*, unavailable)
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
private func setupUI() {
backgroundColor = .white
titleLabel.numberOfLines = 2
titleLabel.accessibilityIdentifier = "scenic.materials.footer.title"
detailLabel.numberOfLines = 2
detailLabel.accessibilityIdentifier = "scenic.materials.footer.detail"
primaryButton.titleLabel?.font = .systemFont(ofSize: 15, weight: .semibold)
primaryButton.titleLabel?.numberOfLines = 1
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.setTitleColor(ScenicMaterialsStyle.secondary, for: .normal)
secondaryButton.titleLabel?.font = .systemFont(ofSize: 15)
secondaryButton.titleLabel?.numberOfLines = 1
secondaryButton.titleLabel?.textAlignment = .center
secondaryButton.layer.cornerRadius = 10
secondaryButton.accessibilityIdentifier = "scenic.materials.secondary"
secondaryButton.addTarget(self, action: #selector(secondaryTapped), for: .touchUpInside)
progressView.progressTintColor = ScenicMaterialsStyle.primary
progressView.trackTintColor = UIColor(hex: 0xE7EDF7)
progressView.accessibilityIdentifier = "scenic.materials.footer.progress"
activity.color = .white
activity.hidesWhenStopped = true
activity.isUserInteractionEnabled = false
activity.accessibilityIdentifier = "scenic.materials.footer.activity"
primaryButton.addSubview(activity)
summaryRow.axis = .horizontal
summaryRow.alignment = .center
summaryRow.spacing = 8
actionRow.axis = .horizontal
actionRow.alignment = .center
actionRow.spacing = 16
stack.axis = .vertical
stack.spacing = 8
addSubview(stack)
}
private func setupConstraints() {
stack.snp.makeConstraints {
$0.top.equalToSuperview().offset(14)
$0.leading.trailing.equalToSuperview().inset(18)
$0.bottom.equalTo(safeAreaLayoutGuide).inset(14)
}
primaryButton.snp.makeConstraints { $0.height.equalTo(46) }
progressView.snp.makeConstraints { $0.height.equalTo(3).priority(999) }
activity.snp.makeConstraints {
$0.centerY.equalToSuperview()
$0.centerX.equalToSuperview()
$0.size.equalTo(18)
}
}
/// 按产品页面的状态布局刷新底栏,忙碌按钮保持蓝色但不可重复触发。
func configure(title: String, detail: String?, primaryTitle: String, primaryEnabled: Bool,
secondaryTitle: String? = nil, progress: Float? = nil,
layout: Layout = .selection, isBusy: Bool = false) {
UIView.performWithoutAnimation {
if currentLayout != layout || hasSecondaryAction != (secondaryTitle != nil) {
arrange(layout, hasSecondary: secondaryTitle != nil)
}
titleLabel.attributedText = nil
titleLabel.text = title
if layout == .selection, let range = title.range(of: #"\d+"#, options: .regularExpression) {
let attributed = NSMutableAttributedString(string: title)
attributed.addAttributes([.foregroundColor: ScenicMaterialsStyle.primary,
.font: UIFont.systemFont(ofSize: 20, weight: .bold)],
range: NSRange(range, in: title))
titleLabel.attributedText = attributed
}
detailLabel.text = detail
detailLabel.isHidden = detail == nil || layout == .progress
primaryButton.accessibilityHint = detail
primaryButton.setTitle(primaryTitle, for: .normal)
primaryButton.setTitle(primaryTitle, for: .disabled)
let titleFont = primaryButton.titleLabel?.font ?? .systemFont(ofSize: 15, weight: .semibold)
let titleWidth = ceil(primaryTitle.size(withAttributes: [.font: titleFont]).width) + 2
activity.snp.updateConstraints { $0.centerX.equalToSuperview().offset(-titleWidth / 2 - 4) }
primaryButton.backgroundColor = primaryEnabled || isBusy ? ScenicMaterialsStyle.primary : UIColor(hex: 0xCDDFFF)
primaryButton.isEnabled = primaryEnabled
secondaryButton.setTitle(secondaryTitle, for: .normal)
secondaryButton.isHidden = secondaryTitle == nil
secondaryButton.isEnabled = secondaryTitle != nil
progressView.isHidden = progress == nil
progressView.progress = min(max(progress ?? 0, 0), 1)
if isBusy { activity.startAnimating() } else { activity.stopAnimating() }
layoutIfNeeded()
}
}
private func arrange(_ layout: Layout, hasSecondary: Bool) {
for container in [stack, summaryRow, actionRow] {
container.arrangedSubviews.forEach { container.removeArrangedSubview($0); $0.removeFromSuperview() }
}
titleLabel.snp.removeConstraints()
detailLabel.snp.removeConstraints()
secondaryButton.snp.removeConstraints()
actionRow.distribution = .fill
titleLabel.font = .systemFont(ofSize: layout == .selection ? 17 : 12)
detailLabel.textAlignment = layout == .selection ? .left : .right
secondaryButton.backgroundColor = layout == .result ? UIColor(hex: 0xF2F4F7) : .clear
secondaryButton.titleLabel?.font = .systemFont(ofSize: layout == .progress ? 12 : 15)
switch layout {
case .selection:
actionRow.addArrangedSubview(titleLabel)
actionRow.addArrangedSubview(primaryButton)
titleLabel.snp.makeConstraints { $0.width.equalTo(126).priority(999) }
stack.addArrangedSubview(actionRow)
stack.addArrangedSubview(detailLabel)
case .progress:
summaryRow.addArrangedSubview(titleLabel)
if hasSecondary {
summaryRow.addArrangedSubview(secondaryButton)
secondaryButton.snp.makeConstraints { $0.width.equalTo(44); $0.height.equalTo(28) }
}
actionRow.addArrangedSubview(primaryButton)
stack.addArrangedSubview(summaryRow)
stack.addArrangedSubview(progressView)
stack.addArrangedSubview(actionRow)
case .result:
summaryRow.addArrangedSubview(titleLabel)
summaryRow.addArrangedSubview(detailLabel)
titleLabel.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
detailLabel.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
if hasSecondary {
actionRow.addArrangedSubview(secondaryButton)
secondaryButton.snp.makeConstraints { $0.height.equalTo(46) }
actionRow.distribution = .fillEqually
}
actionRow.addArrangedSubview(primaryButton)
stack.addArrangedSubview(summaryRow)
stack.addArrangedSubview(actionRow)
}
currentLayout = layout
hasSecondaryAction = hasSecondary
}
@objc private func primaryTapped() {
guard primaryButton.isEnabled else { return }
onPrimaryTap?()
}
@objc private func secondaryTapped() {
guard secondaryButton.isEnabled, !secondaryButton.isHidden else { return }
onSecondaryTap?()
}
}