feat: 完善机巢模拟调试与素材交付交互
增加禁飞期间的设备指令拦截及本地模拟拍摄流程;适配订单素材库和景区空境库,完善素材筛选、预览与下载;订单交付入口改为方式选择弹窗并复用云端上传。补充单元测试、接口记录和模拟器截图。
This commit is contained in:
@@ -2,14 +2,16 @@ import Foundation
|
||||
|
||||
/// 独立景区空镜库的筛选、选择和前台下载演示逻辑;不依赖 UIKit 或机巢业务。
|
||||
final class ScenicMaterialsViewModel {
|
||||
/// 正式模式使用网络素材与真实相册保存,预览模式使用本地演示。
|
||||
let isRemote: Bool
|
||||
/// 本次页面的独立账号与景区范围。
|
||||
let context: ScenicMaterialContext
|
||||
/// 用于筛选的日历及其时区。
|
||||
let calendar: Calendar
|
||||
/// 本地演示保留天数,正式对接后由服务端配置决定。
|
||||
let retentionDays = 90
|
||||
private(set) var retentionDays = 90
|
||||
/// 保留期首日零点,包含今天一共 90 个日历日。
|
||||
let minimumDate: Date
|
||||
private(set) var minimumDate: Date
|
||||
/// 可选的最后一天零点,不允许选择未来日期。
|
||||
let maximumDate: Date
|
||||
/// 当前景区的独立空镜航线,含暂无片段的航线。
|
||||
@@ -37,18 +39,29 @@ final class ScenicMaterialsViewModel {
|
||||
/// 当前批次的逐项演示进度。
|
||||
private(set) var progressByID: [String: Double] = [:]
|
||||
|
||||
/// 已确认接口的加载状态。
|
||||
private(set) var isLoading = false
|
||||
/// 请求失败或素材字段尚无法映射时的可重试提示。
|
||||
private(set) var loadError: String?
|
||||
/// 保留完整响应,非空且未确认字段时不能被当成空库。
|
||||
private(set) var remoteSessions: ScenicMaterialsSessionsResponse?
|
||||
/// 素材保留说明优先使用后端返回的文案。
|
||||
private(set) var retentionTips = "素材保留 90 天,请及时下载"
|
||||
private var loadID: UUID?
|
||||
|
||||
private let initialGroups: [ScenicMaterialGroup]
|
||||
|
||||
/// 创建演示库;可注入日历、日期和数据,测试不依赖时间、网络或登录单例。
|
||||
init(context: ScenicMaterialContext = .demo, now: Date = Date(), calendar: Calendar = .current,
|
||||
routes: [ScenicMaterialRoute]? = nil, groups: [ScenicMaterialGroup]? = nil) {
|
||||
routes: [ScenicMaterialRoute]? = nil, groups: [ScenicMaterialGroup]? = nil, isRemote: Bool = false) {
|
||||
self.isRemote = isRemote
|
||||
self.context = context
|
||||
self.calendar = calendar
|
||||
maximumDate = calendar.startOfDay(for: now)
|
||||
minimumDate = calendar.date(byAdding: .day, value: -(retentionDays - 1), to: maximumDate) ?? maximumDate
|
||||
let demoRoutes = Self.makeRoutes(context: context)
|
||||
let demoRoutes = isRemote ? [] : Self.makeRoutes(context: context)
|
||||
self.routes = routes ?? demoRoutes
|
||||
let data = groups ?? Self.makeGroups(context: context, routes: demoRoutes, today: maximumDate, calendar: calendar)
|
||||
let data = groups ?? (isRemote ? [] : Self.makeGroups(context: context, routes: demoRoutes, today: maximumDate, calendar: calendar))
|
||||
initialGroups = data
|
||||
allGroups = data
|
||||
}
|
||||
@@ -113,10 +126,55 @@ final class ScenicMaterialsViewModel {
|
||||
}
|
||||
}
|
||||
/// 当前空态标题,区分全库为空与有筛选但无结果。
|
||||
var emptyTitle: String { hasAnyMaterials ? "没有符合条件的素材" : "暂无景区素材" }
|
||||
var emptyTitle: String {
|
||||
if isLoading { return "正在加载素材" }
|
||||
if loadError != nil { return "素材加载失败" }
|
||||
return hasAnyMaterials || (isRemote && (selectedRouteID != nil || dateRange != nil)) ? "没有符合条件的素材" : "暂无景区素材"
|
||||
}
|
||||
/// 当前空态说明。
|
||||
var emptyMessage: String {
|
||||
hasAnyMaterials ? "试试其他航线或时间范围" : "景区空镜上传后,将显示在这里"
|
||||
if isLoading { return "请稍候" }
|
||||
if let loadError { return loadError }
|
||||
return hasAnyMaterials || (isRemote && (selectedRouteID != nil || dateRange != nil)) ? "试试其他航线或时间范围" : "景区空镜上传后,将显示在这里"
|
||||
}
|
||||
|
||||
/// 读取当前景区的真实列表;在有样本确认子项合同前保留原始响应并明确报错。
|
||||
func loadRemoteSessions(api: any ScenicMaterialsServing) async {
|
||||
guard isRemote, !isSelectionLocked else { return }
|
||||
let requestID = UUID()
|
||||
loadID = requestID
|
||||
defer { if loadID == requestID { isLoading = false } }
|
||||
isLoading = true
|
||||
loadError = nil
|
||||
let query = ScenicMaterialsQuery(scenicID: Int(context.scenicID) ?? 0,
|
||||
airLineID: selectedRouteID.flatMap(Int.init),
|
||||
dateFrom: dateRange.map { formatDate($0.start, format: "yyyy-MM-dd") },
|
||||
dateTo: dateRange.map { formatDate($0.end, format: "yyyy-MM-dd") }, retentionDays: retentionDays)
|
||||
do {
|
||||
let response = try await api.sessions(query: query)
|
||||
guard loadID == requestID, !Task.isCancelled else { return }
|
||||
remoteSessions = response
|
||||
retentionDays = response.retentionDays
|
||||
retentionTips = response.retentionTips
|
||||
minimumDate = calendar.date(byAdding: .day, value: -(retentionDays - 1), to: maximumDate) ?? maximumDate
|
||||
allGroups = []
|
||||
routes = []
|
||||
selectedIDs.removeAll()
|
||||
if response.total > 0 || !response.list.isEmpty || !response.groups.isEmpty || !response.airLines.isEmpty {
|
||||
// 当前文档没有非空场次/航线的字段合同,不使用猜测的 ID 下载或错配素材。
|
||||
loadError = "暂时无法显示素材,请稍后重试"
|
||||
}
|
||||
} catch is CancellationError {
|
||||
} catch {
|
||||
guard loadID == requestID else { return }
|
||||
loadError = error.localizedDescription
|
||||
}
|
||||
}
|
||||
|
||||
/// 离页或身份变化后作废尚未返回的列表请求。
|
||||
func cancelRemoteLoad() {
|
||||
loadID = nil
|
||||
isLoading = false
|
||||
}
|
||||
|
||||
/// 查找稳定航线,不依赖名称。
|
||||
@@ -171,7 +229,7 @@ final class ScenicMaterialsViewModel {
|
||||
|
||||
/// 当前可选状态;完成、不可用、隐藏片段或锁定批次均不可新选。
|
||||
func canSelect(id: String) -> Bool {
|
||||
!isSelectionLocked && filteredClips.contains { $0.id == id && $0.isAvailable && !completedIDs.contains(id) }
|
||||
!isSelectionLocked && filteredClips.contains { $0.id == id && $0.isAvailable && (!isRemote || $0.remoteURL != nil) && !completedIDs.contains(id) }
|
||||
}
|
||||
|
||||
/// 切换单项选择,不涉及预览或下载。
|
||||
@@ -215,7 +273,7 @@ final class ScenicMaterialsViewModel {
|
||||
@discardableResult
|
||||
func startDownload() -> Bool {
|
||||
guard !isSelectionLocked else { return false }
|
||||
let items = filteredClips.filter { selectedIDs.contains($0.id) && $0.isAvailable && !completedIDs.contains($0.id) }
|
||||
let items = filteredClips.filter { selectedIDs.contains($0.id) && $0.isAvailable && (!isRemote || $0.remoteURL != nil) && !completedIDs.contains($0.id) }
|
||||
guard !items.isEmpty else { return false }
|
||||
batchItemIDs = items.map(\.id)
|
||||
selectedIDs = Set(batchItemIDs)
|
||||
@@ -285,6 +343,7 @@ final class ScenicMaterialsViewModel {
|
||||
|
||||
/// 切换本地状态预览,不读取真实相册,也不更改登录身份或订单。
|
||||
func applyScenario(_ scenario: ScenicMaterialsScenario) {
|
||||
guard !isRemote else { return }
|
||||
clearBatch()
|
||||
completedIDs.removeAll()
|
||||
allGroups = initialGroups
|
||||
@@ -320,11 +379,11 @@ final class ScenicMaterialsViewModel {
|
||||
|
||||
private func selectableIDs(in groupID: String) -> Set<String> {
|
||||
guard let group = filteredGroups.first(where: { $0.id == groupID }) else { return [] }
|
||||
return Set(group.clips.filter { $0.isAvailable && !completedIDs.contains($0.id) }.map(\.id))
|
||||
return Set(group.clips.filter { $0.isAvailable && (!isRemote || $0.remoteURL != nil) && !completedIDs.contains($0.id) }.map(\.id))
|
||||
}
|
||||
|
||||
private func pruneSelection() {
|
||||
let valid = Set(filteredClips.filter { $0.isAvailable && !completedIDs.contains($0.id) }.map(\.id))
|
||||
let valid = Set(filteredClips.filter { $0.isAvailable && (!isRemote || $0.remoteURL != nil) && !completedIDs.contains($0.id) }.map(\.id))
|
||||
selectedIDs.formIntersection(valid)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user