Files
suixinkan_uikit/suixinkan/UI/DroneDock/DroneDockDeliveryMethodViewController.swift
T

151 lines
6.7 KiB
Swift

import SnapKit
import UIKit
/// 订单的交付方式弹窗,将云端交付和局域网闪传导航交回统一入口。
@MainActor
final class DroneDockDeliveryMethodViewController: BaseViewController {
/// 用户选择云端上传后,由入口打开现有真实交付页。
var onCloudUpload: (() -> Void)?
/// 用户选择面对面闪传后,由入口打开独立本地传输页。
var onNearbyTransfer: (() -> Void)?
/// 用户关闭弹窗后,由入口统一结束当前模态流程。
var onClose: (() -> Void)?
/// 两种交付方式使用独立、稳定的列表身份。
private enum Item: Hashable { case nearby, cloud }
private let customerTitle: String
private let headerView = DroneDeliveryUploadHeaderView()
private let footerView = DroneDeliveryUploadFooterView()
private lazy var collectionView = UICollectionView(frame: .zero, collectionViewLayout: makeLayout())
private var dataSource: UICollectionViewDiffableDataSource<Int, Item>!
private var hasRequestedNavigation = false
/// 游客标题由选中的订单传入,本弹窗不读取网络或改变订单状态。
init(customerTitle: String) {
self.customerTitle = customerTitle
super.init(nibName: nil, bundle: nil)
modalPresentationStyle = .pageSheet
}
@available(*, unavailable)
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
hasRequestedNavigation = false
navigationController?.isModalInPresentation = false
isModalInPresentation = false
navigationController?.setNavigationBarHidden(true, animated: false)
configureSheet()
}
override func setupNavigationBar() {
title = "交付成片"
navigationController?.setNavigationBarHidden(true, animated: false)
}
override func setupUI() {
view.backgroundColor = .white
collectionView.backgroundColor = .white
collectionView.alwaysBounceVertical = false
collectionView.showsVerticalScrollIndicator = false
collectionView.accessibilityIdentifier = "drone.delivery.method.list"
collectionView.delegate = self
collectionView.register(DroneDeliveryUploadMethodCell.self, forCellWithReuseIdentifier: "method")
[headerView, collectionView, footerView].forEach(view.addSubview)
configureDataSource()
applySnapshot(animated: false)
}
override func setupConstraints() {
headerView.snp.makeConstraints { make in
make.top.equalTo(view.safeAreaLayoutGuide)
make.leading.trailing.equalToSuperview()
}
collectionView.snp.makeConstraints { make in
make.top.equalTo(headerView.snp.bottom)
make.leading.trailing.equalToSuperview()
make.bottom.equalTo(footerView.snp.top)
}
footerView.snp.makeConstraints { make in make.leading.trailing.bottom.equalToSuperview() }
}
override func bindActions() {
headerView.onCloseTap = { [weak self] in self?.onClose?() }
footerView.onPrimaryTap = { [weak self] in self?.requestCloudUpload() }
}
private func configureSheet() {
guard let sheet = navigationController?.sheetPresentationController ?? sheetPresentationController else { return }
let identifier = UISheetPresentationController.Detent.Identifier("drone.delivery.method")
sheet.detents = [
.custom(identifier: identifier) { context in min(410, context.maximumDetentValue) },
.large()
]
sheet.selectedDetentIdentifier = identifier
sheet.prefersGrabberVisible = true
sheet.preferredCornerRadius = 22
sheet.prefersScrollingExpandsWhenScrolledToEdge = false
}
private func makeLayout() -> UICollectionViewLayout {
UICollectionViewCompositionalLayout { _, _ in
let height: CGFloat = 88
let item = NSCollectionLayoutItem(layoutSize: .init(widthDimension: .fractionalWidth(1), heightDimension: .fractionalHeight(1)))
let group = NSCollectionLayoutGroup.vertical(
layoutSize: .init(widthDimension: .fractionalWidth(1), heightDimension: .absolute(height)), subitems: [item])
let section = NSCollectionLayoutSection(group: group)
section.interGroupSpacing = 12
section.contentInsets = .init(top: 4, leading: 20, bottom: 4, trailing: 20)
return section
}
}
private func configureDataSource() {
dataSource = UICollectionViewDiffableDataSource<Int, Item>(collectionView: collectionView) { collection, path, item in
switch item {
case .nearby, .cloud:
let cell = collection.dequeueReusableCell(withReuseIdentifier: "method", for: path) as! DroneDeliveryUploadMethodCell
if item == .nearby {
cell.apply(title: "面对面闪传", detail: "同一 Wi-Fi 扫码接收 · 无需上传", symbol: "wifi", identifier: "drone.delivery.method.nearby")
} else {
cell.apply(title: "云端上传", detail: "选择打卡点,上传后游客可查看", symbol: "icloud.and.arrow.up", identifier: "drone.delivery.method.cloud")
}
return cell
}
}
}
private func applySnapshot(animated: Bool) {
guard dataSource != nil else { return }
var snapshot = NSDiffableDataSourceSnapshot<Int, Item>()
snapshot.appendSections([0])
headerView.apply(title: "交付成片", customer: customerTitle, detail: "请选择本次交付方式")
footerView.apply(primaryTitle: nil, enabled: false, identifier: "", detail: "可分批交付多个作品,不影响拍摄次数", secondaryTitle: nil)
snapshot.appendItems([.nearby, .cloud])
dataSource.apply(snapshot, animatingDifferences: animated)
collectionView.collectionViewLayout.invalidateLayout()
}
private func requestCloudUpload() {
guard !hasRequestedNavigation, let onCloudUpload else { return }
hasRequestedNavigation = true
onCloudUpload()
}
}
extension DroneDockDeliveryMethodViewController: UICollectionViewDelegate {
/// 只在用户选择后进入对应流程,方式弹窗本身不读取网络。
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
guard let item = dataSource.itemIdentifier(for: indexPath), !hasRequestedNavigation else { return }
switch item {
case .nearby:
guard let onNearbyTransfer else { return }
hasRequestedNavigation = true
onNearbyTransfer()
case .cloud: requestCloudUpload()
}
}
}