Refactor Alerts

This commit is contained in:
世界
2025-12-01 18:22:06 +08:00
parent 8e764c56eb
commit 4ddfcdb324
40 changed files with 324 additions and 224 deletions
+1 -1
View File
@@ -76,7 +76,7 @@ struct EditProfileContentWindow: View {
} message: {
Text("Do you want to save the changes you made?")
}
.alertBinding($viewModel.alert)
.alert($viewModel.alert)
.navigationTitle(navigationTitle)
.toolbar {
ToolbarItemGroup(placement: .navigation) {
+1 -1
View File
@@ -32,7 +32,7 @@ public struct MainView: View {
.onAppear {
viewModel.onAppear(environments: environments)
}
.alertBinding($viewModel.alert)
.alert($viewModel.alert)
.toolbar {
ToolbarItem(placement: .navigation) {
StartStopButton()
+7 -7
View File
@@ -9,7 +9,7 @@ public class MainViewModel: ObservableObject {
@Published public var selection = NavigationPage.dashboard
@Published public var importProfile: LibboxProfileContent?
@Published public var importRemoteProfile: LibboxImportRemoteProfile?
@Published public var alert: Alert?
@Published public var alert: AlertState?
public init() {}
@@ -53,7 +53,7 @@ public class MainViewModel: ObservableObject {
await importURLProfile(url)
}
} else {
alert = Alert(errorMessage: String(localized: "Handled unknown URL \(url.absoluteString)"))
alert = AlertState(errorMessage: String(localized: "Handled unknown URL \(url.absoluteString)"))
}
}
@@ -63,7 +63,7 @@ public class MainViewModel: ObservableObject {
importProfile = try await .from(readURL(url))
url.stopAccessingSecurityScopedResource()
} catch {
alert = Alert(error)
alert = AlertState(error: error)
return
}
if selection != .dashboard {
@@ -78,10 +78,10 @@ public class MainViewModel: ObservableObject {
private func checkApplicationPath() {
let directoryName = URL(filePath: Bundle.main.bundlePath).deletingLastPathComponent().pathComponents.last
if directoryName != "Applications" {
alert = Alert(
title: Text("Wrong application location"),
message: Text("This app needs to be placed under the Applications folder to work."),
dismissButton: .default(Text("Ok")) {
alert = AlertState(
title: String(localized: "Wrong application location"),
message: String(localized: "This app needs to be placed under the Applications folder to work."),
dismissButton: .default(String(localized: "Ok")) {
NSWorkspace.shared.selectFile(Bundle.main.bundlePath, inFileViewerRootedAtPath: "")
NSApp.terminate(nil)
}
+7 -7
View File
@@ -72,7 +72,7 @@ public struct MenuView: View {
private struct StatusSwitch: View {
@ObservedObject private var profile: ExtensionProfile
@State private var alert: Alert?
@State private var alert: AlertState?
init(_ profile: ExtensionProfile) {
self.profile = profile
@@ -88,7 +88,7 @@ public struct MenuView: View {
})) {}
.toggleStyle(.switch)
.disabled(!profile.status.isEnabled)
.alertBinding($alert)
.alert($alert)
}
private func switchProfile(_ isEnabled: Bool) async {
@@ -99,7 +99,7 @@ public struct MenuView: View {
try await profile.stop()
}
} catch {
alert = Alert(error)
alert = AlertState(error: error)
return
}
}
@@ -117,7 +117,7 @@ public struct MenuView: View {
@State private var profileList: [ProfilePreview] = []
@State private var selectedProfileID: Int64 = 0
@State private var reasserting = false
@State private var alert: Alert?
@State private var alert: AlertState?
private var selectedProfileIDLocal: Binding<Int64> {
$selectedProfileID.withSetter { newValue in
@@ -161,7 +161,7 @@ public struct MenuView: View {
selectedProfileID = await SharedPreferences.selectedProfileID.get()
}
}
.alertBinding($alert)
.alert($alert)
}
private func doReload() async {
@@ -171,7 +171,7 @@ public struct MenuView: View {
do {
profileList = try await ProfileManager.list().map { ProfilePreview($0) }
} catch {
alert = Alert(error)
alert = AlertState(error: error)
return
}
if profileList.isEmpty {
@@ -194,7 +194,7 @@ public struct MenuView: View {
do {
try await serviceReload()
} catch {
alert = Alert(error)
alert = AlertState(error: error)
}
}
reasserting = false