feat(drone-dock): 接入首页真实数据并完善设备卡片
通过 airport/home 读取当前景区和门店的设备与统计,隔离失效会话回包,未知遥测显示破折号。完善状态圆点、分割线和卫星图标;订单详情等待后端测试数据。 验证:iPhone 11 上 49 项相关测试、样式调整后 9 项界面回归,以及测试样例匿名化后 20 项测试均通过。真实联调响应、日志和截图仅保留在本地。
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
import Foundation
|
||||
|
||||
/// 一次真实首页请求的账号、景区和门店范围;凭证只参与隔离,不对外提供展示。
|
||||
struct DroneDockHomeContext: Equatable, Sendable {
|
||||
/// 当前登录工作人员的稳定 ID。
|
||||
let userID: String
|
||||
/// 当前账号类型,用于区分相同 ID 的不同账号域。
|
||||
let accountType: String
|
||||
/// 当前景区 ID。
|
||||
let scenicID: Int
|
||||
/// 当前景区名称。
|
||||
let scenicName: String
|
||||
/// 当前景区下的门店 ID。
|
||||
let storeID: Int
|
||||
/// 当前工作人员展示名称。
|
||||
let photographerName: String
|
||||
private let token: String
|
||||
|
||||
/// 冻结请求范围及登录凭证,仅用于与最新会话比较。
|
||||
init(userID: String, accountType: String, scenicID: Int, scenicName: String,
|
||||
storeID: Int, photographerName: String, token: String) {
|
||||
self.userID = userID
|
||||
self.accountType = accountType
|
||||
self.scenicID = scenicID
|
||||
self.scenicName = scenicName
|
||||
self.storeID = storeID
|
||||
self.photographerName = photographerName
|
||||
self.token = token.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
}
|
||||
|
||||
/// 是否具备查询当前首页所需的登录及景区、门店上下文。
|
||||
var canLoad: Bool { !userID.isEmpty && !token.isEmpty && scenicID > 0 && storeID > 0 }
|
||||
|
||||
/// 判断响应是否仍属于当前会话,包含登录 Token 更新但忽略展示名称变化。
|
||||
func hasSameIdentity(as other: DroneDockHomeContext) -> Bool {
|
||||
userID == other.userID && accountType == other.accountType && scenicID == other.scenicID
|
||||
&& storeID == other.storeID && token == other.token
|
||||
}
|
||||
}
|
||||
|
||||
/// 真实机巢首页的独立读取状态;不使用演示订单、演示设备或本地拍摄任务。
|
||||
final class DroneDockHomeViewModel {
|
||||
/// 当前页面所属的真实账号、景区和门店。
|
||||
private(set) var context: DroneDockHomeContext
|
||||
/// 最近一次成功读取的当前范围首页;加载失败或身份变更时清除。
|
||||
private(set) var response: DroneDockHomeResponse?
|
||||
/// 当前最新一次请求是否尚未结束。
|
||||
private(set) var isLoading = false
|
||||
/// 当前请求的可展示错误;与成功空列表分开表达。
|
||||
private(set) var errorMessage: String?
|
||||
|
||||
private let service: any DroneDockHomeServing
|
||||
private let currentContext: @MainActor @Sendable () -> DroneDockHomeContext
|
||||
private var generation = 0
|
||||
|
||||
/// 注入读取服务与最新会话提供器,调用方在刷新完成后主动更新界面。
|
||||
init(context: DroneDockHomeContext, service: any DroneDockHomeServing,
|
||||
currentContext: @escaping @MainActor @Sendable () -> DroneDockHomeContext) {
|
||||
self.context = context
|
||||
self.service = service
|
||||
self.currentContext = currentContext
|
||||
}
|
||||
|
||||
/// 刷新真实首页;拒绝旧请求、旧登录凭证、旧景区及旧门店的迟到结果。
|
||||
func refresh() async {
|
||||
generation &+= 1
|
||||
let requestGeneration = generation
|
||||
isLoading = true
|
||||
errorMessage = nil
|
||||
defer { if generation == requestGeneration { isLoading = false } }
|
||||
let requestedContext = await currentContext()
|
||||
guard generation == requestGeneration, !Task.isCancelled else { return }
|
||||
if !context.hasSameIdentity(as: requestedContext) { response = nil }
|
||||
context = requestedContext
|
||||
guard requestedContext.canLoad else {
|
||||
response = nil
|
||||
isLoading = false
|
||||
errorMessage = "请先登录并选择景区和门店"
|
||||
return
|
||||
}
|
||||
do {
|
||||
let result = try await service.home(scenicID: requestedContext.scenicID, storeID: requestedContext.storeID)
|
||||
guard await accepts(requestGeneration, context: requestedContext) else { return }
|
||||
guard result.scenicID == requestedContext.scenicID else {
|
||||
response = nil
|
||||
errorMessage = "接口返回的景区与当前景区不一致,请刷新重试"
|
||||
return
|
||||
}
|
||||
response = result
|
||||
} catch {
|
||||
guard await accepts(requestGeneration, context: requestedContext) else { return }
|
||||
response = nil
|
||||
if error is CancellationError { return }
|
||||
errorMessage = error.localizedDescription
|
||||
}
|
||||
}
|
||||
|
||||
/// 离开或取消当前读取时使所有在途结果失效;再次刷新仍可重新加载。
|
||||
func invalidate() {
|
||||
generation &+= 1
|
||||
response = nil
|
||||
errorMessage = nil
|
||||
isLoading = false
|
||||
}
|
||||
|
||||
private func accepts(_ requestGeneration: Int, context requested: DroneDockHomeContext) async -> Bool {
|
||||
guard generation == requestGeneration, !Task.isCancelled else { return false }
|
||||
let latest = await currentContext()
|
||||
guard generation == requestGeneration, !Task.isCancelled else { return false }
|
||||
guard requested.hasSameIdentity(as: latest) else {
|
||||
generation &+= 1
|
||||
context = latest
|
||||
response = nil
|
||||
isLoading = false
|
||||
errorMessage = "账号或景区已变化,请刷新工作台"
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user