从 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,13 +6,12 @@
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import Observation
|
||||
import Combine
|
||||
|
||||
@MainActor
|
||||
@Observable
|
||||
/// 首页 ViewModel,根据当前角色权限构建首页菜单列表。
|
||||
final class HomeViewModel {
|
||||
private(set) var menuItems: [HomeMenuItem] = []
|
||||
final class HomeViewModel: ObservableObject {
|
||||
@Published private(set) var menuItems: [HomeMenuItem] = []
|
||||
|
||||
/// 菜单排序权重,与旧工程和 Android 端保持一致。
|
||||
private let preferredOrder: [String] = [
|
||||
|
||||
@ -9,12 +9,12 @@ import SwiftUI
|
||||
|
||||
/// 首页全部功能视图,展示常用应用和当前角色拥有的更多功能。
|
||||
struct HomeMoreFunctionsView: View {
|
||||
@Environment(PermissionContext.self) private var permissionContext
|
||||
@Environment(AppRouter.self) private var appRouter
|
||||
@Environment(RouterPath.self) private var router
|
||||
@EnvironmentObject private var permissionContext: PermissionContext
|
||||
@EnvironmentObject private var appRouter: AppRouter
|
||||
@EnvironmentObject private var router: RouterPath
|
||||
@Environment(\.dismiss) private var dismiss
|
||||
|
||||
@State private var viewModel = HomeViewModel()
|
||||
@StateObject private var viewModel = HomeViewModel()
|
||||
@State private var commonUris: [String] = []
|
||||
|
||||
private let commonMenuStore = HomeCommonMenuStore()
|
||||
@ -49,10 +49,10 @@ struct HomeMoreFunctionsView: View {
|
||||
.task {
|
||||
rebuildMenus()
|
||||
}
|
||||
.onChange(of: permissionContext.currentRole?.id) { _, _ in
|
||||
.onChange(of: permissionContext.currentRole?.id) { _ in
|
||||
rebuildMenus()
|
||||
}
|
||||
.onChange(of: permissionContext.rolePermissions.count) { _, _ in
|
||||
.onChange(of: permissionContext.rolePermissions.count) { _ in
|
||||
rebuildMenus()
|
||||
}
|
||||
}
|
||||
|
||||
@ -10,13 +10,13 @@ import SwiftUI
|
||||
|
||||
/// 首页主视图,展示景区、工作状态、快捷操作和权限菜单入口。
|
||||
struct HomeView: View {
|
||||
@Environment(AccountContext.self) private var accountContext
|
||||
@Environment(PermissionContext.self) private var permissionContext
|
||||
@Environment(AppRouter.self) private var appRouter
|
||||
@Environment(RouterPath.self) private var router
|
||||
@EnvironmentObject private var accountContext: AccountContext
|
||||
@EnvironmentObject private var permissionContext: PermissionContext
|
||||
@EnvironmentObject private var appRouter: AppRouter
|
||||
@EnvironmentObject private var router: RouterPath
|
||||
@Environment(\.horizontalSizeClass) private var horizontalSizeClass
|
||||
|
||||
@State private var viewModel = HomeViewModel()
|
||||
@StateObject private var viewModel = HomeViewModel()
|
||||
@State private var commonUris: [String] = []
|
||||
@State private var isOnline = false
|
||||
@State private var reminderMinutes = 0
|
||||
@ -113,10 +113,10 @@ struct HomeView: View {
|
||||
guard isOnline, secondsUntilReport > 0 else { return }
|
||||
secondsUntilReport -= 1
|
||||
}
|
||||
.onChange(of: permissionContext.currentRole?.id) { _, _ in
|
||||
.onChange(of: permissionContext.currentRole?.id) { _ in
|
||||
rebuildMenusFromCurrentContext()
|
||||
}
|
||||
.onChange(of: permissionContext.rolePermissions.count) { _, _ in
|
||||
.onChange(of: permissionContext.rolePermissions.count) { _ in
|
||||
rebuildMenusFromCurrentContext()
|
||||
}
|
||||
.alert("切换在线状态", isPresented: $showOnlineDialog) {
|
||||
@ -423,9 +423,9 @@ struct HomeView: View {
|
||||
#Preview {
|
||||
NavigationStack {
|
||||
HomeView()
|
||||
.environment(AccountContext())
|
||||
.environment(PermissionContext())
|
||||
.environment(AppRouter())
|
||||
.environment(RouterPath())
|
||||
.environmentObject(AccountContext())
|
||||
.environmentObject(PermissionContext())
|
||||
.environmentObject(AppRouter())
|
||||
.environmentObject(RouterPath())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user