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
@@ -36,7 +36,7 @@
}
}
}
.alertBinding($viewModel.alert)
.alert($viewModel.alert)
.navigationTitle(navigationTitle)
#if os(macOS)
.toolbar {
@@ -33,7 +33,7 @@
do {
try await loadContentBackground()
} catch {
alert = Alert(error)
alert = AlertState(error: error)
}
isLoading = false
}
@@ -59,7 +59,7 @@
do {
try await saveContentBackground(profile)
} catch {
alert = Alert(error)
alert = AlertState(error: error)
return
}
isChanged = false
@@ -95,7 +95,7 @@ public struct EditProfileView: View {
viewModel.markAsChanged()
}
.disabled(viewModel.isLoading)
.alertBinding($viewModel.alert)
.alert($viewModel.alert)
}
#else
private var iOSBody: some View {
@@ -122,7 +122,7 @@ public struct EditProfileView: View {
}
}
#endif
.alertBinding($viewModel.alert)
.alert($viewModel.alert)
.navigationTitle("Edit Profile")
}
#endif
@@ -21,7 +21,7 @@ public final class EditProfileViewModel: BaseViewModel {
try await profile.updateRemoteProfile()
environments.profileUpdate.send()
} catch {
alert = Alert(error)
alert = AlertState(error: error)
}
}
@@ -29,7 +29,7 @@ public final class EditProfileViewModel: BaseViewModel {
do {
try await ProfileManager.delete(profile)
} catch {
alert = Alert(error)
alert = AlertState(error: error)
return
}
environments.profileUpdate.send()
@@ -46,7 +46,7 @@ public final class EditProfileViewModel: BaseViewModel {
#endif
try await profile.onProfileUpdated()
} catch {
alert = Alert(error)
alert = AlertState(error: error)
return
}
isChanged = false
@@ -59,7 +59,7 @@
}
}
.focusSection()
.alertBinding($viewModel.alert)
.alert($viewModel.alert)
.navigationTitle("Import Profile")
}
}
@@ -37,7 +37,7 @@
case let .failed(error):
DispatchQueue.main.async { [self] in
reset()
alert = Alert(error)
alert = AlertState(error: error)
}
default: break
}
@@ -46,7 +46,7 @@
do {
try await loopMessages(environments: environments, dismiss: dismiss)
} catch {
alert = Alert(error)
alert = AlertState(error: error)
reset()
}
}
@@ -115,7 +115,7 @@
try socket.write(request.encode())
isImporting = true
} catch {
alert = Alert(error)
alert = AlertState(error: error)
reset()
}
}
@@ -158,7 +158,7 @@ public struct NewProfileView: View {
}
}
.disabled(viewModel.isSaving)
.alertBinding($viewModel.alert)
.alert($viewModel.alert)
.fileImporter(
isPresented: $viewModel.pickerPresented,
allowedContentTypes: [.json],
@@ -170,7 +170,7 @@ public struct NewProfileView: View {
viewModel.fileURL = urls[0]
}
} catch {
viewModel.alert = Alert(error)
viewModel.alert = AlertState(error: error)
return
}
}
@@ -180,7 +180,7 @@ public struct NewProfileView: View {
formContent
.navigationTitle("New Profile")
.disabled(viewModel.isSaving)
.alertBinding($viewModel.alert)
.alert($viewModel.alert)
#if os(iOS)
.fileImporter(
isPresented: $viewModel.pickerPresented,
@@ -193,7 +193,7 @@ public struct NewProfileView: View {
viewModel.fileURL = urls[0]
}
} catch {
viewModel.alert = Alert(error)
viewModel.alert = AlertState(error: error)
return
}
}
@@ -45,17 +45,17 @@ public final class NewProfileViewModel: BaseViewModel {
defer { isSaving = false }
guard !profileName.isEmpty else {
alert = Alert(errorMessage: String(localized: "Missing profile name"))
alert = AlertState(errorMessage: String(localized: "Missing profile name"))
return
}
if profileType == .icloud, remotePath.isEmpty {
alert = Alert(errorMessage: String(localized: "Missing path"))
alert = AlertState(errorMessage: String(localized: "Missing path"))
return
}
if profileType == .remote, remotePath.isEmpty {
alert = Alert(errorMessage: String(localized: "Missing URL"))
alert = AlertState(errorMessage: String(localized: "Missing URL"))
return
}
@@ -63,7 +63,7 @@ public final class NewProfileViewModel: BaseViewModel {
do {
createdProfile = try await createProfileBackground()
} catch {
alert = Alert(error)
alert = AlertState(error: error)
return
}
@@ -88,7 +88,7 @@ public struct ProfileView: View {
}
}
.disabled(viewModel.isUpdating)
.alertBinding($viewModel.alert, $viewModel.isLoading)
.alert($viewModel.alert, isLoading: $viewModel.isLoading)
.onAppear {
if let profile = importProfile.wrappedValue {
importProfile.wrappedValue = nil
@@ -16,7 +16,7 @@ public class ProfileViewModel: BaseViewModel {
private weak var environments: ExtensionEnvironments?
public override init() {
override public init() {
super.init()
isLoading = true
}
@@ -26,15 +26,15 @@ public class ProfileViewModel: BaseViewModel {
}
public func createImportProfileDialog(_ profile: LibboxProfileContent) {
alert = Alert(
title: Text("Import Profile"),
message: Text("Are you sure to import profile \(profile.name)?"),
primaryButton: .default(Text("Import")) {
alert = AlertState(
title: String(localized: "Import Profile"),
message: String(localized: "Are you sure to import profile \(profile.name)?"),
primaryButton: .default(String(localized: "Import")) {
Task {
do {
try await profile.importProfile()
} catch {
self.alert = Alert(error)
self.alert = AlertState(error: error)
return
}
await self.doReload()
@@ -47,10 +47,10 @@ public class ProfileViewModel: BaseViewModel {
public func createImportRemoteProfileDialog(_ newValue: LibboxImportRemoteProfile) {
importRemoteProfileRequest = .init(name: newValue.name, url: newValue.url)
alert = Alert(
title: Text("Import Remote Profile"),
message: Text("Are you sure to import remote profile \(newValue.name)? You will connect to \(newValue.host) to download the configuration."),
primaryButton: .default(Text("Import")) {
alert = AlertState(
title: String(localized: "Import Remote Profile"),
message: String(localized: "Are you sure to import remote profile \(newValue.name)? You will connect to \(newValue.host) to download the configuration."),
primaryButton: .default(String(localized: "Import")) {
self.importRemoteProfilePresented = true
},
secondaryButton: .cancel()
@@ -70,7 +70,7 @@ public class ProfileViewModel: BaseViewModel {
do {
profileList = try await ProfileManager.list().map { ProfilePreview($0) }
} catch {
alert = Alert(error)
alert = AlertState(error: error)
return
}
}
@@ -87,7 +87,7 @@ public class ProfileViewModel: BaseViewModel {
_ = try await profile.updateRemoteProfile()
} catch {
await MainActor.run {
alert = Alert(error)
alert = AlertState(error: error)
}
}
}
@@ -98,7 +98,7 @@ public class ProfileViewModel: BaseViewModel {
environments?.profileUpdate.send()
environments?.emptyProfiles = profileList.isEmpty
} catch {
alert = Alert(error)
alert = AlertState(error: error)
}
}
@@ -113,7 +113,7 @@ public class ProfileViewModel: BaseViewModel {
try await ProfileManager.update(profileList.map(\.origin))
environments?.profileUpdate.send()
} catch {
alert = Alert(error)
alert = AlertState(error: error)
}
}
}
@@ -129,7 +129,7 @@ public class ProfileViewModel: BaseViewModel {
environments?.emptyProfiles = profileList.isEmpty
environments?.profileUpdate.send()
} catch {
alert = Alert(error)
alert = AlertState(error: error)
}
}
}