feat(drone-dock): 接通任务召唤与状态页并固定拍摄布局
接入 plane-status、tasks/create、shoot 与 shooting,保持同一任务 ID 串接、重复提交保护和只读单次刷新。拍摄任务页固定顶部参数与底部安全控制,中间状态区域适配小屏且不滚动;同步接口记录、脱敏响应及相关测试。
This commit is contained in:
@@ -1,27 +1,41 @@
|
||||
import SnapKit
|
||||
import UIKit
|
||||
|
||||
/// 进入页面后通过 project-info 读取已购套餐、点位和航线。
|
||||
/// 展示准备信息并提交召唤;业务成功后携同一任务 ID 进入真实拍摄任务页。
|
||||
@MainActor
|
||||
final class DroneDockRemotePreparationViewController: BaseViewController {
|
||||
/// 当前订单与准备页的真实读取状态。
|
||||
/// 当前订单、准备信息及任务创建状态。
|
||||
let viewModel: DroneDockPreparationViewModel
|
||||
|
||||
/// 点位与航线沿用原准备页顺序,状态区单独承载加载和错误说明。
|
||||
private enum Section: Int, CaseIterable { case order, points, routes, status }
|
||||
private enum Section: Int, CaseIterable { case order, points, routes, status, creationResult, shootResult }
|
||||
/// 点位与航线使用后端稳定 ID,刷新时保留有效选择。
|
||||
private enum Item: Hashable { case order, point(Int), route(Int), emptyPoints, emptyRoutes, status }
|
||||
private enum Item: Hashable { case order, point(Int), route(Int), emptyPoints, emptyRoutes, status, creationResult, shootResult }
|
||||
|
||||
private lazy var collectionView = UICollectionView(frame: .zero, collectionViewLayout: makeLayout())
|
||||
private var dataSource: UICollectionViewDiffableDataSource<Section, Item>!
|
||||
private let footerView = DronePreparationFooterView()
|
||||
private let refreshControl = UIRefreshControl()
|
||||
private var refreshTask: Task<Void, Never>?
|
||||
private var planeStatusTask: Task<Void, Never>?
|
||||
private var creationTask: Task<Void, Never>?
|
||||
private var isStartingCreation = false
|
||||
private let planeStatusPollDelay: @Sendable (UInt64) async throws -> Void
|
||||
private var isRefreshing = false
|
||||
private var isPageVisible = false
|
||||
private var isApplicationActive = false
|
||||
private var automaticallyOpenedTaskID: Int?
|
||||
private let taskControllerFactory: @MainActor (Int, DroneDockHomeOrder, DroneDockHomeContext) -> UIViewController
|
||||
|
||||
/// 注入只读状态源,不初始化本地演示订单或任务。
|
||||
init(viewModel: DroneDockPreparationViewModel) {
|
||||
/// 注入准备页业务状态和轮询等待,便于验证任务创建及页面生命周期。
|
||||
init(viewModel: DroneDockPreparationViewModel,
|
||||
taskControllerFactory: (@MainActor (Int, DroneDockHomeOrder, DroneDockHomeContext) -> UIViewController)? = nil,
|
||||
planeStatusPollDelay: @escaping @Sendable (UInt64) async throws -> Void = { try await Task.sleep(nanoseconds: $0) }) {
|
||||
self.viewModel = viewModel
|
||||
self.taskControllerFactory = taskControllerFactory ?? { taskID, order, context in
|
||||
DroneDockShootingTaskEntry.makeController(taskID: taskID, order: order, context: context)
|
||||
}
|
||||
self.planeStatusPollDelay = planeStatusPollDelay
|
||||
super.init(nibName: nil, bundle: nil)
|
||||
hidesBottomBarWhenPushed = true
|
||||
}
|
||||
@@ -29,7 +43,12 @@ final class DroneDockRemotePreparationViewController: BaseViewController {
|
||||
@available(*, unavailable)
|
||||
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
|
||||
|
||||
deinit { refreshTask?.cancel() }
|
||||
deinit {
|
||||
refreshTask?.cancel()
|
||||
planeStatusTask?.cancel()
|
||||
creationTask?.cancel()
|
||||
NotificationCenter.default.removeObserver(self)
|
||||
}
|
||||
|
||||
override func setupNavigationBar() {
|
||||
title = "拍摄准备"
|
||||
@@ -47,6 +66,7 @@ final class DroneDockRemotePreparationViewController: BaseViewController {
|
||||
collectionView.register(DronePreparationPointCell.self, forCellWithReuseIdentifier: "point")
|
||||
collectionView.register(DronePreparationRouteCell.self, forCellWithReuseIdentifier: "route")
|
||||
collectionView.register(DronePreparationEmptyCell.self, forCellWithReuseIdentifier: "message")
|
||||
collectionView.register(DroneDockTaskCreationResultCell.self, forCellWithReuseIdentifier: "creationResult")
|
||||
collectionView.register(DronePreparationHeadingView.self,
|
||||
forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader,
|
||||
withReuseIdentifier: "heading")
|
||||
@@ -70,22 +90,35 @@ final class DroneDockRemotePreparationViewController: BaseViewController {
|
||||
override func bindActions() {
|
||||
refreshControl.addTarget(self, action: #selector(refreshTriggered), for: .valueChanged)
|
||||
footerView.onRefreshTap = { [weak self] in self?.refreshPreparation() }
|
||||
footerView.onPrimaryTap = { [weak self] in self?.createSelectedTask() }
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(applicationWillResignActive),
|
||||
name: UIApplication.willResignActiveNotification, object: nil)
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(applicationDidBecomeActive),
|
||||
name: UIApplication.didBecomeActiveNotification, object: nil)
|
||||
}
|
||||
|
||||
override func viewWillAppear(_ animated: Bool) {
|
||||
super.viewWillAppear(animated)
|
||||
isPageVisible = true
|
||||
isApplicationActive = UIApplication.shared.applicationState == .active
|
||||
refreshPreparation()
|
||||
}
|
||||
|
||||
override func viewWillDisappear(_ animated: Bool) {
|
||||
super.viewWillDisappear(animated)
|
||||
isPageVisible = false
|
||||
stopPlaneStatusPolling()
|
||||
refreshTask?.cancel()
|
||||
creationTask?.cancel()
|
||||
isStartingCreation = false
|
||||
isRefreshing = false
|
||||
refreshControl.endRefreshing()
|
||||
viewModel.invalidate()
|
||||
}
|
||||
|
||||
private func refreshPreparation() {
|
||||
guard !isStartingCreation, !viewModel.isCreatingTask, !viewModel.isSummoning else { refreshControl.endRefreshing(); return }
|
||||
startPlaneStatusPolling()
|
||||
guard !isRefreshing else { return }
|
||||
isRefreshing = true
|
||||
refreshTask = Task { @MainActor [weak self] in
|
||||
@@ -109,13 +142,93 @@ final class DroneDockRemotePreparationViewController: BaseViewController {
|
||||
}
|
||||
}
|
||||
|
||||
/// 首次创建并召唤;成功后进入原任务,再次查看不经过创建或召唤接口。
|
||||
private func createSelectedTask() {
|
||||
guard !isRefreshing, !isStartingCreation else { return }
|
||||
if viewModel.canOpenShootingTask {
|
||||
_ = openShootingTask(automatically: false)
|
||||
return
|
||||
}
|
||||
guard viewModel.isPrimaryActionEnabled else { return }
|
||||
isStartingCreation = true
|
||||
applySnapshot(animated: false)
|
||||
creationTask = Task { @MainActor [weak self] in
|
||||
guard let self else { return }
|
||||
await self.viewModel.prepareAndSummon { [weak self] in self?.applySnapshot(animated: false) }
|
||||
guard !Task.isCancelled else { return }
|
||||
self.isStartingCreation = false
|
||||
if self.openShootingTask(automatically: true) { return }
|
||||
self.applySnapshot(animated: true) { [weak self] in
|
||||
guard let self,
|
||||
let path = self.dataSource.indexPath(for: .shootResult) ?? self.dataSource.indexPath(for: .creationResult) else { return }
|
||||
self.collectionView.scrollToItem(at: path, at: .top, animated: true)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 只在准备页可见且召唤已成功时导航一次;返回后允许主动查看同一任务。
|
||||
@discardableResult
|
||||
private func openShootingTask(automatically: Bool) -> Bool {
|
||||
guard isPageVisible, isApplicationActive, viewModel.canOpenShootingTask,
|
||||
let taskID = viewModel.createdTask?.taskID,
|
||||
let navigationController, navigationController.topViewController === self,
|
||||
!automatically || automaticallyOpenedTaskID != taskID else { return false }
|
||||
automaticallyOpenedTaskID = taskID
|
||||
let controller = taskControllerFactory(taskID, viewModel.order, viewModel.context)
|
||||
navigationController.pushViewController(controller, animated: true)
|
||||
return true
|
||||
}
|
||||
|
||||
/// 设备请求与购买快照各自更新,轮询不重载列表或改变点位、航线选择。
|
||||
private func startPlaneStatusPolling() {
|
||||
guard isPageVisible, isApplicationActive else { return }
|
||||
stopPlaneStatusPolling()
|
||||
let delay = planeStatusPollDelay
|
||||
planeStatusTask = Task { @MainActor [weak self] in
|
||||
while !Task.isCancelled {
|
||||
guard let interval = await self?.refreshPlaneStatusIfVisible() else { return }
|
||||
do { try await delay(interval) }
|
||||
catch { return }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func refreshPlaneStatusIfVisible() async -> UInt64? {
|
||||
guard isPageVisible, isApplicationActive, !Task.isCancelled else { return nil }
|
||||
await viewModel.refreshPlaneStatus()
|
||||
guard isPageVisible, isApplicationActive, !Task.isCancelled else { return nil }
|
||||
if viewModel.errorMessage != nil {
|
||||
applySnapshot(animated: false)
|
||||
} else {
|
||||
footerView.apply(preparationViewModel: viewModel, isRefreshing: isRefreshing || isStartingCreation)
|
||||
}
|
||||
return viewModel.pollIntervalNanoseconds
|
||||
}
|
||||
|
||||
private func stopPlaneStatusPolling() {
|
||||
planeStatusTask?.cancel()
|
||||
planeStatusTask = nil
|
||||
viewModel.invalidatePlaneStatus()
|
||||
}
|
||||
|
||||
@objc private func applicationWillResignActive() {
|
||||
isApplicationActive = false
|
||||
stopPlaneStatusPolling()
|
||||
footerView.apply(preparationViewModel: viewModel, isRefreshing: isRefreshing || isStartingCreation)
|
||||
}
|
||||
|
||||
@objc private func applicationDidBecomeActive() {
|
||||
isApplicationActive = true
|
||||
startPlaneStatusPolling()
|
||||
}
|
||||
|
||||
private func configureDataSource() {
|
||||
dataSource = UICollectionViewDiffableDataSource<Section, Item>(collectionView: collectionView) { [weak self] list, path, item in
|
||||
guard let self else { return nil }
|
||||
switch item {
|
||||
case .order:
|
||||
let cell = list.dequeueReusableCell(withReuseIdentifier: "order", for: path) as! DronePreparationOrderCell
|
||||
cell.apply(homeOrder: self.viewModel.order, summary: self.viewModel.projectInfo?.order,
|
||||
cell.apply(homeOrder: self.viewModel.order, summary: self.viewModel.latestOrderSummary,
|
||||
usesHomeSnapshot: self.viewModel.projectInfo == nil && self.viewModel.errorMessage == nil)
|
||||
return cell
|
||||
case .point(let id):
|
||||
@@ -130,6 +243,20 @@ final class DroneDockRemotePreparationViewController: BaseViewController {
|
||||
cell.apply(route: route, selected: self.viewModel.selectedRouteID == id)
|
||||
}
|
||||
return cell
|
||||
case .creationResult:
|
||||
let cell = list.dequeueReusableCell(withReuseIdentifier: "creationResult", for: path) as! DroneDockTaskCreationResultCell
|
||||
cell.apply(task: self.viewModel.createdTask, errorMessage: self.viewModel.taskCreationErrorMessage,
|
||||
outcomeUnknown: self.viewModel.taskCreationOutcomeUnknown)
|
||||
return cell
|
||||
case .shootResult:
|
||||
let cell = list.dequeueReusableCell(withReuseIdentifier: "creationResult", for: path) as! DroneDockTaskCreationResultCell
|
||||
cell.applyShoot(taskID: self.viewModel.createdTask?.taskID,
|
||||
dataJSON: self.viewModel.shootDataJSON,
|
||||
errorMessage: self.viewModel.shootErrorMessage,
|
||||
isLoading: self.viewModel.isSummoning,
|
||||
hasSucceeded: self.viewModel.shootResponse != nil,
|
||||
outcomeUnknown: self.viewModel.summonOutcomeUnknown)
|
||||
return cell
|
||||
case .emptyPoints, .emptyRoutes, .status:
|
||||
let cell = list.dequeueReusableCell(withReuseIdentifier: "message", for: path) as! DronePreparationEmptyCell
|
||||
if item == .status {
|
||||
@@ -177,8 +304,15 @@ final class DroneDockRemotePreparationViewController: BaseViewController {
|
||||
} else {
|
||||
snapshot.appendItems([.status], toSection: .status)
|
||||
}
|
||||
if viewModel.createdTask != nil || (viewModel.errorMessage == nil && viewModel.projectInfo != nil && viewModel.taskCreationErrorMessage != nil) {
|
||||
snapshot.appendItems([.creationResult], toSection: .creationResult)
|
||||
}
|
||||
if viewModel.createdTask != nil,
|
||||
viewModel.isSummoning || viewModel.shootResponse != nil || viewModel.shootErrorMessage != nil {
|
||||
snapshot.appendItems([.shootResult], toSection: .shootResult)
|
||||
}
|
||||
snapshot.reconfigureItems(snapshot.itemIdentifiers.filter { existing.contains($0) })
|
||||
footerView.apply(preparationViewModel: viewModel, isRefreshing: isRefreshing)
|
||||
footerView.apply(preparationViewModel: viewModel, isRefreshing: isRefreshing || isStartingCreation)
|
||||
let previousPointCount = previous.sectionIdentifiers.contains(.points) ? previous.numberOfItems(inSection: .points) : 0
|
||||
let previousRouteCount = previous.sectionIdentifiers.contains(.routes) ? previous.numberOfItems(inSection: .routes) : 0
|
||||
let layoutChanged = previousPointCount != snapshot.numberOfItems(inSection: .points)
|
||||
@@ -212,11 +346,13 @@ final class DroneDockRemotePreparationViewController: BaseViewController {
|
||||
height = self.viewModel.routes.isEmpty ? .absolute(160) : .estimated(64)
|
||||
width = .fractionalWidth(1)
|
||||
case .status: height = .absolute(160); width = .fractionalWidth(1)
|
||||
case .creationResult: height = .absolute(380); width = .fractionalWidth(1)
|
||||
case .shootResult: height = .absolute(420); width = .fractionalWidth(1)
|
||||
}
|
||||
let item = NSCollectionLayoutItem(layoutSize: NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: height))
|
||||
let group = NSCollectionLayoutGroup.vertical(layoutSize: NSCollectionLayoutSize(widthDimension: width, heightDimension: height), subitems: [item])
|
||||
let layout = NSCollectionLayoutSection(group: group)
|
||||
layout.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 16, bottom: section == .status ? 12 : 0, trailing: 16)
|
||||
layout.contentInsets = NSDirectionalEdgeInsets(top: section == .shootResult ? 12 : 0, leading: 16, bottom: section == .status ? 12 : 0, trailing: 16)
|
||||
if section == .points, !self.viewModel.spots.isEmpty {
|
||||
layout.orthogonalScrollingBehavior = .continuous
|
||||
layout.interGroupSpacing = 8
|
||||
@@ -265,7 +401,9 @@ final class DroneDockRemotePreparationViewController: BaseViewController {
|
||||
extension DroneDockRemotePreparationViewController: UICollectionViewDelegate {
|
||||
/// 点位和航线只在当前成功快照中选择,刷新期间不切换旧配置。
|
||||
func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
|
||||
guard !isRefreshing, viewModel.projectInfo != nil, viewModel.errorMessage == nil,
|
||||
guard !isRefreshing, !isStartingCreation, !viewModel.isCreatingTask,
|
||||
viewModel.createdTask == nil, !viewModel.taskCreationOutcomeUnknown,
|
||||
viewModel.projectInfo != nil, viewModel.errorMessage == nil,
|
||||
let item = dataSource.itemIdentifier(for: indexPath) else { return false }
|
||||
switch item {
|
||||
case .point(let id): return viewModel.spots.first { $0.id == id }?.available == true
|
||||
@@ -276,7 +414,9 @@ extension DroneDockRemotePreparationViewController: UICollectionViewDelegate {
|
||||
|
||||
/// 更新本地点位、航线及底部摘要,不创建拍摄任务。
|
||||
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
||||
guard !isRefreshing, let item = dataSource.itemIdentifier(for: indexPath) else { return }
|
||||
guard !isRefreshing, !isStartingCreation, !viewModel.isCreatingTask,
|
||||
viewModel.createdTask == nil, !viewModel.taskCreationOutcomeUnknown,
|
||||
let item = dataSource.itemIdentifier(for: indexPath) else { return }
|
||||
switch item {
|
||||
case .point(let id): viewModel.selectSpot(id: id)
|
||||
case .route(let id): viewModel.selectRoute(id: id)
|
||||
|
||||
Reference in New Issue
Block a user