Я пытаюсь ввести программные изменения в состояние вкладки Tabview от координатора. Tabview работает и изменяет состояние, когда я нажимаю на новую вкладку, но не работает, когда я меняю состояние в координаторе. Я застрял здесь и буду признателен за советом.import SwiftUI
@main
struct ExAIApp: App {
@State private var coordinator = AppCoordinator()
@State private var tab: AppCoordinator.TabType = .workouts
var body: some Scene {
var tabSelection: Binding {
Binding(
get: {
print("Got tab binding: \(tab)")
return tab
},
set: { newValue in
print("Setting new tab: \(newValue)")
tab = newValue
coordinator.selectedTab = newValue
}
)
}
WindowGroup {
Group {
if coordinator.isAuthenticated {
TabView(selection: tabSelection) {
WorkoutsView()
.tabItem {
Label("Workouts", systemImage: "dumbbell.fill")
}
.tag(AppCoordinator.TabType.workouts)
ProfileView()
.tabItem {
Label("Profile", systemImage: "person.fill")
}
.tag(AppCoordinator.TabType.profile)
}
} else {
AuthenticationView()
}
}
.environment(coordinator)
}
}
}
< /code>
Мой координатор: < /p>
import SwiftUI
@Observable
final class AppCoordinator: Equatable {
// MARK: - Services
let authService: AuthenticationService
let databaseService: DatabaseService
let timerService: TimerService
let deepSeekService: DeepSeekService
// MARK: - View State
var selectedTab: TabType = .workouts
var isAuthenticated: Bool = false
// MARK: - Initialization
init() {
self.authService = AuthenticationService()
self.databaseService = DatabaseService()
self.timerService = TimerService()
self.deepSeekService = DeepSeekService()
// Initialize authentication state
self.isAuthenticated = authService.isAuthenticated
// Bind to authentication state
Task { @MainActor in
for await isAuthenticated in authService.$isAuthenticated.values {
self.isAuthenticated = isAuthenticated
if !isAuthenticated {
// Reset selected tab when signing out
self.selectedTab = .workouts
}
}
}
}
// MARK: - Navigation
enum TabType: Hashable, Equatable {
case workouts
case profile
}
func selectTab(_ tab: TabType) {
self.selectedTab = tab
}
// MARK: - App State
var currentUser: User? {
authService.currentUser
}
// MARK: - Equatable
static func == (lhs: AppCoordinator, rhs: AppCoordinator) -> Bool {
lhs === rhs
}
}
// MARK: - View Modifiers
struct AppCoordinatorKey: EnvironmentKey {
static let defaultValue = AppCoordinator()
}
extension EnvironmentValues {
var coordinator: AppCoordinator {
get { self[AppCoordinatorKey.self] }
set { self[AppCoordinatorKey.self] = newValue }
}
}
Подробнее здесь: https://stackoverflow.com/questions/796 ... controller