From a3b805a31907fe045f47f41da54a1169df653834 Mon Sep 17 00:00:00 2001 From: Angelo Stavrow Date: Fri, 13 May 2022 07:20:47 -0400 Subject: [PATCH] Add error handling to top-level content view --- Shared/Navigation/ContentView.swift | 15 +++++++++++++++ Shared/WriteFreely_MultiPlatformApp.swift | 1 + 2 files changed, 16 insertions(+) diff --git a/Shared/Navigation/ContentView.swift b/Shared/Navigation/ContentView.swift index a3addfe..f1e82a0 100644 --- a/Shared/Navigation/ContentView.swift +++ b/Shared/Navigation/ContentView.swift @@ -2,11 +2,13 @@ import SwiftUI struct ContentView: View { @EnvironmentObject var model: WriteFreelyModel + @EnvironmentObject var errorHandling: ErrorHandling var body: some View { NavigationView { #if os(macOS) CollectionListView() + .withErrorHandling() .toolbar { Button( action: { @@ -36,6 +38,7 @@ struct ContentView: View { } #else CollectionListView() + .withErrorHandling() #endif #if os(macOS) @@ -56,6 +59,18 @@ struct ContentView: View { Text("Select a post, or create a new local draft.") .foregroundColor(.secondary) + + EmptyView() + .onChange(of: model.hasError) { value in + if value { + if let error = model.currentError { + self.errorHandling.handle(error: error) + } else { + self.errorHandling.handle(error: AppError.genericError) + } + model.hasError = false + } + } } .environmentObject(model) } diff --git a/Shared/WriteFreely_MultiPlatformApp.swift b/Shared/WriteFreely_MultiPlatformApp.swift index 4378ada..76ff61b 100644 --- a/Shared/WriteFreely_MultiPlatformApp.swift +++ b/Shared/WriteFreely_MultiPlatformApp.swift @@ -48,6 +48,7 @@ struct WriteFreely_MultiPlatformApp: App { } } }) + .withErrorHandling() .environmentObject(model) .environment(\.managedObjectContext, LocalStorageManager.standard.container.viewContext) // .preferredColorScheme(preferences.selectedColorScheme) // See PreferencesModel for info.