Fix publishing changes during view updates warning

Defer state mutations in .onReceive handlers to next run loop
by wrapping them in Task { @MainActor in ... }
This commit is contained in:
世界
2026-01-05 14:15:53 +08:00
parent e68af19112
commit af9cfd0f59
3 changed files with 31 additions and 25 deletions
@@ -155,19 +155,23 @@ import SwiftUI
} }
#if os(iOS) || os(tvOS) #if os(iOS) || os(tvOS)
.onReceive(environments.commandClient.$groups) { _ in .onReceive(environments.commandClient.$groups) { _ in
Task { @MainActor in
updateButtonVisibility() updateButtonVisibility()
#if os(iOS) #if os(iOS)
if useLegacyTabView, coordinator.selection == .groups, !buttonState.showGroupsButton { if useLegacyTabView, coordinator.selection == .groups, !buttonState.showGroupsButton {
coordinator.selection = .overview coordinator.selection = .overview
} }
#endif #endif
}
}.onReceive(profile.$status) { _ in }.onReceive(profile.$status) { _ in
Task { @MainActor in
updateButtonVisibility() updateButtonVisibility()
#if os(iOS) #if os(iOS)
if useLegacyTabView, coordinator.selection == .groups, !buttonState.showGroupsButton { if useLegacyTabView, coordinator.selection == .groups, !buttonState.showGroupsButton {
coordinator.selection = .overview coordinator.selection = .overview
} }
#endif #endif
}
}.onAppear { }.onAppear {
updateButtonVisibility() updateButtonVisibility()
} }
+2
View File
@@ -77,6 +77,7 @@ public struct MainView: View {
} }
.onReceive(NotificationCenter.default.publisher(for: .navigateToSettingsPage)) { notification in .onReceive(NotificationCenter.default.publisher(for: .navigateToSettingsPage)) { notification in
guard let page = notification.object as? SettingsPage else { return } guard let page = notification.object as? SettingsPage else { return }
Task { @MainActor in
pendingSettingsPage = page pendingSettingsPage = page
if viewModel.selection == .settings { if viewModel.selection == .settings {
settingsNavigationPath = NavigationPath() settingsNavigationPath = NavigationPath()
@@ -86,6 +87,7 @@ public struct MainView: View {
viewModel.selection = .settings viewModel.selection = .settings
} }
} }
}
.environment(\.selection, $viewModel.selection) .environment(\.selection, $viewModel.selection)
.environment(\.importProfile, $viewModel.importProfile) .environment(\.importProfile, $viewModel.importProfile)
.environment(\.importRemoteProfile, $viewModel.importRemoteProfile) .environment(\.importRemoteProfile, $viewModel.importRemoteProfile)
+6 -6
View File
@@ -92,22 +92,22 @@ struct MainView: View {
updateButtonVisibility() updateButtonVisibility()
} }
.onReceive(environments.commandClient.$groups) { _ in .onReceive(environments.commandClient.$groups) { _ in
updateButtonVisibility() Task { @MainActor in updateButtonVisibility() }
} }
.onReceive(environments.commandClient.$connections) { _ in .onReceive(environments.commandClient.$connections) { _ in
updateButtonVisibility() Task { @MainActor in updateButtonVisibility() }
} }
.onReceive(environments.commandClient.$hasAnyConnection) { _ in .onReceive(environments.commandClient.$hasAnyConnection) { _ in
updateButtonVisibility() Task { @MainActor in updateButtonVisibility() }
} }
.onReceive(NotificationCenter.default.publisher(for: .NEVPNStatusDidChange)) { _ in .onReceive(NotificationCenter.default.publisher(for: .NEVPNStatusDidChange)) { _ in
updateButtonVisibility() Task { @MainActor in updateButtonVisibility() }
} }
.onReceive(environments.$extensionProfile) { _ in .onReceive(environments.$extensionProfile) { _ in
updateButtonVisibility() Task { @MainActor in updateButtonVisibility() }
} }
.onReceive(environments.$emptyProfiles) { _ in .onReceive(environments.$emptyProfiles) { _ in
updateButtonVisibility() Task { @MainActor in updateButtonVisibility() }
} }
.sheet(isPresented: $showGroups) { .sheet(isPresented: $showGroups) {
GroupsSheetContent() GroupsSheetContent()