从 iOS 17 Observation 迁移至 iOS 16 兼容的 Combine 架构

将最低部署版本降至 iOS 16,以 ObservableObject 替换 @Observable,新增导航与 UI 兼容层,并补充登录冒烟 UI 测试。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-06-26 10:16:35 +08:00
parent 0314033a7f
commit 703078352c
127 changed files with 2320 additions and 1465 deletions

View File

@ -6,7 +6,7 @@
//
import Foundation
import Observation
import Combine
/// 便
@MainActor
@ -29,9 +29,8 @@ protocol PunchPointServing {
/// API
@MainActor
@Observable
final class PunchPointAPI: PunchPointServing {
@ObservationIgnored private let client: APIClient
private let client: APIClient
/// API
init(client: APIClient) {

View File

@ -6,21 +6,20 @@
//
import Foundation
import Observation
import Combine
/// ViewModel
@MainActor
@Observable
final class PunchPointListViewModel {
var selectedFilter: PunchPointFilter = .all
var items: [PunchPointItem] = []
var selectedDetail: PunchPointItem?
var errorMessage: String?
var isLoading = false
var isLoadingMore = false
var total = 0
final class PunchPointListViewModel: ObservableObject {
@Published var selectedFilter: PunchPointFilter = .all
@Published var items: [PunchPointItem] = []
@Published var selectedDetail: PunchPointItem?
@Published var errorMessage: String?
@Published var isLoading = false
@Published var isLoadingMore = false
@Published var total = 0
private var page = 1
@Published private var page = 1
private let pageSize = 20
///
@ -121,35 +120,41 @@ final class PunchPointListViewModel {
/// ViewModel OSS
@MainActor
@Observable
final class PunchPointEditorViewModel {
var name = ""
var description = ""
var address = ""
var latitudeText = ""
var longitudeText = ""
var scenicSpotText = ""
var remoteImages: [String] = []
var localImages: [PunchPointLocalImage] = []
var errorMessage: String?
var isSubmitting = false
final class PunchPointEditorViewModel: ObservableObject {
@Published var name = ""
@Published var description = ""
@Published var address = ""
@Published var latitudeText = ""
@Published var longitudeText = ""
@Published var scenicSpotText = ""
@Published var remoteImages: [String] = []
@Published var localImages: [PunchPointLocalImage] = []
@Published var errorMessage: String?
@Published var isSubmitting = false
let editingItem: PunchPointItem?
private(set) var editingItem: PunchPointItem?
/// ViewModel
init(item: PunchPointItem? = nil) {
editingItem = item
if let item {
name = item.name
description = item.description
address = item.region?.address ?? ""
latitudeText = item.region.map { String($0.lat) } ?? ""
longitudeText = item.region.map { String($0.lot) } ?? ""
scenicSpotText = item.scenicSpotStr
remoteImages = item.guideImages
apply(item)
}
}
/// ObservableObject
func apply(_ item: PunchPointItem) {
editingItem = item
name = item.name
description = item.description
address = item.region?.address ?? ""
latitudeText = item.region.map { String($0.lat) } ?? ""
longitudeText = item.region.map { String($0.lot) } ?? ""
scenicSpotText = item.scenicSpotStr
remoteImages = item.guideImages
localImages = []
}
///
func applyLocation(latitude: Double, longitude: Double, address: String) {
latitudeText = String(format: "%.6f", latitude)

View File

@ -11,15 +11,15 @@ import UIKit
///
struct PunchPointListView: View {
@Environment(AccountContext.self) private var accountContext
@Environment(AccountContextAPI.self) private var accountContextAPI
@Environment(PunchPointAPI.self) private var punchPointAPI
@Environment(RouterPath.self) private var router
@Environment(ScenicSpotContext.self) private var scenicSpotContext
@Environment(ToastCenter.self) private var toastCenter
@EnvironmentObject private var accountContext: AccountContext
@Environment(\.accountContextAPI) private var accountContextAPI
@Environment(\.punchPointAPI) private var punchPointAPI
@EnvironmentObject private var router: RouterPath
@EnvironmentObject private var scenicSpotContext: ScenicSpotContext
@EnvironmentObject private var toastCenter: ToastCenter
@Environment(\.globalLoading) private var globalLoading
@State private var viewModel = PunchPointListViewModel()
@StateObject private var viewModel = PunchPointListViewModel()
var body: some View {
ScrollView {
@ -58,7 +58,7 @@ struct PunchPointListView: View {
}
}
.pickerStyle(.segmented)
.onChange(of: viewModel.selectedFilter) { _, newValue in
.onChange(of: viewModel.selectedFilter) { newValue in
Task {
await globalLoading.withOptionalLoading(currentScenicId != nil) {
await viewModel.selectFilter(newValue, scenicId: currentScenicId, api: punchPointAPI)
@ -116,15 +116,15 @@ struct PunchPointDetailView: View {
let punchPointId: Int
let summary: PunchPointItem?
@Environment(AccountContext.self) private var accountContext
@Environment(AccountContextAPI.self) private var accountContextAPI
@Environment(PunchPointAPI.self) private var punchPointAPI
@Environment(RouterPath.self) private var router
@Environment(ScenicSpotContext.self) private var scenicSpotContext
@Environment(ToastCenter.self) private var toastCenter
@EnvironmentObject private var accountContext: AccountContext
@Environment(\.accountContextAPI) private var accountContextAPI
@Environment(\.punchPointAPI) private var punchPointAPI
@EnvironmentObject private var router: RouterPath
@EnvironmentObject private var scenicSpotContext: ScenicSpotContext
@EnvironmentObject private var toastCenter: ToastCenter
@Environment(\.globalLoading) private var globalLoading
@State private var viewModel = PunchPointListViewModel()
@StateObject private var viewModel = PunchPointListViewModel()
@State private var showDeleteConfirm = false
var item: PunchPointItem? {
@ -275,17 +275,17 @@ struct PunchPointDetailView: View {
struct PunchPointEditorView: View {
let punchPointId: Int?
@Environment(AccountContext.self) private var accountContext
@Environment(AccountContextAPI.self) private var accountContextAPI
@Environment(PunchPointAPI.self) private var punchPointAPI
@Environment(OSSUploadService.self) private var uploadService
@Environment(ScenicSpotContext.self) private var scenicSpotContext
@Environment(ToastCenter.self) private var toastCenter
@EnvironmentObject private var accountContext: AccountContext
@Environment(\.accountContextAPI) private var accountContextAPI
@Environment(\.punchPointAPI) private var punchPointAPI
@Environment(\.ossUploadService) private var uploadService
@EnvironmentObject private var scenicSpotContext: ScenicSpotContext
@EnvironmentObject private var toastCenter: ToastCenter
@Environment(\.dismiss) private var dismiss
@Environment(\.globalLoading) private var globalLoading
@State private var detailLoader = PunchPointListViewModel()
@State private var viewModel = PunchPointEditorViewModel()
@StateObject private var detailLoader = PunchPointListViewModel()
@StateObject private var viewModel = PunchPointEditorViewModel()
@State private var locationProvider = ForegroundLocationProvider()
@State private var selectedItems: [PhotosPickerItem] = []
@ -308,11 +308,11 @@ struct PunchPointEditorView: View {
await globalLoading.withOptionalLoading(detailLoader.selectedDetail == nil) {
await detailLoader.loadDetail(id: punchPointId, api: punchPointAPI)
if let detail = detailLoader.selectedDetail {
viewModel = PunchPointEditorViewModel(item: detail)
viewModel.apply(detail)
}
}
}
.onChange(of: selectedItems) { _, items in
.onChange(of: selectedItems) { items in
Task { await loadPickedImages(items) }
}
}