从 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
|
||||
@ -29,9 +29,8 @@ protocol ScheduleServing {
|
||||
|
||||
/// 排班 API,封装摄影师排班相关接口。
|
||||
@MainActor
|
||||
@Observable
|
||||
final class ScheduleAPI: ScheduleServing {
|
||||
@ObservationIgnored private let client: APIClient
|
||||
private let client: APIClient
|
||||
|
||||
/// 初始化排班 API,并注入共享网络客户端。
|
||||
init(client: APIClient) {
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import Observation
|
||||
import Combine
|
||||
|
||||
/// 排班错误实体,表示表单校验失败。
|
||||
enum ScheduleValidationError: LocalizedError, Equatable {
|
||||
@ -26,14 +26,13 @@ enum ScheduleValidationError: LocalizedError, Equatable {
|
||||
|
||||
/// 排班管理 ViewModel,负责月份日期标记、某日列表和删除操作。
|
||||
@MainActor
|
||||
@Observable
|
||||
final class ScheduleManagementViewModel {
|
||||
var monthDate = Date.scheduleFirstDayOfMonth
|
||||
var selectedDate = Date()
|
||||
private(set) var markedDays: Set<String> = []
|
||||
private(set) var items: [ScheduleItem] = []
|
||||
private(set) var loading = false
|
||||
var errorMessage: String?
|
||||
final class ScheduleManagementViewModel: ObservableObject {
|
||||
@Published var monthDate = Date.scheduleFirstDayOfMonth
|
||||
@Published var selectedDate = Date()
|
||||
@Published private(set) var markedDays: Set<String> = []
|
||||
@Published private(set) var items: [ScheduleItem] = []
|
||||
@Published private(set) var loading = false
|
||||
@Published var errorMessage: String?
|
||||
|
||||
/// 切换到上一个月并重新加载。
|
||||
func previousMonth(api: any ScheduleServing, scenicId: Int?) async {
|
||||
@ -109,13 +108,12 @@ final class ScheduleManagementViewModel {
|
||||
|
||||
/// 新增排班 ViewModel,负责可选订单加载、表单校验和提交。
|
||||
@MainActor
|
||||
@Observable
|
||||
final class ScheduleAddViewModel {
|
||||
var draft = ScheduleDraft()
|
||||
private(set) var availableOrders: [AvailableOrderResponse] = []
|
||||
private(set) var loadingOrders = false
|
||||
private(set) var submitting = false
|
||||
var errorMessage: String?
|
||||
final class ScheduleAddViewModel: ObservableObject {
|
||||
@Published var draft = ScheduleDraft()
|
||||
@Published private(set) var availableOrders: [AvailableOrderResponse] = []
|
||||
@Published private(set) var loadingOrders = false
|
||||
@Published private(set) var submitting = false
|
||||
@Published var errorMessage: String?
|
||||
|
||||
/// 加载可关联订单列表。
|
||||
func loadOrders(api: any ScheduleServing, scenicId: Int?) async {
|
||||
|
||||
@ -9,15 +9,15 @@ import SwiftUI
|
||||
|
||||
/// 排班管理页面,展示月视图日期标记和当天排班列表。
|
||||
struct ScheduleManagementView: View {
|
||||
@Environment(AccountContext.self) private var accountContext
|
||||
@Environment(ScheduleAPI.self) private var scheduleAPI
|
||||
@EnvironmentObject private var accountContext: AccountContext
|
||||
@Environment(\.scheduleAPI) private var scheduleAPI
|
||||
@Environment(\.globalLoading) private var globalLoading
|
||||
@State private var viewModel = ScheduleManagementViewModel()
|
||||
@StateObject private var viewModel = ScheduleManagementViewModel()
|
||||
|
||||
var body: some View {
|
||||
VStack(spacing: 0) {
|
||||
if accountContext.currentScenic?.id == nil {
|
||||
ContentUnavailableView("缺少景区", systemImage: "calendar.badge.exclamationmark", description: Text("请选择景区后再查看排班。"))
|
||||
AppContentUnavailableView("缺少景区", systemImage: "calendar.badge.exclamationmark", description: Text("请选择景区后再查看排班。"))
|
||||
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
||||
} else {
|
||||
ScrollView {
|
||||
@ -96,7 +96,7 @@ struct ScheduleManagementView: View {
|
||||
Text(viewModel.selectedDate.scheduleDayText)
|
||||
.font(.system(size: AppMetrics.FontSize.body, weight: .semibold))
|
||||
if viewModel.items.isEmpty {
|
||||
ContentUnavailableView("当天暂无日程", systemImage: "calendar")
|
||||
AppContentUnavailableView("当天暂无日程", systemImage: "calendar")
|
||||
.frame(minHeight: 180)
|
||||
} else {
|
||||
ForEach(viewModel.items) { item in
|
||||
@ -126,10 +126,10 @@ struct ScheduleManagementView: View {
|
||||
/// 新增排班页面,支持手动录入和关联订单。
|
||||
struct ScheduleAddView: View {
|
||||
@Environment(\.dismiss) private var dismiss
|
||||
@Environment(AccountContext.self) private var accountContext
|
||||
@Environment(ScheduleAPI.self) private var scheduleAPI
|
||||
@EnvironmentObject private var accountContext: AccountContext
|
||||
@Environment(\.scheduleAPI) private var scheduleAPI
|
||||
@Environment(\.globalLoading) private var globalLoading
|
||||
@State private var viewModel = ScheduleAddViewModel()
|
||||
@StateObject private var viewModel = ScheduleAddViewModel()
|
||||
@State private var date = Date()
|
||||
@State private var startTime = Date()
|
||||
@State private var endTime = Calendar.current.date(byAdding: .hour, value: 1, to: Date()) ?? Date()
|
||||
|
||||
Reference in New Issue
Block a user