From 93c016268a935d6905cda97d5d53384d30d87e28 Mon Sep 17 00:00:00 2001 From: Angelo Stavrow Date: Thu, 28 Jul 2022 07:38:19 -0400 Subject: [PATCH] Add error handling to iOS post editor (#210) (#214) * Add error handling to post editor * Cleanup * Add error handling to macOS (#211) * Add error handling to Mac app * Log fatal errors and present alert on next launch (#212) * Log fatal crashes and present alert on next launch * Update crash alert copy and navigate to help forum * Refactor logging into reuseable methods * Refactor class to use protocol * Clean up todo comment * Revert "Log fatal errors and present alert on next launch (#212)" This reverts commit 7475b577726faffec856dbdac4c69ed79501b972. --- Shared/WriteFreely_MultiPlatformApp.swift | 1 + macOS/PostEditor/PostEditorView.swift | 11 +++++++++++ macOS/Settings/MacAccountView.swift | 15 +++++++++++++-- macOS/Settings/MacPreferencesView.swift | 13 +++++++++++++ macOS/Settings/MacUpdatesView.swift | 13 +++++++++++++ 5 files changed, 51 insertions(+), 2 deletions(-) diff --git a/Shared/WriteFreely_MultiPlatformApp.swift b/Shared/WriteFreely_MultiPlatformApp.swift index 76ff61b..d17c965 100644 --- a/Shared/WriteFreely_MultiPlatformApp.swift +++ b/Shared/WriteFreely_MultiPlatformApp.swift @@ -115,6 +115,7 @@ struct WriteFreely_MultiPlatformApp: App { } .tag(2) } + .withErrorHandling() .frame(minWidth: 500, maxWidth: 500, minHeight: 200) .padding() // .preferredColorScheme(preferences.selectedColorScheme) // See PreferencesModel for info. diff --git a/macOS/PostEditor/PostEditorView.swift b/macOS/PostEditor/PostEditorView.swift index 56bedc9..b76e921 100644 --- a/macOS/PostEditor/PostEditorView.swift +++ b/macOS/PostEditor/PostEditorView.swift @@ -2,6 +2,7 @@ import SwiftUI struct PostEditorView: View { @EnvironmentObject var model: WriteFreelyModel + @EnvironmentObject var errorHandling: ErrorHandling @ObservedObject var post: WFAPost @State private var isHovering: Bool = false @@ -38,6 +39,16 @@ struct PostEditorView: View { LocalStorageManager.standard.saveContext() } }) + .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 + } + } .onDisappear(perform: { DispatchQueue.main.async { model.editor.clearLastDraft() diff --git a/macOS/Settings/MacAccountView.swift b/macOS/Settings/MacAccountView.swift index f0d4c30..9939e99 100644 --- a/macOS/Settings/MacAccountView.swift +++ b/macOS/Settings/MacAccountView.swift @@ -2,11 +2,22 @@ import SwiftUI struct MacAccountView: View { @EnvironmentObject var model: WriteFreelyModel + @EnvironmentObject var errorHandling: ErrorHandling var body: some View { - Form { - AccountView() + Form { + AccountView() + } + .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 } + } } } diff --git a/macOS/Settings/MacPreferencesView.swift b/macOS/Settings/MacPreferencesView.swift index 85fa829..feb91e5 100644 --- a/macOS/Settings/MacPreferencesView.swift +++ b/macOS/Settings/MacPreferencesView.swift @@ -1,6 +1,9 @@ import SwiftUI struct MacPreferencesView: View { + @EnvironmentObject var model: WriteFreelyModel + @EnvironmentObject var errorHandling: ErrorHandling + @ObservedObject var preferences: PreferencesModel var body: some View { @@ -8,6 +11,16 @@ struct MacPreferencesView: View { PreferencesView(preferences: preferences) Spacer() } + .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 + } + } } } diff --git a/macOS/Settings/MacUpdatesView.swift b/macOS/Settings/MacUpdatesView.swift index afb6c48..ba9f6a3 100644 --- a/macOS/Settings/MacUpdatesView.swift +++ b/macOS/Settings/MacUpdatesView.swift @@ -2,6 +2,9 @@ import SwiftUI import Sparkle struct MacUpdatesView: View { + @EnvironmentObject var model: WriteFreelyModel + @EnvironmentObject var errorHandling: ErrorHandling + @ObservedObject var updaterViewModel: MacUpdatesViewModel @AppStorage(WFDefaults.automaticallyChecksForUpdates, store: UserDefaults.shared) @@ -81,6 +84,16 @@ that can cause crashes and data loss. .onChange(of: subscribeToBetaUpdates) { _ in updaterViewModel.toggleAllowedChannels() } + .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 + } + } } }