feat: 完善机巢模拟调试与素材交付交互

增加禁飞期间的设备指令拦截及本地模拟拍摄流程;适配订单素材库和景区空境库,完善素材筛选、预览与下载;订单交付入口改为方式选择弹窗并复用云端上传。补充单元测试、接口记录和模拟器截图。
This commit is contained in:
2026-10-30 09:59:11 +08:00
parent e827bf8725
commit b95b251de1
100 changed files with 7256 additions and 500 deletions
@@ -0,0 +1,39 @@
import Foundation
/// 朱工开始拍摄服务,只使用已创建并召唤的订单飞行任务身份。
protocol DroneDockStartShooting: Sendable {
/// 提交一次开拍请求;调用方负责校验执行门禁并阻止重复提交。
@MainActor
func startShooting(taskID: Int) async throws -> DroneDockStartShootResponse
}
/// start-shoot 合同适配;禁飞期间真实传输仍由统一客户端阻断,仅用 Mock 验证。
@MainActor
final class DroneDockStartShootAPI: DroneDockStartShooting {
private let client: APIClient
/// 注入统一客户端,测试使用不会联网的本地模拟传输。
init(client: APIClient) { self.client = client }
/// 仅 POST 任务 ID,不附带飞机、航线或未约定的模拟参数,也不自动重试。
func startShooting(taskID: Int) async throws -> DroneDockStartShootResponse {
try Task.checkCancellation()
guard taskID > 0 else { throw APIError.networkFailed("缺少有效拍摄任务 ID,请返回任务页确认") }
let response: DroneDockStartShootResponse = try await client.send(APIRequest(
method: .post, path: "/api/app/airport/start-shoot", body: StartShootBody(taskID: taskID)
))
guard let returnedID = response.orderFlightTaskID, returnedID > 0, returnedID == taskID,
response.orderFlightTask?.id == nil || response.orderFlightTask?.id == taskID else {
throw APIError.decodeFailed("开始拍摄回执的任务身份缺失或不一致,请核对原任务状态")
}
return response
}
/// 开拍只提交订单飞行任务身份,不使用航线任务 ID。
private struct StartShootBody: Encodable {
let taskID: Int
/// 朱工开拍请求的 JSON 字段。
private enum CodingKeys: String, CodingKey { case taskID = "id" }
}
}
@@ -0,0 +1,9 @@
import Foundation
/// 原素材的选择身份;文件 ID 只在所属任务内唯一,成片不计入原素材选择。
struct DroneDockMaterialSelectionID: Hashable, Sendable {
/// 所属订单飞行任务。
let taskID: Int
/// 任务中的原素材 ID。
let fileID: Int
}
@@ -22,7 +22,7 @@ struct DroneDockShootingResponse: Decodable, Equatable, Sendable {
let metrics: [DroneDockHomeMetric]
/// 景区设备连接与轮询建议,不在客户端改选推荐飞机。
let planeStatus: DroneDockShootingPlaneStatus?
/// 服务端执行门禁只用于展示,本次接入不执行其动作。
/// 执行门禁参与展示及本地模拟开拍校验,不能解除真实飞控禁令。
let executeGate: DroneDockShootingExecuteGate?
/// 服务端拍摄阶段和页面提示。
let ui: DroneDockShootingUI?
@@ -169,7 +169,7 @@ struct DroneDockShootingExecuteGate: Decodable, Equatable, Sendable {
let code: String?
/// 服务端门禁说明。
let message: String?
/// 服务端建议动作,本阶段仅展示。
/// 服务端建议动作;本地模拟开拍要求明确为 start_shoot。
let primaryAction: String?
/// 服务端建议动作名称。
let primaryActionLabel: String?
@@ -0,0 +1,58 @@
import Foundation
/// start-shoot 的受理回执;不凭请求成功推断飞行、拍摄或素材已完成。
struct DroneDockStartShootResponse: Decodable, Equatable, Sendable {
/// 文档约定的订单飞行任务 ID,API 必须核对其与请求一致。
let orderFlightTaskID: Int?
/// 服务端提供的任务摘要,嵌套身份存在时同样参与核对。
let orderFlightTask: DroneDockShootingTaskIdentity?
/// 当前任务锁定的飞机,不由客户端选择或覆盖。
let planeID: Int?
/// 服务端任务 UUID,用于后续核对,不等于订单飞行任务 ID。
let taskUUID: String?
/// 服务端返回的机场标识,仅保留已有值。
let airportSN: String?
/// 服务端受理后的阶段文案,最新设备状态由 shooting 单次回查确认。
let ui: DroneDockShootingUI?
/// 完整 data 的格式化 JSON,保留未知字段与原始类型供模拟联调展示。
let dataJSON: String
/// 朱工开拍回执字段映射。
private enum CodingKeys: String, CodingKey {
case orderFlightTaskID = "order_flight_task_id", orderFlightTask = "order_flight_task"
case planeID = "plane_id", taskUUID = "task_uuid", airportSN = "airport_sn", ui
}
/// 仅解析响应实际提供的字段,不补造任务身份或成功状态。
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
orderFlightTaskID = try container.decodeIfPresent(Int.self, forKey: .orderFlightTaskID)
orderFlightTask = try container.decodeIfPresent(DroneDockShootingTaskIdentity.self, forKey: .orderFlightTask)
planeID = try container.decodeIfPresent(Int.self, forKey: .planeID)
taskUUID = try container.decodeIfPresent(String.self, forKey: .taskUUID)
airportSN = try container.decodeIfPresent(String.self, forKey: .airportSN)
ui = try container.decodeIfPresent(DroneDockShootingUI.self, forKey: .ui)
let raw = try DroneDockHomeJSONValue(from: decoder)
let encoder = JSONEncoder()
encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]
dataJSON = String(decoding: try encoder.encode(DroneDockStartShootJSONSnapshot(value: raw)), as: UTF8.self)
}
}
/// 将已有 JSON 值完整格式化,不给模拟响应添加后端未提供的字段。
private struct DroneDockStartShootJSONSnapshot: Encodable {
let value: DroneDockHomeJSONValue
/// 保留字符串、数字、布尔、空值和嵌套对象,不追加业务码包裹。
func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch value {
case .object(let object): try container.encode(object.mapValues { DroneDockStartShootJSONSnapshot(value: $0) })
case .array(let array): try container.encode(array.map { DroneDockStartShootJSONSnapshot(value: $0) })
case .string(let text): try container.encode(text)
case .number(let number): try container.encode(number)
case .bool(let flag): try container.encode(flag)
case .null: try container.encodeNil()
}
}
}
@@ -26,11 +26,11 @@ enum DroneDockTaskDisplayPhase: String, Equatable, Sendable {
case readError
}
/// 任务状态区可呈现的动作;开始与恢复在本轮仅禁用展示,不发送飞控。
/// 任务状态区可呈现的动作;开始仅在明确注入本地模拟服务时可用,真实飞控保持禁用。
enum DroneDockTaskDisplayAction: Equatable, Sendable {
/// 重新读取同一任务。
case refresh
/// 开始拍摄占位动作,本轮禁用。
/// 开始拍摄动作;真实入口无服务,只有本地模拟可提交验证。
case start
/// 恢复拍摄占位动作,本轮禁用。
case resume
@@ -56,7 +56,7 @@ struct DroneDockTaskPresentation: Equatable, Sendable {
let action: DroneDockTaskDisplayAction
/// 主动作名称,无动作时为 nil。
let actionTitle: String?
/// 是否可触发动作;未对接的开始、恢复和重新召唤始终为 false。
/// 是否可触发动作;真实开始、恢复和重新召唤保持禁用。
let isActionEnabled: Bool
/// 主动作下方的真实设备限制说明,未提供时不展示。
let actionNote: String?
@@ -24,6 +24,25 @@ final class DroneDockMaterialsViewModel {
private(set) var tasksErrorMessage: String?
/// 点位标签始终来自整单购买快照,不从当前任务推导。
var spots: [DroneDockMaterialSpot] { detail?.spots ?? [] }
/// 当前范围内已选择的就绪原素材,不包含独立交付成片。
private(set) var selectedMaterialIDs: Set<DroneDockMaterialSelectionID> = []
/// 只有完整读取成功且具备有效文件地址的原素材允许选择。
var selectableMaterialIDs: Set<DroneDockMaterialSelectionID> {
guard detail != nil, !isLoading, !isLoadingTasks,
detailErrorMessage == nil, tasksErrorMessage == nil else { return [] }
return Set(tasks.flatMap { task in
task.materials.filter { $0.previewURL != nil }.map {
DroneDockMaterialSelectionID(taskID: task.id, fileID: $0.id)
}
})
}
/// 当前选择数量仅用于界面展示,不表示已经下载或保存。
var selectedCount: Int { selectedMaterialIDs.count }
/// 空列表不展示全选;全部当前可用原素材被选中时才标记全选。
var allDownloadableSelected: Bool {
let selectable = selectableMaterialIDs
return !selectable.isEmpty && selectedMaterialIDs == selectable
}
private let service: any DroneDockMaterialsServing
private let currentContext: @MainActor @Sendable () -> DroneDockHomeContext
@@ -39,6 +58,38 @@ final class DroneDockMaterialsViewModel {
self.currentContext = currentContext
}
/// 勾选只修改本地状态;无效、未就绪或其他任务的素材不会进入选择集合。
@MainActor
func toggleSelection(taskID: Int, fileID: Int) {
guard acceptsSelectionContext() else { return }
let identifier = DroneDockMaterialSelectionID(taskID: taskID, fileID: fileID)
guard selectableMaterialIDs.contains(identifier) else { return }
if selectedMaterialIDs.contains(identifier) {
selectedMaterialIDs.remove(identifier)
} else {
selectedMaterialIDs.insert(identifier)
}
}
/// 全选仅覆盖当前点位可用的原素材,不跨点位、订单或成片扩大范围。
@MainActor
func toggleSelectAll() {
guard acceptsSelectionContext() else { return }
let selectable = selectableMaterialIDs
guard !selectable.isEmpty else { return }
selectedMaterialIDs = selectedMaterialIDs == selectable ? [] : selectable
}
@MainActor
private func acceptsSelectionContext() -> Bool {
guard context.hasSameIdentity(as: currentContext()) else {
invalidate()
detailErrorMessage = "账号或景区已变化,请返回工作台重新进入"
return false
}
return true
}
/// 先刷新整单摘要,再读取任务;保留仍在购买快照中的点位筛选。
@MainActor
func refresh() async {
@@ -156,6 +207,7 @@ final class DroneDockMaterialsViewModel {
@MainActor
private func clearTasks() {
tasks = []
selectedMaterialIDs = []
total = nil
tasksErrorMessage = nil
}
@@ -43,6 +43,7 @@ final class DroneDockPreparationViewModel {
private let planeStatusService: any DroneDockPlaneStatusServing
private let taskCreationService: any DroneDockTaskCreating
private let shootService: any DroneDockShootServing
private let allowsTaskSubmission: Bool
private let currentContext: @MainActor @Sendable () -> DroneDockHomeContext
private var generation = 0
private var planeStatusGeneration = 0
@@ -62,6 +63,7 @@ final class DroneDockPreparationViewModel {
planeStatusService: any DroneDockPlaneStatusServing,
taskCreationService: any DroneDockTaskCreating,
shootService: any DroneDockShootServing,
allowsTaskSubmission: Bool = true,
currentContext: @escaping @MainActor @Sendable () -> DroneDockHomeContext) {
self.context = context
self.order = order
@@ -69,12 +71,13 @@ final class DroneDockPreparationViewModel {
self.planeStatusService = planeStatusService
self.taskCreationService = taskCreationService
self.shootService = shootService
self.allowsTaskSubmission = allowsTaskSubmission
self.currentContext = currentContext
}
/// 当前阶段只创建待飞任务,不以设备电量或位置作为创建任务条件。
var canCreateTask: Bool {
guard context.canLoad, !creationContextInvalidated, !isLoading, !isCreatingTask,
guard allowsTaskSubmission, context.canLoad, !creationContextInvalidated, !isLoading, !isCreatingTask,
!hasCreatedTask, !taskCreationOutcomeUnknown, errorMessage == nil,
let info = projectInfo, info.order.canCreateTask == true,
let total = info.order.shotTotal, total > 0,
@@ -86,6 +89,9 @@ final class DroneDockPreparationViewModel {
return info.order.orderNumber == number && info.order.projectID == projectID
}
/// 当前入口是否被禁飞限制禁止提交,用于提供独立模拟入口而不放开真实写入。
var isTaskSubmissionBlocked: Bool { !allowsTaskSubmission }
/// 提交中和已有回执/不确定结果时冻结点位航线,避免显示与任务归属不一致。
var isTaskSelectionLocked: Bool {
isCreatingTask || isSummoning || isPerformingPrimaryAction || hasCreatedTask || taskCreationOutcomeUnknown
@@ -107,7 +113,7 @@ final class DroneDockPreparationViewModel {
/// 首次允许创建再召唤;明确业务拒绝后可对同任务重试,成功和未知结果锁定。
var isPrimaryActionEnabled: Bool {
guard context.canLoad, !creationContextInvalidated, !isLoading, !isCreatingTask,
guard allowsTaskSubmission, context.canLoad, !creationContextInvalidated, !isLoading, !isCreatingTask,
!isSummoning, !isPerformingPrimaryAction, !taskCreationOutcomeUnknown,
!hasSubmittedSummon, !summonOutcomeUnknown else { return false }
if let createdTask { return createdTask.taskID > 0 }
@@ -117,7 +123,8 @@ final class DroneDockPreparationViewModel {
/// 主按钮串接创建与召唤;已有任务跳过创建,成功后的导航由页面处理。
@MainActor
func prepareAndSummon(onStateChange: @escaping @MainActor () -> Void = {}) async {
guard isPrimaryActionEnabled else { return }
// 禁飞调试在整条链开始前阻断,避免先残留真实待飞任务再拦截召唤。
guard allowsTaskSubmission, isPrimaryActionEnabled else { return }
primaryActionGeneration &+= 1
let actionGeneration = primaryActionGeneration
isPerformingPrimaryAction = true
@@ -136,7 +143,7 @@ final class DroneDockPreparationViewModel {
/// 对明确创建成功的任务发送一次召唤;只有明确业务拒绝允许之后再次提交。
@MainActor
private func summonCreatedTask(onStateChange: @escaping @MainActor () -> Void) async {
guard !isCreatingTask, !isSummoning, !taskCreationOutcomeUnknown,
guard allowsTaskSubmission, !isCreatingTask, !isSummoning, !taskCreationOutcomeUnknown,
!hasSubmittedSummon, !summonOutcomeUnknown,
let taskID = createdTask?.taskID, taskID > 0 else { return }
summonGeneration &+= 1
@@ -162,8 +169,13 @@ final class DroneDockPreparationViewModel {
hasSubmittedSummon = true
summonOutcomeUnknown = false
} catch {
if error is DeviceCommandDebugBlockedError, summonGeneration == requestGeneration {
didSendSummon = false
}
guard acceptsSummon(requestGeneration, taskID: taskID) else { return }
if case APIError.serverCode = error {
if error is DeviceCommandDebugBlockedError {
shootErrorMessage = DeviceCommandDebugPolicy.blockedMessage
} else if case APIError.serverCode = error {
shootErrorMessage = error.localizedDescription
} else {
markSummonUnknown(reason: error.localizedDescription)
@@ -189,7 +201,7 @@ final class DroneDockPreparationViewModel {
/// 点击后先校验最新购买配置,再发送一次创建请求;成功留在准备页供核对。
@MainActor
func createTask(onStateChange: @escaping @MainActor () -> Void = {}) async {
guard canCreateTask, let spotID = selectedSpotID, let routeID = selectedRouteID,
guard allowsTaskSubmission, canCreateTask, let spotID = selectedSpotID, let routeID = selectedRouteID,
let number = order.orderNumber, let projectID = order.projectID else { return }
taskCreationGeneration &+= 1
let requestGeneration = taskCreationGeneration
@@ -238,8 +250,13 @@ final class DroneDockPreparationViewModel {
hasCreatedTask = true
taskCreationOutcomeUnknown = false
} catch {
if error is DeviceCommandDebugBlockedError, taskCreationGeneration == requestGeneration {
didSendTaskCreation = false
}
guard acceptsTaskCreation(requestGeneration) else { return }
if didSendTaskCreation {
if error is DeviceCommandDebugBlockedError {
taskCreationErrorMessage = DeviceCommandDebugPolicy.blockedMessage
} else if didSendTaskCreation {
if case APIError.serverCode = error {
taskCreationErrorMessage = error.localizedDescription
} else {
@@ -414,6 +431,7 @@ final class DroneDockPreparationViewModel {
if isCreatingTask { return "正在创建任务…" }
if isSummoning { return "正在召唤无人机…" }
if hasSubmittedSummon { return "查看拍摄任务" }
if !allowsTaskSubmission { return "仅限模拟调试" }
if summonOutcomeUnknown { return "召唤结果待核对" }
if hasCreatedTask { return shootErrorMessage == nil ? "确认并召唤" : "重试召唤" }
if taskCreationOutcomeUnknown { return "结果待核对" }
@@ -426,6 +444,7 @@ final class DroneDockPreparationViewModel {
if isCreatingTask { return "正在创建待飞任务,请勿重复点击" }
if isSummoning { return "任务已创建,正在提交召唤请求" }
if creationContextInvalidated, let errorMessage { return errorMessage }
if !allowsTaskSubmission && !hasSubmittedSummon { return DeviceCommandDebugPolicy.blockedMessage }
if let createdTask {
if hasSubmittedSummon { return "任务 \(createdTask.taskID) 的召唤请求已提交,请等待任务状态更新" }
if let shootErrorMessage { return shootErrorMessage }
@@ -1,6 +1,6 @@
import Foundation
/// 真实拍摄任务页只读状态,只按准备页传入的既有任务 ID 读取 shooting。
/// 拍摄任务状态及可选开拍流程,始终复用同一任务;禁飞调试仅为本地模拟注入开拍服务。
final class DroneDockShootingTaskViewModel {
/// 页面绑定的账号、景区和门店身份。
let context: DroneDockHomeContext
@@ -14,27 +14,47 @@ final class DroneDockShootingTaskViewModel {
private(set) var isLoading = false
/// 读取失败或身份变化时的提示。
private(set) var errorMessage: String?
/// 开拍提交及其后单次状态核对期间禁用重复操作,保留之前的显示快照。
private(set) var isStartingShooting = false
/// 开拍业务拒绝、本地拦截或结果待核对的说明。
private(set) var startErrorMessage: String?
/// 已发送但无法确认的开拍请求,不因后续读取仍允许开始而自动解除。
private(set) var startOutcomeUnknown = false
/// 当前页面是否已收到明确开拍受理回执,收到后不会再次提交。
private(set) var hasSubmittedStart = false
/// 同任务开拍回执;受理成功不直接替代 shooting 的完整状态快照。
private(set) var startResponse: DroneDockStartShootResponse?
private let service: any DroneDockShootingServing
private let startService: (any DroneDockStartShooting)?
private let currentContext: @MainActor @Sendable () -> DroneDockHomeContext
private var generation = 0
private var identityInvalidated = false
private var startGeneration = 0
private var didSendStart = false
/// 只注入读取服务,不依赖创建、召唤、开始拍摄或控制服务。
/// 默认只读;只有显式注入开拍服务才提供开拍动作,当前正式入口不注入该服务。
init(context: DroneDockHomeContext, taskID: Int, order: DroneDockHomeOrder,
service: any DroneDockShootingServing,
startService: (any DroneDockStartShooting)? = nil,
currentContext: @escaping @MainActor @Sendable () -> DroneDockHomeContext) {
self.context = context
self.taskID = taskID
self.order = order
self.service = service
self.startService = startService
self.currentContext = currentContext
}
/// 页面可见时读取同一任务;刷新及普通失败保留旧快照,身份失效立即清除。
@MainActor
func refresh() async {
guard !isLoading, !identityInvalidated else { return }
await refreshSnapshot(allowWhileStarting: false)
}
@MainActor
private func refreshSnapshot(allowWhileStarting: Bool) async {
guard !isLoading, !identityInvalidated, !isStartingShooting || allowWhileStarting else { return }
generation &+= 1
let requestGeneration = generation
isLoading = true
@@ -68,11 +88,14 @@ final class DroneDockShootingTaskViewModel {
/// 使旧请求失效;同一身份离页或进入后台可保留快照,账号变化始终清除并停止旧任务读取。
@MainActor
func invalidate(preservingSnapshot: Bool = false) {
invalidateStartShooting()
generation &+= 1
isLoading = false
if !context.hasSameIdentity(as: currentContext()) { identityInvalidated = true }
if identityInvalidated {
response = nil
startResponse = nil
startErrorMessage = nil
errorMessage = "账号或景区已变化,请返回工作台重新进入"
} else {
if !preservingSnapshot { response = nil }
@@ -80,6 +103,83 @@ final class DroneDockShootingTaskViewModel {
}
}
/// 只在有效读取明确到达、锁定飞机并允许 start_shoot 后开放;默认真实页面保持禁用。
var canStartShooting: Bool {
startService != nil && context.canLoad && taskID > 0 && !identityInvalidated
&& !isLoading && !isStartingShooting && errorMessage == nil
&& !hasSubmittedStart && !startOutcomeUnknown
&& resolvedDisplayPhase().0 == .ready
&& response?.executeGate?.canExecute == true
&& response?.executeGate?.primaryAction == "start_shoot"
&& (response?.currentPlane?.id ?? 0) > 0
}
/// 提交一次开拍并单次读取同任务;失败保留原快照,不轮询、不重建、不自动重发。
@MainActor
func startShooting(onStateChange: @escaping @MainActor () -> Void = {}) async {
guard canStartShooting, let startService else { return }
guard context.hasSameIdentity(as: currentContext()) else {
identityInvalidated = true
invalidate()
onStateChange()
return
}
guard !Task.isCancelled else { return }
let planeID = response?.currentPlane?.id
startGeneration &+= 1
let requestGeneration = startGeneration
isStartingShooting = true
didSendStart = false
startErrorMessage = nil
onStateChange()
defer {
if startGeneration == requestGeneration {
if Task.isCancelled && didSendStart && !hasSubmittedStart { markStartUnknown() }
isStartingShooting = false
didSendStart = false
onStateChange()
}
}
guard acceptsStart(requestGeneration) else { return }
do {
didSendStart = true
let value = try await startService.startShooting(taskID: taskID)
guard acceptsStart(requestGeneration) else { return }
guard value.orderFlightTaskID == taskID,
value.orderFlightTask?.id == nil || value.orderFlightTask?.id == taskID,
value.orderFlightTask?.orderNumber == nil || value.orderFlightTask?.orderNumber == order.orderNumber,
value.orderFlightTask?.scenicID == nil || value.orderFlightTask?.scenicID == context.scenicID,
value.planeID == nil || value.planeID == planeID else {
throw APIError.decodeFailed("开始拍摄回执与当前任务或飞机不一致,请先核对任务状态")
}
startResponse = value
hasSubmittedStart = true
startOutcomeUnknown = false
onStateChange()
guard acceptsStart(requestGeneration) else { return }
await refreshSnapshot(allowWhileStarting: true)
} catch {
if error is DeviceCommandDebugBlockedError, startGeneration == requestGeneration { didSendStart = false }
guard acceptsStart(requestGeneration) else { return }
if error is DeviceCommandDebugBlockedError {
startErrorMessage = DeviceCommandDebugPolicy.blockedMessage
} else if case APIError.serverCode = error {
startErrorMessage = error.localizedDescription
} else {
markStartUnknown(reason: error.localizedDescription)
}
}
}
/// 离页或进入后台后拒绝迟到回执;已发而未确认的请求保留待核对锁。
@MainActor
func invalidateStartShooting() {
if isStartingShooting && didSendStart && !hasSubmittedStart { markStartUnknown() }
startGeneration &+= 1
isStartingShooting = false
didSendStart = false
}
/// 优先使用当前接口标题,缺失时展示当前接口的点位和航线名称。
var title: String {
if let title = usable(response?.header?.title) { return title }
@@ -111,13 +211,18 @@ final class DroneDockShootingTaskViewModel {
?? usable(response?.orderFlightTask?.statusLabel) ?? "状态待确认"
}
/// 保留服务端阶段说明与执行门禁原因;本模块不执行门禁给出的动作。
/// 保留服务端阶段说明;未确认的开拍结果明确提示核对,不伪装为正在拍摄。
var statusMessage: String {
if response == nil {
if let errorMessage { return errorMessage }
if isLoading { return "正在读取当前拍摄任务" }
}
if resolvedDisplayPhase().0 == .failed { return failureMessage }
if startOutcomeUnknown, let startErrorMessage { return startErrorMessage }
if resolvedDisplayPhase().0 == .ready {
if let startErrorMessage { return startErrorMessage }
if hasSubmittedStart { return "开始拍摄请求已受理,请刷新确认任务状态" }
}
return usable(response?.ui?.message) ?? usable(response?.executeGate?.message) ?? "—"
}
@@ -138,10 +243,15 @@ final class DroneDockShootingTaskViewModel {
let actionTitle: String?
let enabled: Bool
switch phase {
case .ready where hasSubmittedStart || startOutcomeUnknown:
action = .refresh
actionTitle = "刷新状态"
enabled = !identityInvalidated
case .ready:
action = .start
actionTitle = usable(response?.executeGate?.primaryActionLabel) ?? "开始拍摄"
enabled = false
actionTitle = isStartingShooting ? "正在提交开拍…"
: startErrorMessage == nil ? (usable(response?.executeGate?.primaryActionLabel) ?? "开始拍摄") : "重试开始拍摄"
enabled = canStartShooting
case .paused where response?.executeGate?.canExecute == true:
action = .resume
actionTitle = usable(response?.executeGate?.primaryActionLabel) ?? "继续拍摄"
@@ -161,7 +271,7 @@ final class DroneDockShootingTaskViewModel {
}
return DroneDockTaskPresentation(phase: phase, title: title, message: message,
deviceStatus: deviceStatusText, action: action, actionTitle: actionTitle,
isActionEnabled: enabled && !isLoading && !identityInvalidated && (errorMessage == nil || action == .refresh),
isActionEnabled: enabled && !isLoading && !isStartingShooting && !identityInvalidated && (errorMessage == nil || action == .refresh),
actionNote: phase == .failed ? retryUnavailableNote : nil)
}
@@ -189,6 +299,22 @@ final class DroneDockShootingTaskViewModel {
return !Task.isCancelled
}
@MainActor
private func acceptsStart(_ requestGeneration: Int) -> Bool {
guard startGeneration == requestGeneration else { return false }
guard context.hasSameIdentity(as: currentContext()), !identityInvalidated else {
identityInvalidated = true
invalidate()
return false
}
return !Task.isCancelled
}
private func markStartUnknown(reason: String? = nil) {
startOutcomeUnknown = true
startErrorMessage = (reason.map { "\($0)。" } ?? "") + "开始拍摄结果待核对,请刷新任务状态,勿重复提交"
}
private func matchesIdentity(_ value: DroneDockShootingResponse) -> Bool {
let taskIDs = [value.orderFlightTask?.id, value.task?.id].compactMap { $0 }
let orderNumbers = [value.orderNumber, value.orderFlightTask?.orderNumber,