从 iOS 17 Observation 迁移至 iOS 16 兼容的 Combine 架构
将最低部署版本降至 iOS 16,以 ObservableObject 替换 @Observable,新增导航与 UI 兼容层,并补充登录冒烟 UI 测试。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@ -6,7 +6,7 @@
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import Observation
|
||||
import Combine
|
||||
|
||||
@MainActor
|
||||
/// 数据统计服务协议,抽象汇总和每日明细读取能力以便测试替换。
|
||||
@ -26,10 +26,9 @@ protocol StatisticsServing {
|
||||
}
|
||||
|
||||
@MainActor
|
||||
@Observable
|
||||
/// 数据统计 API,封装摄影师和景区管理员两套统计接口。
|
||||
final class StatisticsAPI: StatisticsServing {
|
||||
@ObservationIgnored private let client: APIClient
|
||||
private let client: APIClient
|
||||
|
||||
/// 初始化数据统计 API,并注入共享网络客户端。
|
||||
init(client: APIClient) {
|
||||
|
||||
@ -6,21 +6,20 @@
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import Observation
|
||||
import Combine
|
||||
|
||||
@MainActor
|
||||
@Observable
|
||||
/// 数据统计页面 ViewModel,管理时间段、汇总数据、每日明细和分页状态。
|
||||
final class StatisticsViewModel {
|
||||
var selectedPeriod: StatisticsPeriod = .today
|
||||
private(set) var loading = false
|
||||
private(set) var loadingMore = false
|
||||
private(set) var summary = StatisticsSummaryResponse()
|
||||
private(set) var dailyItems: [StatisticsDailyItem] = []
|
||||
private(set) var totalDailyCount = 0
|
||||
private(set) var lastRefreshAt: Date?
|
||||
final class StatisticsViewModel: ObservableObject {
|
||||
@Published var selectedPeriod: StatisticsPeriod = .today
|
||||
@Published private(set) var loading = false
|
||||
@Published private(set) var loadingMore = false
|
||||
@Published private(set) var summary = StatisticsSummaryResponse()
|
||||
@Published private(set) var dailyItems: [StatisticsDailyItem] = []
|
||||
@Published private(set) var totalDailyCount = 0
|
||||
@Published private(set) var lastRefreshAt: Date?
|
||||
|
||||
private var page = 1
|
||||
@Published private var page = 1
|
||||
private let pageSize = 15
|
||||
|
||||
/// 当前日数据是否还有下一页。
|
||||
|
||||
@ -9,14 +9,14 @@ import SwiftUI
|
||||
|
||||
/// 数据 Tab 根视图,展示订单统计汇总和每日明细。
|
||||
struct StatisticsView: View {
|
||||
@Environment(AccountContext.self) private var accountContext
|
||||
@Environment(PermissionContext.self) private var permissionContext
|
||||
@Environment(StatisticsAPI.self) private var statisticsAPI
|
||||
@Environment(ToastCenter.self) private var toastCenter
|
||||
@EnvironmentObject private var accountContext: AccountContext
|
||||
@EnvironmentObject private var permissionContext: PermissionContext
|
||||
@Environment(\.statisticsAPI) private var statisticsAPI
|
||||
@EnvironmentObject private var toastCenter: ToastCenter
|
||||
@Environment(\.globalLoading) private var globalLoading
|
||||
@Environment(\.horizontalSizeClass) private var horizontalSizeClass
|
||||
|
||||
@State private var viewModel = StatisticsViewModel()
|
||||
@StateObject private var viewModel = StatisticsViewModel()
|
||||
|
||||
private var currentScenicId: Int? {
|
||||
accountContext.currentScenic?.id
|
||||
@ -33,7 +33,7 @@ struct StatisticsView: View {
|
||||
var body: some View {
|
||||
Group {
|
||||
if currentScenicId == nil {
|
||||
ContentUnavailableView(
|
||||
AppContentUnavailableView(
|
||||
"缺少经营上下文",
|
||||
systemImage: "chart.bar.doc.horizontal",
|
||||
description: Text("请先在首页选择景区后查看数据看板。")
|
||||
@ -59,10 +59,10 @@ struct StatisticsView: View {
|
||||
.navigationTitle("数据")
|
||||
.navigationBarTitleDisplayMode(.inline)
|
||||
.task { await reload() }
|
||||
.onChange(of: accountContext.currentScenic?.id) { _, _ in
|
||||
.onChange(of: accountContext.currentScenic?.id) { _ in
|
||||
Task { await reload() }
|
||||
}
|
||||
.onChange(of: permissionContext.currentRole?.id) { _, _ in
|
||||
.onChange(of: permissionContext.currentRole?.id) { _ in
|
||||
Task { await reload() }
|
||||
}
|
||||
}
|
||||
@ -171,7 +171,7 @@ struct StatisticsView: View {
|
||||
.frame(maxWidth: .infinity)
|
||||
.frame(minHeight: 280)
|
||||
} else if viewModel.dailyItems.isEmpty {
|
||||
ContentUnavailableView("暂无数据", systemImage: "tray", description: Text("可切换时间范围或下拉刷新。"))
|
||||
AppContentUnavailableView("暂无数据", systemImage: "tray", description: Text("可切换时间范围或下拉刷新。"))
|
||||
.frame(minHeight: 280)
|
||||
} else {
|
||||
VStack(spacing: 0) {
|
||||
@ -318,9 +318,9 @@ struct StatisticsView: View {
|
||||
#Preview {
|
||||
NavigationStack {
|
||||
StatisticsView()
|
||||
.environment(AccountContext())
|
||||
.environment(PermissionContext())
|
||||
.environment(StatisticsAPI(client: APIClient()))
|
||||
.environment(ToastCenter())
|
||||
.environmentObject(AccountContext())
|
||||
.environmentObject(PermissionContext())
|
||||
.environment(\.statisticsAPI, StatisticsAPI(client: APIClient()))
|
||||
.environmentObject(ToastCenter())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user