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
@@ -9,7 +9,7 @@ public struct ExtensionStatusView: View {
@EnvironmentObject private var commandClient: CommandClient
@State private var columnCount: Int = 4
@State private var alert: Alert?
@State private var alert: AlertState?
private let infoFont = Font.system(.caption, design: .monospaced)
@@ -91,7 +91,7 @@ public struct ExtensionStatusView: View {
.frame(alignment: .topLeading)
.padding([.top, .leading, .trailing])
}
.alertBinding($alert)
.alert($alert)
}
private func updateColumnCount(_ width: Double) {
@@ -3,7 +3,7 @@ import SwiftUI
@MainActor
public struct InstallProfileButton: View {
@State private var alert: Alert?
@State private var alert: AlertState?
private let callback: () async -> Void
public init(_ callback: @escaping (() async -> Void)) {
@@ -18,7 +18,7 @@ public struct InstallProfileButton: View {
} label: {
Label("Install Network Extension", systemImage: "lock.doc.fill")
}
.alertBinding($alert)
.alert($alert)
}
private func installProfile() async {
@@ -26,7 +26,7 @@ public struct InstallProfileButton: View {
try await ExtensionProfile.install()
await callback()
} catch {
alert = Alert(error)
alert = AlertState(error: error)
}
}
}
@@ -5,7 +5,7 @@
@MainActor
public struct InstallSystemExtensionButton: View {
@State private var alert: Alert?
@State private var alert: AlertState?
private let callback: () async -> Void
public init(_ callback: @escaping () async -> Void) {
self.callback = callback
@@ -19,19 +19,19 @@
} label: {
Label("Install System Extension", systemImage: "lock.doc.fill")
}
.alertBinding($alert)
.alert($alert)
}
private func installSystemExtension() async {
do {
if let result = try await SystemExtension.install() {
if result == .willCompleteAfterReboot {
alert = Alert(errorMessage: String(localized: "Need Reboot"))
alert = AlertState(errorMessage: String(localized: "Need Reboot"))
}
}
await callback()
} catch {
alert = Alert(error)
alert = AlertState(error: error)
}
}
}
@@ -39,7 +39,7 @@ public struct StartStopButton: View {
private struct ToggleConnectionButton: View {
@EnvironmentObject private var environments: ExtensionEnvironments
@EnvironmentObject private var profile: ExtensionProfile
@State private var alert: Alert?
@State private var alert: AlertState?
@State private var currentTime = Date()
private let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
@@ -105,7 +105,7 @@ public struct StartStopButton: View {
.modifier(PrimaryTintModifier())
#endif
.disabled(!profile.status.isEnabled)
.alertBinding($alert)
.alert($alert)
.onReceive(timer) { _ in
currentTime = Date()
}
@@ -146,7 +146,7 @@ public struct StartStopButton: View {
}
} catch {
await MainActor.run {
alert = Alert(error)
alert = AlertState(error: error)
}
}
}