167 lines
7.5 KiB
Swift
167 lines
7.5 KiB
Swift
import Foundation
|
|
|
|
/// 拍摄准备状态:只读取张工的已购配置,不创建飞行任务。
|
|
final class DroneDockPreparationViewModel {
|
|
/// 页面进入时固定的账号、景区及门店范围。
|
|
let context: DroneDockHomeContext
|
|
/// 首页订单仅提供身份和初次加载时的摘要。
|
|
let order: DroneDockHomeOrder
|
|
/// 张工返回的订单购买范围及最新次数。
|
|
private(set) var projectInfo: DroneDockProjectInfoResponse?
|
|
/// 购买配置是否尚未读取完成。
|
|
private(set) var isLoading = false
|
|
/// 购买配置或请求身份错误。
|
|
private(set) var errorMessage: String?
|
|
/// 已购且可用的当前点位。
|
|
private(set) var selectedSpotID: Int?
|
|
/// 当前点位下已购且可用的航线。
|
|
private(set) var selectedRouteID: Int?
|
|
|
|
private let service: any DroneDockPreparationServing
|
|
private let currentContext: @MainActor @Sendable () -> DroneDockHomeContext
|
|
private var generation = 0
|
|
|
|
/// 注入只读接口及最新会话,不绑定默认 MainActor。
|
|
init(context: DroneDockHomeContext, order: DroneDockHomeOrder,
|
|
service: any DroneDockPreparationServing,
|
|
currentContext: @escaping @MainActor @Sendable () -> DroneDockHomeContext) {
|
|
self.context = context
|
|
self.order = order
|
|
self.service = service
|
|
self.currentContext = currentContext
|
|
}
|
|
|
|
/// 只展示购买快照中的点位,保持张工返回顺序。
|
|
var spots: [DroneDockPurchasedSpot] { projectInfo?.spots ?? [] }
|
|
/// 当前有效点位。
|
|
var selectedSpot: DroneDockPurchasedSpot? { spots.first { $0.id == selectedSpotID } }
|
|
/// 只展示当前已购点位中的航线。
|
|
var routes: [DroneDockPurchasedAirLine] { selectedSpot?.airLines ?? [] }
|
|
/// 当前有效航线。
|
|
var selectedRoute: DroneDockPurchasedAirLine? { routes.first { $0.id == selectedRouteID } }
|
|
/// 进入页面后直接请求购买快照,不等待定位或设备接口。
|
|
func refresh() async {
|
|
generation &+= 1
|
|
let requestGeneration = generation
|
|
isLoading = true
|
|
errorMessage = nil
|
|
defer { if generation == requestGeneration { isLoading = false } }
|
|
guard await accepts(requestGeneration) else { return }
|
|
guard context.canLoad, let number = DroneDockHomeMetric.usableText(order.orderNumber),
|
|
let projectID = order.projectID, projectID > 0 else {
|
|
clearProject()
|
|
errorMessage = "缺少当前景区、订单或套餐信息,请返回工作台重新进入"
|
|
return
|
|
}
|
|
|
|
let projectResult: Result<DroneDockProjectInfoResponse, Error>
|
|
do {
|
|
projectResult = .success(try await service.projectInfo(scenicID: context.scenicID, projectID: projectID, orderNumber: number))
|
|
} catch {
|
|
projectResult = .failure(error)
|
|
}
|
|
guard await accepts(requestGeneration) else { return }
|
|
switch projectResult {
|
|
case .success(let value) where value.order.orderNumber == number && value.order.projectID == projectID:
|
|
projectInfo = value
|
|
restoreValidSelection()
|
|
case .success:
|
|
clearProject()
|
|
errorMessage = "接口返回的订单或套餐与当前订单不一致,请刷新重试"
|
|
case .failure(let error):
|
|
clearProject()
|
|
errorMessage = Self.message(for: error)
|
|
}
|
|
}
|
|
|
|
/// 不可用或可用性未知的点位不能选中,切点后选择首条明确可用的已购航线。
|
|
func selectSpot(id: Int) {
|
|
guard !isLoading, spots.contains(where: { $0.id == id && $0.available == true }), selectedSpotID != id else { return }
|
|
selectedSpotID = id
|
|
selectedRouteID = routes.first(where: { $0.available == true })?.id
|
|
}
|
|
|
|
/// 忽略其他点位、不可用及身份不明的航线选择。
|
|
func selectRoute(id: Int) {
|
|
guard !isLoading, selectedSpot?.available == true,
|
|
routes.contains(where: { $0.id == id && $0.available == true }) else { return }
|
|
selectedRouteID = id
|
|
}
|
|
|
|
/// 离开后使购买快照请求失效,避免旧响应回填。
|
|
func invalidate() {
|
|
generation &+= 1
|
|
isLoading = false
|
|
errorMessage = nil
|
|
clearProject()
|
|
}
|
|
|
|
/// 当前接口没有机巢连接信息,不能由购买权限推导设备状态。
|
|
var connectionTitle: String { "机巢连接待确认" }
|
|
/// 设备连接未知时使用中性色。
|
|
var connectionTone: DroneDockStatusTone { .gray }
|
|
/// 以购买快照中的名称展示当前选择。
|
|
var selectionSummary: String {
|
|
guard let selectedSpot else { return "请选择拍摄点" }
|
|
guard let selectedRoute else { return "\(selectedSpot.name) · 未选航线" }
|
|
return "\(selectedSpot.name) · \(selectedRoute.name)"
|
|
}
|
|
/// 当前未读取设备状态,也尚未派机。
|
|
var deviceStatusText: String { "状态待确认" }
|
|
/// 准备阶段尚未派机。
|
|
var deviceTone: DroneDockStatusTone { .gray }
|
|
/// 文案反映购买权限,不开放实际召唤操作。
|
|
var primaryButtonTitle: String {
|
|
if projectInfo?.order.shotRemain == 0 { return "拍摄次数已用完" }
|
|
if let info = projectInfo, info.order.canCreateTask != true { return "暂不可召唤" }
|
|
return "确认并召唤"
|
|
}
|
|
/// 检查购买范围与额度;条件有效后仍提示召唤尚未开放。
|
|
var admissionMessage: String {
|
|
if isLoading { return "正在获取拍摄准备信息" }
|
|
if let errorMessage { return errorMessage }
|
|
guard let info = projectInfo else { return "正在加载拍摄准备信息" }
|
|
if info.order.shotRemain == 0 { return "当前订单拍摄次数已用完" }
|
|
guard let remaining = info.order.shotRemain, remaining > 0, info.order.shotTotal != nil else { return "拍摄次数待确认,请刷新状态" }
|
|
guard info.order.canCreateTask == true else { return "当前订单暂不可新增拍摄任务,请刷新状态" }
|
|
if spots.isEmpty { return "当前暂无可用拍摄点" }
|
|
guard let spot = selectedSpot, spot.available == true else { return "请选择可用拍摄点" }
|
|
if routes.isEmpty { return "当前拍摄点暂无可用航线" }
|
|
guard selectedRoute?.available == true else { return "请选择可用航线" }
|
|
return "召唤功能暂未开放"
|
|
}
|
|
|
|
private func restoreValidSelection() {
|
|
if !spots.contains(where: { $0.id == selectedSpotID && $0.available == true }) {
|
|
selectedSpotID = spots.first(where: { $0.available == true })?.id
|
|
selectedRouteID = nil
|
|
}
|
|
if !routes.contains(where: { $0.id == selectedRouteID && $0.available == true }) {
|
|
selectedRouteID = routes.first(where: { $0.available == true })?.id
|
|
}
|
|
}
|
|
|
|
private func clearProject() {
|
|
projectInfo = nil
|
|
selectedSpotID = nil
|
|
selectedRouteID = nil
|
|
}
|
|
|
|
private func accepts(_ requestGeneration: Int) async -> Bool {
|
|
guard generation == requestGeneration, !Task.isCancelled else { return false }
|
|
let latest = await currentContext()
|
|
guard generation == requestGeneration, !Task.isCancelled else { return false }
|
|
guard context.hasSameIdentity(as: latest) else {
|
|
invalidate()
|
|
errorMessage = "账号或景区已变化,请返回工作台重新进入"
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
private static func message(for error: Error) -> String? {
|
|
error is CancellationError ? nil : error.localizedDescription
|
|
}
|
|
|
|
}
|