feat(drone-dock): 接入素材下载并限制拍满后游客确认
This commit is contained in:
@@ -8,10 +8,14 @@ import UIKit
|
||||
final class DroneDockMaterialsViewController: BaseViewController {
|
||||
/// 保存当前订单摘要、点位和任务的真实状态。
|
||||
let viewModel: DroneDockMaterialsViewModel
|
||||
/// 当前页面的前台下载批次;成功保存后才计入完成数量。
|
||||
let downloadViewModel: DroneDockMaterialDownloadViewModel
|
||||
private let thumbnailProvider: ((Int, Int) -> UIImage?)?
|
||||
private let mediaPreviewControllerFactory: ((URL, Bool, String) -> UIViewController)?
|
||||
private let footerView = DroneMaterialsFooterView()
|
||||
private let deliveryControllerFactory: (DroneDockHomeOrder, DroneDockHomeContext, Int?) -> UIViewController
|
||||
private let downloadSourceDescription: String?
|
||||
private let downloadIsSimulated: Bool
|
||||
|
||||
/// 元数据与媒体分区独立,任务没有素材时也保留其状态。
|
||||
private enum Section: Hashable { case order, filters, summary, task(Int), media(Int), message }
|
||||
@@ -28,13 +32,23 @@ final class DroneDockMaterialsViewController: BaseViewController {
|
||||
private var operation: LoadAction?
|
||||
private var hasAppeared = false
|
||||
private var refreshAfterDelivery = false
|
||||
private var lastDownloadPhase = DroneDockMaterialDownloadViewModel.Phase.idle
|
||||
private var hasPresentedPhotoPermission = false
|
||||
private var selectionLocked: Bool { downloadViewModel.phase != .idle }
|
||||
|
||||
/// 注入冻结订单范围的只读素材服务。
|
||||
init(viewModel: DroneDockMaterialsViewModel,
|
||||
deliveryControllerFactory: ((DroneDockHomeOrder, DroneDockHomeContext, Int?) -> UIViewController)? = nil,
|
||||
thumbnailProvider: ((Int, Int) -> UIImage?)? = nil,
|
||||
mediaPreviewControllerFactory: ((URL, Bool, String) -> UIViewController)? = nil) {
|
||||
mediaPreviewControllerFactory: ((URL, Bool, String) -> UIViewController)? = nil,
|
||||
downloadViewModel: DroneDockMaterialDownloadViewModel? = nil,
|
||||
downloadSourceDescription: String? = nil,
|
||||
downloadIsSimulated: Bool = false) {
|
||||
self.viewModel = viewModel
|
||||
self.downloadViewModel = downloadViewModel ?? DroneDockMaterialDownloadViewModel(
|
||||
isContextValid: { [weak viewModel] in viewModel?.acceptsDownloadContext() == true })
|
||||
self.downloadSourceDescription = downloadSourceDescription
|
||||
self.downloadIsSimulated = downloadIsSimulated
|
||||
self.thumbnailProvider = thumbnailProvider
|
||||
self.mediaPreviewControllerFactory = mediaPreviewControllerFactory
|
||||
self.deliveryControllerFactory = deliveryControllerFactory ?? DroneDockDeliveryEntry.makeController
|
||||
@@ -80,6 +94,10 @@ final class DroneDockMaterialsViewController: BaseViewController {
|
||||
view.addSubview(footerView)
|
||||
footerView.accessibilityIdentifier = "drone.materials.remote.footer"
|
||||
footerView.onPrimaryTap = { [weak self] in self?.downloadTapped() }
|
||||
footerView.onSecondaryTap = { [weak self] in self?.downloadViewModel.cancel() }
|
||||
downloadViewModel.onUpdate = { [weak self] in self?.downloadUpdated() }
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(pauseDownloadForBackground),
|
||||
name: UIApplication.didEnterBackgroundNotification, object: nil)
|
||||
configureDataSource()
|
||||
applySnapshot(animated: false)
|
||||
}
|
||||
@@ -106,6 +124,7 @@ final class DroneDockMaterialsViewController: BaseViewController {
|
||||
super.viewDidDisappear(animated)
|
||||
guard isMovingFromParent || isBeingDismissed || navigationController?.isBeingDismissed == true || parent == nil else { return }
|
||||
hasAppeared = false
|
||||
downloadViewModel.cancel()
|
||||
loadTask?.cancel()
|
||||
operation = nil
|
||||
viewModel.invalidate()
|
||||
@@ -129,10 +148,10 @@ final class DroneDockMaterialsViewController: BaseViewController {
|
||||
let cell = collection.dequeueReusableCell(withReuseIdentifier: "summary", for: path) as! DroneMaterialSelectionCell
|
||||
cell.apply(recordCount: self.viewModel.total ?? 0,
|
||||
allSelected: self.viewModel.allDownloadableSelected,
|
||||
enabled: self.operation == nil && !self.viewModel.selectableMaterialIDs.isEmpty)
|
||||
enabled: self.operation == nil && !self.selectionLocked && !self.viewModel.selectableMaterialIDs.isEmpty)
|
||||
cell.accessibilityIdentifier = "drone.materials.remote.summary"
|
||||
cell.onSelectAllTap = { [weak self] in
|
||||
guard let self, self.operation == nil else { return }
|
||||
guard let self, self.operation == nil, !self.selectionLocked else { return }
|
||||
self.viewModel.toggleSelectAll()
|
||||
self.applySnapshot(animated: false)
|
||||
}
|
||||
@@ -140,12 +159,12 @@ final class DroneDockMaterialsViewController: BaseViewController {
|
||||
case .filter(let id):
|
||||
let cell = collection.dequeueReusableCell(withReuseIdentifier: "filter", for: path) as! DroneMaterialFilterCell
|
||||
let title = id.flatMap { id in self.viewModel.spots.first { $0.id == id } }.map { Self.usable($0.name, fallback: "点位 \($0.id)") } ?? "全部点位"
|
||||
cell.apply(title: title, id: id.map(String.init), selected: id == self.displayedSpotID, locked: self.viewModel.detail == nil)
|
||||
cell.apply(title: title, id: id.map(String.init), selected: id == self.displayedSpotID, locked: self.viewModel.detail == nil || self.selectionLocked)
|
||||
return cell
|
||||
case .task(let taskID):
|
||||
let cell = collection.dequeueReusableCell(withReuseIdentifier: "record", for: path) as! DroneDockMaterialRecordCell
|
||||
if let task = self.task(taskID) {
|
||||
cell.apply(task: task, canDeliver: self.operation == nil && self.viewModel.detail?.order.canUploadFilm == true) { [weak self] in
|
||||
cell.apply(task: task, canDeliver: self.operation == nil && !self.selectionLocked && self.viewModel.detail?.order.canUploadFilm == true) { [weak self] in
|
||||
self?.openDelivery(taskID: taskID)
|
||||
}
|
||||
}
|
||||
@@ -156,12 +175,12 @@ final class DroneDockMaterialsViewController: BaseViewController {
|
||||
let identity = DroneDockMaterialSelectionID(taskID: taskID, fileID: fileID)
|
||||
cell.apply(file: file, taskID: taskID,
|
||||
selected: self.viewModel.selectedMaterialIDs.contains(identity),
|
||||
selectionEnabled: self.operation == nil && self.viewModel.selectableMaterialIDs.contains(identity),
|
||||
selectionEnabled: self.operation == nil && !self.selectionLocked && self.viewModel.selectableMaterialIDs.contains(identity),
|
||||
localImage: self.thumbnailProvider?(taskID, fileID),
|
||||
allowsRemoteImage: self.thumbnailProvider == nil,
|
||||
preview: { [weak self] in self?.previewFile(taskID: taskID, fileID: fileID) },
|
||||
select: { [weak self] in
|
||||
guard let self, self.operation == nil else { return }
|
||||
guard let self, self.operation == nil, !self.selectionLocked else { return }
|
||||
self.viewModel.toggleSelection(taskID: taskID, fileID: fileID)
|
||||
self.applySnapshot(animated: false)
|
||||
})
|
||||
@@ -261,17 +280,17 @@ final class DroneDockMaterialsViewController: BaseViewController {
|
||||
snapshot.appendSections([.message])
|
||||
snapshot.appendItems([.message], toSection: .message)
|
||||
}
|
||||
footerView.apply(title: "已选择\(viewModel.selectedCount)项", detail: "",
|
||||
primaryTitle: "下载所选素材", primarySymbol: "arrow.down.to.line",
|
||||
enabled: operation == nil && viewModel.selectedCount > 0,
|
||||
identifier: "drone.materials.remote.download", progress: nil,
|
||||
trailingText: "仅可选择已上传素材")
|
||||
updateDownloadFooter()
|
||||
let previous = Set(dataSource.snapshot().itemIdentifiers)
|
||||
snapshot.reconfigureItems(snapshot.itemIdentifiers.filter { previous.contains($0) })
|
||||
dataSource.apply(snapshot, animatingDifferences: animated)
|
||||
}
|
||||
|
||||
private func load(_ action: LoadAction) {
|
||||
guard !selectionLocked else {
|
||||
collectionView.refreshControl?.endRefreshing()
|
||||
return
|
||||
}
|
||||
loadTask?.cancel()
|
||||
operation = action
|
||||
applySnapshot(animated: false)
|
||||
@@ -293,7 +312,7 @@ final class DroneDockMaterialsViewController: BaseViewController {
|
||||
private func task(_ id: Int) -> DroneDockMaterialTask? { viewModel.tasks.first { $0.id == id } }
|
||||
|
||||
private func openDelivery(taskID: Int) {
|
||||
guard operation == nil, presentedViewController == nil,
|
||||
guard operation == nil, !selectionLocked, presentedViewController == nil,
|
||||
let order = viewModel.detail?.order, order.canUploadFilm == true, task(taskID) != nil else { return }
|
||||
let sheet = DroneDockDeliveryEntry.makeMethodSheet(order: order, context: viewModel.context,
|
||||
taskID: taskID, controllerFactory: deliveryControllerFactory) { [weak self] in
|
||||
@@ -307,8 +326,67 @@ final class DroneDockMaterialsViewController: BaseViewController {
|
||||
}
|
||||
|
||||
private func downloadTapped() {
|
||||
guard operation == nil, viewModel.selectedCount > 0 else { return }
|
||||
showAlert(title: "下载功能暂未开放", message: "已选择的素材会保留。当前版本尚未接入下载和保存相册功能。")
|
||||
guard operation == nil else { return }
|
||||
switch downloadViewModel.phase {
|
||||
case .idle:
|
||||
_ = downloadViewModel.start(items: viewModel.selectedDownloadItems())
|
||||
applySnapshot(animated: false)
|
||||
case .downloading: downloadViewModel.pause()
|
||||
case .paused: downloadViewModel.resume()
|
||||
case .finished:
|
||||
if downloadViewModel.failedCount > 0 { downloadViewModel.retryFailed() }
|
||||
else { downloadViewModel.cancel() }
|
||||
}
|
||||
}
|
||||
|
||||
@objc private func pauseDownloadForBackground() { downloadViewModel.pause() }
|
||||
|
||||
private func downloadUpdated() {
|
||||
if lastDownloadPhase != downloadViewModel.phase {
|
||||
lastDownloadPhase = downloadViewModel.phase
|
||||
applySnapshot(animated: false)
|
||||
} else {
|
||||
updateDownloadFooter()
|
||||
}
|
||||
if !downloadViewModel.permissionDenied { hasPresentedPhotoPermission = false }
|
||||
if downloadViewModel.permissionDenied, !hasPresentedPhotoPermission,
|
||||
viewIfLoaded?.window != nil, presentedViewController == nil {
|
||||
hasPresentedPhotoPermission = true
|
||||
let alert = UIAlertController(title: "需要相册保存权限",
|
||||
message: "请允许添加照片和视频,以保存所选素材。授权后可返回重试失败项。", preferredStyle: .alert)
|
||||
alert.addAction(UIAlertAction(title: "取消", style: .cancel))
|
||||
alert.addAction(UIAlertAction(title: "去设置", style: .default) { _ in
|
||||
if let url = URL(string: UIApplication.openSettingsURLString) { UIApplication.shared.open(url) }
|
||||
})
|
||||
present(alert, animated: true)
|
||||
}
|
||||
}
|
||||
|
||||
private func updateDownloadFooter() {
|
||||
navigationItem.rightBarButtonItem?.isEnabled = !selectionLocked
|
||||
let model = downloadViewModel
|
||||
let identifier = "drone.materials.remote.download"
|
||||
switch model.phase {
|
||||
case .idle:
|
||||
footerView.apply(title: "已选择\(viewModel.selectedCount)项", detail: model.lastErrorMessage ?? downloadSourceDescription ?? "",
|
||||
primaryTitle: "下载所选素材", primarySymbol: "arrow.down.to.line",
|
||||
enabled: operation == nil && viewModel.selectedCount > 0, identifier: identifier, progress: nil,
|
||||
trailingText: "仅可选择已上传素材")
|
||||
case .downloading, .paused:
|
||||
let paused = model.phase == .paused
|
||||
footerView.apply(title: "\(paused ? "已暂停" : "下载中") · \(downloadIsSimulated ? "演示完成" : "已保存") \(model.completedCount)/\(model.totalCount) 项",
|
||||
detail: downloadSourceDescription ?? "请保持页面开启,切到后台将暂停", primaryTitle: paused ? "继续下载" : "暂停下载",
|
||||
primarySymbol: paused ? "play.fill" : "pause.fill", enabled: true, identifier: identifier,
|
||||
progress: model.progress, secondaryTitle: "取消下载", trailingText: "\(Int(model.progress * 100))%")
|
||||
case .finished:
|
||||
let failed = model.failedCount > 0
|
||||
let saved = "\(downloadIsSimulated ? "演示完成" : "已保存") \(model.completedCount) 项"
|
||||
footerView.apply(title: failed ? "\(saved) · 失败 \(model.failedCount) 项" : saved,
|
||||
detail: failed ? model.lastErrorMessage ?? "失败项可重试,成功项已保存相册" : downloadSourceDescription ?? "可在系统相册查看",
|
||||
primaryTitle: failed ? "重试失败项" : "继续选择", primarySymbol: failed ? "arrow.clockwise" : "checkmark",
|
||||
enabled: true, identifier: identifier, progress: nil, secondaryTitle: failed ? "继续选择" : nil,
|
||||
tone: failed ? .orange : .green, resultSymbol: failed ? "exclamationmark.circle.fill" : "checkmark.circle.fill")
|
||||
}
|
||||
}
|
||||
|
||||
private func previewFile(taskID: Int, fileID: Int) {
|
||||
@@ -341,7 +419,7 @@ extension DroneDockMaterialsViewController: UICollectionViewDelegate {
|
||||
guard let item = dataSource.itemIdentifier(for: indexPath) else { return }
|
||||
switch item {
|
||||
case .filter(let id):
|
||||
guard viewModel.detail != nil, id != displayedSpotID else { return }
|
||||
guard viewModel.detail != nil, !selectionLocked, id != displayedSpotID else { return }
|
||||
load(.filter(id))
|
||||
case .file(let taskID, let fileID):
|
||||
previewFile(taskID: taskID, fileID: fileID)
|
||||
|
||||
Reference in New Issue
Block a user