feat(drone-dock): 拆分首页设备订单与统计接口
This commit is contained in:
@@ -1,21 +1,23 @@
|
||||
import SnapKit
|
||||
import UIKit
|
||||
|
||||
/// 正式机巢首页,仅展示 airport/home 的真实快照,不进入本地演示业务。
|
||||
/// 正式机巢首页:朱工提供设备,张工提供订单与今日统计。
|
||||
final class DroneDockHomeViewController: BaseViewController {
|
||||
/// 与演示数据源隔离的首页状态。
|
||||
let viewModel: DroneDockHomeViewModel
|
||||
private let paymentControllerFactory: (() -> UIViewController)?
|
||||
private let preparationControllerFactory: ((DroneDockHomeOrder, DroneDockHomeContext) -> UIViewController)?
|
||||
private var refreshTask: Task<Void, Never>?
|
||||
private var loadMoreTask: Task<Void, Never>?
|
||||
private var isRefreshing = false
|
||||
private var isLoadingOrdersPage = false
|
||||
private var devicePage = 0
|
||||
private let pageControl = UIPageControl()
|
||||
|
||||
/// 首页按当前身份、设备、订单与统计展示服务端快照。
|
||||
private enum Section: Int, CaseIterable { case context, devices, orders, statistics }
|
||||
/// 稳定身份确保刷新使用 Diffable 更新。
|
||||
private enum Item: Hashable { case context, devices, device(Int), orders, order(Int), statistics }
|
||||
private enum Item: Hashable { case context, devices, device(Int), orders, order(String), moreOrders, statistics }
|
||||
private lazy var collectionView = UICollectionView(frame: .zero, collectionViewLayout: makeLayout())
|
||||
private var dataSource: UICollectionViewDiffableDataSource<Section, Item>!
|
||||
|
||||
@@ -41,7 +43,10 @@ final class DroneDockHomeViewController: BaseViewController {
|
||||
@available(*, unavailable)
|
||||
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
|
||||
|
||||
deinit { refreshTask?.cancel() }
|
||||
deinit {
|
||||
refreshTask?.cancel()
|
||||
loadMoreTask?.cancel()
|
||||
}
|
||||
|
||||
override func setupNavigationBar() {
|
||||
title = "机巢工作台"
|
||||
@@ -92,16 +97,20 @@ final class DroneDockHomeViewController: BaseViewController {
|
||||
override func viewWillDisappear(_ animated: Bool) {
|
||||
super.viewWillDisappear(animated)
|
||||
refreshTask?.cancel()
|
||||
loadMoreTask?.cancel()
|
||||
isRefreshing = false
|
||||
isLoadingOrdersPage = false
|
||||
viewModel.invalidate()
|
||||
}
|
||||
|
||||
private func refreshHome() {
|
||||
refreshTask?.cancel()
|
||||
loadMoreTask?.cancel()
|
||||
isLoadingOrdersPage = false
|
||||
isRefreshing = true
|
||||
refreshTask = Task { @MainActor [weak self] in
|
||||
guard let self else { return }
|
||||
let isInitialLoad = self.viewModel.response == nil
|
||||
let isInitialLoad = self.viewModel.response == nil && self.viewModel.ordersResponse == nil
|
||||
self.applySnapshot(animated: false)
|
||||
await self.viewModel.refresh()
|
||||
guard !Task.isCancelled else { return }
|
||||
@@ -131,7 +140,7 @@ final class DroneDockHomeViewController: BaseViewController {
|
||||
}
|
||||
if case .order(let id) = item {
|
||||
let cell = collection.dequeueReusableCell(withReuseIdentifier: "order", for: path) as! DroneDockCompactOrderCell
|
||||
if let order = self.viewModel.response?.orders.first(where: { $0.orderID == id }) {
|
||||
if let order = self.viewModel.orders.first(where: { $0.orderID == id }) {
|
||||
cell.apply(homeOrder: order)
|
||||
cell.onShootingTap = { [weak self] in
|
||||
self?.openPreparation(orderID: id)
|
||||
@@ -147,7 +156,12 @@ final class DroneDockHomeViewController: BaseViewController {
|
||||
let title = [context.scenicName, context.photographerName].filter { !$0.isEmpty }.joined(separator: " · ")
|
||||
cell.applyContext(title.isEmpty ? "机巢工作台" : title)
|
||||
case .statistics:
|
||||
cell.applyStatistics(self.viewModel.response?.today)
|
||||
cell.applyStatistics(self.viewModel.today,
|
||||
error: self.viewModel.statisticsErrorMessage ?? self.viewModel.errorMessage)
|
||||
case .moreOrders:
|
||||
cell.applyLoadMore(isLoading: self.isLoadingOrdersPage,
|
||||
error: self.viewModel.ordersLoadMoreErrorMessage,
|
||||
enabled: !self.isRefreshing) { [weak self] in self?.loadMoreOrders() }
|
||||
case .devices, .orders:
|
||||
self.applyDataState(to: cell, isDevice: item == .devices)
|
||||
case .device, .order: break
|
||||
@@ -163,27 +177,30 @@ final class DroneDockHomeViewController: BaseViewController {
|
||||
}
|
||||
let header = collection.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "header", for: path) as! DroneDockHomeHeader
|
||||
let isOrders = Section(rawValue: path.section) == .orders
|
||||
header.apply(title: isOrders ? "我的拍摄订单" : "今日统计", showsAction: isOrders)
|
||||
header.onViewAll = { [weak self] in self?.showUnavailable("拍摄订单") }
|
||||
let orderTitle = self.viewModel.orderTotal.map { "我的拍摄订单(\($0))" } ?? "我的拍摄订单"
|
||||
header.apply(title: isOrders ? orderTitle : "今日统计", isOrders: isOrders)
|
||||
return header
|
||||
}
|
||||
}
|
||||
|
||||
private func applyDataState(to cell: DroneDockHomeContentCell, isDevice: Bool) {
|
||||
if isRefreshing, viewModel.response == nil {
|
||||
let hasResponse = isDevice ? viewModel.response != nil : viewModel.ordersResponse != nil
|
||||
let error = (isDevice ? viewModel.deviceErrorMessage : viewModel.ordersErrorMessage) ?? viewModel.errorMessage
|
||||
if isRefreshing, !hasResponse {
|
||||
cell.applyMessage(symbol: "arrow.triangle.2.circlepath", title: "正在加载", detail: "正在获取最新数据")
|
||||
return
|
||||
}
|
||||
if let error = viewModel.errorMessage {
|
||||
cell.applyMessage(symbol: "wifi.exclamationmark", title: "首页加载失败", detail: error + ",请下拉重试")
|
||||
if let error {
|
||||
cell.applyMessage(symbol: "wifi.exclamationmark", title: isDevice ? "设备加载失败" : "订单加载失败",
|
||||
detail: error + ",请下拉重试")
|
||||
return
|
||||
}
|
||||
guard viewModel.response != nil else {
|
||||
guard hasResponse else {
|
||||
cell.applyMessage(symbol: "arrow.triangle.2.circlepath", title: "正在加载", detail: "正在获取最新数据")
|
||||
return
|
||||
}
|
||||
cell.applyMessage(symbol: isDevice ? "airplane" : "doc.text",
|
||||
title: isDevice ? "暂无无人机" : "暂无待拍摄订单",
|
||||
title: isDevice ? "暂无无人机" : "暂无拍摄订单",
|
||||
detail: isDevice ? "当前景区暂无设备数据" : "当前景区暂无拍摄订单数据")
|
||||
}
|
||||
|
||||
@@ -194,8 +211,11 @@ final class DroneDockHomeViewController: BaseViewController {
|
||||
snapshot.appendItems([.context], toSection: .context)
|
||||
let airports = viewModel.response?.airports ?? []
|
||||
snapshot.appendItems(airports.isEmpty ? [.devices] : airports.map { .device($0.id) }, toSection: .devices)
|
||||
let orders = viewModel.response?.orders ?? []
|
||||
let orders = viewModel.orders
|
||||
snapshot.appendItems(orders.isEmpty ? [.orders] : orders.map { .order($0.orderID) }, toSection: .orders)
|
||||
if viewModel.hasMoreOrders || viewModel.isLoadingMore || viewModel.ordersLoadMoreErrorMessage != nil {
|
||||
snapshot.appendItems([.moreOrders], toSection: .orders)
|
||||
}
|
||||
snapshot.appendItems([.statistics], toSection: .statistics)
|
||||
let old = Set(dataSource.snapshot().itemIdentifiers)
|
||||
let previousDeviceCount = old.filter { if case .device = $0 { return true }; return false }.count
|
||||
@@ -206,7 +226,18 @@ final class DroneDockHomeViewController: BaseViewController {
|
||||
if previousDeviceCount != airports.count {
|
||||
collectionView.setCollectionViewLayout(makeLayout(), animated: false)
|
||||
}
|
||||
dataSource.apply(snapshot, animatingDifferences: animated, completion: completion)
|
||||
dataSource.apply(snapshot, animatingDifferences: animated) { [weak self] in
|
||||
if let self {
|
||||
for path in self.collectionView.indexPathsForVisibleSupplementaryElements(ofKind: UICollectionView.elementKindSectionHeader) {
|
||||
guard let header = self.collectionView.supplementaryView(
|
||||
forElementKind: UICollectionView.elementKindSectionHeader, at: path) as? DroneDockHomeHeader else { continue }
|
||||
let isOrders = Section(rawValue: path.section) == .orders
|
||||
let title = self.viewModel.orderTotal.map { "我的拍摄订单(\($0))" } ?? "我的拍摄订单"
|
||||
header.apply(title: isOrders ? title : "今日统计", isOrders: isOrders)
|
||||
}
|
||||
}
|
||||
completion?()
|
||||
}
|
||||
}
|
||||
|
||||
private func makeLayout() -> UICollectionViewLayout {
|
||||
@@ -249,8 +280,8 @@ final class DroneDockHomeViewController: BaseViewController {
|
||||
showAlert(title: feature, message: "该功能暂未开放")
|
||||
}
|
||||
|
||||
private func openPreparation(orderID: Int) {
|
||||
guard !isRefreshing, let order = viewModel.response?.orders.first(where: { $0.orderID == orderID }) else { return }
|
||||
private func openPreparation(orderID: String) {
|
||||
guard !isRefreshing, let order = viewModel.orders.first(where: { $0.orderID == orderID }) else { return }
|
||||
guard order.primaryAction == "start_shoot" else {
|
||||
showUnavailable(order.actionTitle ?? "拍摄订单")
|
||||
return
|
||||
@@ -276,6 +307,21 @@ final class DroneDockHomeViewController: BaseViewController {
|
||||
}
|
||||
@objc private func refreshTriggered() { refreshHome() }
|
||||
|
||||
/// 按后端分页继续读取订单,失败保留已有列表并允许重试。
|
||||
private func loadMoreOrders() {
|
||||
guard !isRefreshing, !isLoadingOrdersPage, !viewModel.isLoadingMore else { return }
|
||||
loadMoreTask?.cancel()
|
||||
isLoadingOrdersPage = true
|
||||
applySnapshot(animated: false)
|
||||
loadMoreTask = Task { @MainActor [weak self] in
|
||||
guard let self else { return }
|
||||
await self.viewModel.loadMoreOrders()
|
||||
guard !Task.isCancelled else { return }
|
||||
self.isLoadingOrdersPage = false
|
||||
self.applySnapshot(animated: true)
|
||||
}
|
||||
}
|
||||
|
||||
#if DEBUG
|
||||
/// 显式联调参数下只保存本应用窗口,不截取用户切换后的其他 App。
|
||||
private func captureHomeIfRequested() async {
|
||||
@@ -348,8 +394,8 @@ private final class DroneDockHomeContentCell: UICollectionViewCell {
|
||||
install(stack, insets: UIEdgeInsets(top: 22, left: 16, bottom: 22, right: 16), card: true)
|
||||
}
|
||||
|
||||
/// 使用服务端 today 字段;缺失数据保持未知,不显示为零。
|
||||
func applyStatistics(_ today: DroneDockHomeToday?) {
|
||||
/// 展示张工 today-stats 的成交与确认完成统计,失败保持未知并提示重试。
|
||||
func applyStatistics(_ today: DroneDockHomeToday?, error: String?) {
|
||||
let formatter = NumberFormatter()
|
||||
formatter.locale = Locale(identifier: "zh_CN")
|
||||
formatter.numberStyle = .decimal
|
||||
@@ -368,11 +414,40 @@ private final class DroneDockHomeContentCell: UICollectionViewCell {
|
||||
stack.spacing = 9
|
||||
return stack
|
||||
}
|
||||
let stack = UIStackView(arrangedSubviews: columns)
|
||||
stack.distribution = .fillEqually
|
||||
let row = UIStackView(arrangedSubviews: columns)
|
||||
row.distribution = .fillEqually
|
||||
let stack = UIStackView(arrangedSubviews: [row])
|
||||
stack.axis = .vertical
|
||||
stack.spacing = 12
|
||||
if let error {
|
||||
let message = DroneDockStyle.label("统计加载失败,请下拉重试\n\(error)", size: 12, color: DroneDockStyle.secondary)
|
||||
message.numberOfLines = 0
|
||||
message.textAlignment = .center
|
||||
stack.addArrangedSubview(message)
|
||||
}
|
||||
install(stack, insets: UIEdgeInsets(top: 23, left: 10, bottom: 23, right: 10), card: true)
|
||||
}
|
||||
|
||||
/// 订单分页入口;提交期间禁用,查询失败不清空之前已加载的订单。
|
||||
func applyLoadMore(isLoading: Bool, error: String?, enabled: Bool, action: @escaping () -> Void) {
|
||||
let button = UIButton(type: .system)
|
||||
button.setTitle(isLoading ? "加载中…" : error == nil ? "加载更多订单" : "重试加载更多", for: .normal)
|
||||
button.tintColor = DroneDockStyle.primary
|
||||
button.isEnabled = enabled && !isLoading
|
||||
button.accessibilityIdentifier = "drone.home.orders.loadMore"
|
||||
button.snp.makeConstraints { $0.height.greaterThanOrEqualTo(44) }
|
||||
button.addAction(UIAction { _ in action() }, for: .touchUpInside)
|
||||
let stack = UIStackView(arrangedSubviews: [button])
|
||||
stack.axis = .vertical
|
||||
if let error {
|
||||
let label = DroneDockStyle.label(error, size: 12, color: DroneDockStyle.secondary)
|
||||
label.numberOfLines = 0
|
||||
label.textAlignment = .center
|
||||
stack.addArrangedSubview(label)
|
||||
}
|
||||
install(stack, insets: UIEdgeInsets(top: 4, left: 16, bottom: 8, right: 16), card: false)
|
||||
}
|
||||
|
||||
private func install(_ view: UIView, insets: UIEdgeInsets, card: Bool) {
|
||||
embeddedView?.removeFromSuperview()
|
||||
embeddedView = view
|
||||
@@ -382,35 +457,22 @@ private final class DroneDockHomeContentCell: UICollectionViewCell {
|
||||
}
|
||||
}
|
||||
|
||||
/// 首页分区标题与后续订单列表入口。
|
||||
/// 首页分区标题,订单数量使用接口 total,不使用当前页条数。
|
||||
private final class DroneDockHomeHeader: UICollectionReusableView {
|
||||
/// 用户请求查看完整订单列表。
|
||||
var onViewAll: (() -> Void)?
|
||||
private let label = DroneDockStyle.label(size: 17, weight: .semibold)
|
||||
private let button = UIButton(type: .system)
|
||||
|
||||
override init(frame: CGRect) {
|
||||
super.init(frame: frame)
|
||||
button.setTitle("查看全部 ›", for: .normal)
|
||||
button.titleLabel?.font = .systemFont(ofSize: 13)
|
||||
button.tintColor = DroneDockStyle.primary
|
||||
button.accessibilityIdentifier = "drone.home.allOrders"
|
||||
button.addTarget(self, action: #selector(tapped), for: .touchUpInside)
|
||||
button.snp.makeConstraints { $0.height.equalTo(44) }
|
||||
let stack = UIStackView(arrangedSubviews: [label, UIView(), button])
|
||||
stack.alignment = .center
|
||||
addSubview(stack)
|
||||
stack.snp.makeConstraints { $0.leading.trailing.centerY.equalToSuperview() }
|
||||
addSubview(label)
|
||||
label.snp.makeConstraints { $0.leading.trailing.centerY.equalToSuperview() }
|
||||
}
|
||||
|
||||
@available(*, unavailable)
|
||||
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
|
||||
|
||||
/// 刷新区块标题和入口可见性。
|
||||
func apply(title: String, showsAction: Bool) {
|
||||
/// 区分订单总数与统计标题,便于读取真实总数。
|
||||
func apply(title: String, isOrders: Bool) {
|
||||
label.text = title
|
||||
button.isHidden = !showsAction
|
||||
label.accessibilityIdentifier = isOrders ? "drone.home.orderCount" : nil
|
||||
}
|
||||
|
||||
@objc private func tapped() { onViewAll?() }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user