swiftui-multiplatform/Shared/ErrorHandling/ErrorHandling.swift

43 lines
1.2 KiB
Swift
Raw Normal View History

// Based on https://www.ralfebert.com/swiftui/generic-error-handling/
import SwiftUI
struct ErrorAlert: Identifiable {
var id = UUID()
var message: String
var dismissAction: (() -> Void)?
}
class ErrorHandling: ObservableObject {
@Published var currentAlert: ErrorAlert?
func handle(error: Error) {
currentAlert = ErrorAlert(message: error.localizedDescription)
}
}
struct HandleErrorByShowingAlertViewModifier: ViewModifier {
@StateObject var errorHandling = ErrorHandling()
func body(content: Content) -> some View {
content
.environmentObject(errorHandling)
.background(
EmptyView()
.alert(item: $errorHandling.currentAlert) { currentAlert in
Alert(title: Text("Error"),
message: Text(currentAlert.message),
dismissButton: .default(Text("OK")) {
currentAlert.dismissAction?()
})
}
)
}
}
extension View {
func withErrorHandling() -> some View {
modifier(HandleErrorByShowingAlertViewModifier())
}
}