279 lines
12 KiB
Swift
279 lines
12 KiB
Swift
import Foundation
|
|
|
|
/// 两个机巢页面共享的本地数据源,不请求接口、不发送设备指令,也不自动推进任务。
|
|
final class DroneDockDemoStore {
|
|
/// 当前登录用户与景区范围。
|
|
private(set) var context: DroneDockContext
|
|
|
|
private var ordersByContext: [DroneDockContext: [DroneShootingOrder]] = [:]
|
|
private var devicesByContext: [DroneDockContext: [DroneDockDevice]] = [:]
|
|
|
|
/// 注入初始上下文及可选数据,便于页面演示与独立规则测试。
|
|
init(
|
|
context: DroneDockContext = .demo,
|
|
orders: [DroneShootingOrder]? = nil,
|
|
devices: [DroneDockDevice]? = nil
|
|
) {
|
|
self.context = context
|
|
ordersByContext[context] = orders ?? DroneDockDemoData.orders(context: context)
|
|
devicesByContext[context] = devices ?? DroneDockDemoData.devices()
|
|
}
|
|
|
|
/// 只向页面提供当前用户在当前景区的订单。
|
|
var orders: [DroneShootingOrder] {
|
|
(ordersByContext[context] ?? []).filter {
|
|
$0.scenicID == context.scenicID && $0.assignedUserID == context.userID
|
|
}
|
|
}
|
|
|
|
/// 当前景区的无人机,不将横向浏览等同于执行资源选择。
|
|
var devices: [DroneDockDevice] { devicesByContext[context] ?? [] }
|
|
|
|
/// 当前演示上下文的今日统计;空场景不保留误导的数值。
|
|
var statistics: DroneDockDailyStatistics {
|
|
orders.isEmpty
|
|
? DroneDockDailyStatistics(orderCount: 0, revenueText: "¥0", successfulShootingCount: 0)
|
|
: DroneDockDailyStatistics(orderCount: 12, revenueText: "¥1,896", successfulShootingCount: 9)
|
|
}
|
|
|
|
/// 更换范围并保留旧范围中的本地任务,切回后仍可查看。
|
|
func switchContext(to context: DroneDockContext) {
|
|
if ordersByContext[context] == nil {
|
|
ordersByContext[context] = DroneDockDemoData.orders(context: context)
|
|
devicesByContext[context] = DroneDockDemoData.devices()
|
|
}
|
|
self.context = context
|
|
}
|
|
|
|
#if DEBUG
|
|
/// 只在 Debug 提供场景切换;不会触发扣次、真实设备或网络操作。
|
|
func applyScenario(_ scenario: DroneDockDemoScenario) {
|
|
var orders = DroneDockDemoData.orders(context: context)
|
|
var devices = DroneDockDemoData.devices()
|
|
switch scenario {
|
|
case .standard: break
|
|
case .singleDevice: devices = Array(devices.prefix(1))
|
|
case .unknownTelemetry:
|
|
devices = [DroneDockDevice(id: "demo-unknown", name: "01号无人机", state: .unknown, telemetry: .unknown)]
|
|
case .noDevices: devices = []
|
|
case .emptyOrders: orders = []
|
|
case .completedReturning:
|
|
let index = 4
|
|
orders[index] = DroneDockDemoData.withTask(
|
|
orders[index], phase: .returning, shootingResult: .succeeded
|
|
)
|
|
default:
|
|
if let phase = scenario.taskPhase {
|
|
orders[2] = DroneDockDemoData.withTask(
|
|
orders[2], phase: phase,
|
|
shootingResult: phase == .returning || phase == .returnPaused ? .succeeded : .inProgress
|
|
)
|
|
}
|
|
}
|
|
ordersByContext[context] = orders
|
|
devicesByContext[context] = devices
|
|
}
|
|
#endif
|
|
}
|
|
|
|
/// 工作台的展示规则:仅余次订单、当前任务置顶,多设备只作展示。
|
|
final class DroneDockWorkbenchViewModel {
|
|
/// 页面共享数据源,进入订单列表时传递同一实例。
|
|
let store: DroneDockDemoStore
|
|
/// 当前用户与景区。
|
|
var context: DroneDockContext { store.context }
|
|
/// 首页可见订单,不包含零次但尚在返航的订单。
|
|
private(set) var orders: [DroneShootingOrder] = []
|
|
/// 当前景区的设备列表。
|
|
private(set) var devices: [DroneDockDevice] = []
|
|
/// 今日统计摘要。
|
|
private(set) var statistics = DroneDockDailyStatistics(orderCount: 0, revenueText: "¥0", successfulShootingCount: 0)
|
|
/// 当前用户全部订单数量,含已完成。
|
|
var allOrderCount: Int { store.orders.count }
|
|
/// 首页实际可拍摄订单数量。
|
|
var orderCount: Int { orders.count }
|
|
|
|
/// 读取本地初始数据。
|
|
init(store: DroneDockDemoStore = DroneDockDemoStore()) {
|
|
self.store = store
|
|
refresh()
|
|
}
|
|
|
|
/// 在出现、返回或切换展示场景后主动刷新,不创建或取消任务。
|
|
func refresh() {
|
|
orders = DroneShootingOrderSorter.activeFirst(store.orders.filter { $0.remainingShootingCount > 0 })
|
|
devices = store.devices
|
|
statistics = store.statistics
|
|
}
|
|
|
|
/// 返回当前范围中的订单,避免使用过期卡片跨景区跳转。
|
|
func order(id: String) -> DroneShootingOrder? {
|
|
orders.first { $0.id == id }
|
|
}
|
|
|
|
/// 切换景区并刷新展示;原景区数据保持不变。
|
|
func switchContext(to context: DroneDockContext) {
|
|
store.switchContext(to: context)
|
|
refresh()
|
|
}
|
|
|
|
#if DEBUG
|
|
/// 应用仅本地有效的验收场景。
|
|
func applyScenario(_ scenario: DroneDockDemoScenario) {
|
|
store.applyScenario(scenario)
|
|
refresh()
|
|
}
|
|
#endif
|
|
}
|
|
|
|
/// 我的拍摄订单展示逻辑;筛选按履约状态,不受上传、交付或返航干扰。
|
|
final class DroneShootingOrdersViewModel {
|
|
/// 与工作台共享的数据源。
|
|
let store: DroneDockDemoStore
|
|
/// 当前范围。
|
|
var context: DroneDockContext { store.context }
|
|
/// 当前选中的 Tab;支付成功入口可显式传入 pending。
|
|
private(set) var selectedFilter: DroneShootingOrderFilter
|
|
/// 当前 Tab 下的订单。
|
|
private(set) var orders: [DroneShootingOrder] = []
|
|
/// 当前列表的标题。
|
|
var listTitle: String { selectedFilter.listTitle }
|
|
|
|
/// 初始化明确的入口 Tab,普通查看全部默认 all。
|
|
init(store: DroneDockDemoStore = DroneDockDemoStore(), initialFilter: DroneShootingOrderFilter = .all) {
|
|
self.store = store
|
|
selectedFilter = initialFilter
|
|
refresh()
|
|
}
|
|
|
|
/// 进入页面时主动读取数据,不改变任务和计数。
|
|
func refresh() {
|
|
orders = DroneShootingOrderSorter.activeFirst(store.orders.filter(selectedFilter.includes))
|
|
}
|
|
|
|
/// 用户切换 Tab。
|
|
func selectFilter(_ filter: DroneShootingOrderFilter) {
|
|
selectedFilter = filter
|
|
refresh()
|
|
}
|
|
|
|
/// 返回指定 Tab 下当前用户的订单数量。
|
|
func count(for filter: DroneShootingOrderFilter) -> Int {
|
|
store.orders.filter(filter.includes).count
|
|
}
|
|
|
|
/// 获取当前显示订单,过滤失效卡片操作。
|
|
func order(id: String) -> DroneShootingOrder? {
|
|
orders.first { $0.id == id }
|
|
}
|
|
}
|
|
|
|
/// 稳定地将当前任务置顶,其余顺序延续数据源。
|
|
private enum DroneShootingOrderSorter {
|
|
static func activeFirst(_ orders: [DroneShootingOrder]) -> [DroneShootingOrder] {
|
|
orders.filter { $0.activeTask != nil } + orders.filter { $0.activeTask == nil }
|
|
}
|
|
}
|
|
|
|
/// 本地 UI 展示夹具,不使用远程图片或任何真实接口。
|
|
private enum DroneDockDemoData {
|
|
static func devices() -> [DroneDockDevice] {
|
|
[
|
|
device(1, .ready, 95, 19.3, 0, 36),
|
|
device(2, .occupied, 72, 20.1, 1.2, 32),
|
|
DroneDockDevice(id: "demo-device-3", name: "03号无人机", state: .disconnected, telemetry: .unknown),
|
|
device(4, .lowBattery, 18, 19.8, 0.6, 34),
|
|
device(5, .ready, 88, 20.3, 0.8, 35),
|
|
DroneDockDevice(
|
|
id: "demo-device-6", name: "06号无人机", state: .noSignal,
|
|
telemetry: DroneDockTelemetry(
|
|
batteryPercent: nil, temperatureCelsius: nil, windSpeed: nil,
|
|
rainDescription: nil, signalDescription: "无信号", satelliteCount: nil
|
|
)
|
|
)
|
|
]
|
|
}
|
|
|
|
private static func device(
|
|
_ number: Int, _ state: DroneDockDeviceState, _ battery: Int,
|
|
_ temperature: Double, _ wind: Double, _ satellites: Int
|
|
) -> DroneDockDevice {
|
|
DroneDockDevice(
|
|
id: "demo-device-\(number)", name: String(format: "%02d号无人机", number), state: state,
|
|
telemetry: DroneDockTelemetry(
|
|
batteryPercent: battery, temperatureCelsius: temperature, windSpeed: wind,
|
|
rainDescription: "无雨", signalDescription: "良好", satelliteCount: satellites
|
|
)
|
|
)
|
|
}
|
|
|
|
static func orders(context: DroneDockContext) -> [DroneShootingOrder] {
|
|
[
|
|
order(context, 1, "158****8888", 5, 3, .uploading),
|
|
order(context, 2, "136****2806", 1, 0, .none),
|
|
order(context, 3, "139****5192", 5, 2, .failed),
|
|
order(context, 4, "137****6628", 5, 5, .ready),
|
|
order(context, 5, "180****4075", 1, 1, .uploading),
|
|
order(context, 6, "185****5736", 5, 5, .ready, delivered: 2, confirmation: .awaitingConfirmation),
|
|
order(context, 7, "188****3160", 5, 5, .ready, delivered: 3, confirmation: .confirmed),
|
|
order(context, 8, "135****2026", 3, 3, .partiallyReady)
|
|
]
|
|
}
|
|
|
|
private static func order(
|
|
_ context: DroneDockContext, _ number: Int, _ phone: String,
|
|
_ total: Int, _ successful: Int, _ material: DroneShootingMaterialState,
|
|
delivered: Int = 0, confirmation: DroneVisitorConfirmationState = .none
|
|
) -> DroneShootingOrder {
|
|
DroneShootingOrder(
|
|
id: "\(context.scenicID)-\(context.userID)-order-\(number)",
|
|
scenicID: context.scenicID, assignedUserID: context.userID,
|
|
customerName: "游客\(phone)", orderNumber: String(format: "HT2026102000%02d", number),
|
|
createdAtText: "10-20 \(14 - number):18", packageName: total == 1 ? "单次航拍" : "航拍套餐",
|
|
totalShootingCount: total, successfulShootingCount: successful, activeTask: nil,
|
|
needsRepreparation: false, materialState: material, deliveredVideoCount: delivered,
|
|
visitorConfirmationState: confirmation
|
|
)
|
|
}
|
|
|
|
static func withTask(
|
|
_ order: DroneShootingOrder, phase: DroneShootingTaskPhase, shootingResult: DroneShootingResult
|
|
) -> DroneShootingOrder {
|
|
DroneShootingOrder(
|
|
id: order.id, scenicID: order.scenicID, assignedUserID: order.assignedUserID,
|
|
customerName: order.customerName, orderNumber: order.orderNumber, createdAtText: order.createdAtText,
|
|
packageName: order.packageName, totalShootingCount: order.totalShootingCount,
|
|
successfulShootingCount: order.successfulShootingCount,
|
|
activeTask: DroneShootingTask(id: "\(order.id)-task", phase: phase, shootingResult: shootingResult),
|
|
needsRepreparation: order.needsRepreparation, materialState: order.materialState,
|
|
deliveredVideoCount: order.deliveredVideoCount, visitorConfirmationState: order.visitorConfirmationState
|
|
)
|
|
}
|
|
}
|
|
|
|
#if DEBUG
|
|
/// 仅供 Debug 页面验收的状态集合,不会编译进正式版入口。
|
|
enum DroneDockDemoScenario: String, CaseIterable {
|
|
case standard, singleDevice, unknownTelemetry, noDevices, emptyOrders
|
|
case navigating, arrived, shooting, shootingPaused, returning, returnPaused, unconfirmed, completedReturning
|
|
|
|
/// 状态选择菜单文案。
|
|
var title: String {
|
|
switch self {
|
|
case .standard: "默认数据 · 多架无人机"
|
|
case .singleDevice: "单架无人机"
|
|
case .unknownTelemetry: "遥测未知"
|
|
case .noDevices: "暂无无人机"
|
|
case .emptyOrders: "暂无订单"
|
|
case .completedReturning: "次数用完 · 仍在返航"
|
|
default: taskPhase?.title ?? "默认数据"
|
|
}
|
|
}
|
|
|
|
/// 该演示场景对应的未结束任务阶段。
|
|
var taskPhase: DroneShootingTaskPhase? {
|
|
DroneShootingTaskPhase(rawValue: rawValue)
|
|
}
|
|
}
|
|
#endif
|