mirror of
https://github.com/writeas/writefreely-swiftui-multiplatform.git
synced 2024-11-15 01:11:02 +00:00
a51bbd3abc
* Initial work on presenting alert on error
* Move Account-related error handling up the hierarchy
* Handle errors on logout
* Fix for temporary debugging
* Clean up WriteFreelyModel’s published vars
* Add error handling to top-level content view
* Set current error on API call failures
* Set current error on API call handlers
* Move User Defaults errors to ErrorConstants file
* Add default values for some error strings
* Handle purging post errors
* Add FIXME to track silent failure on fetching collections
As collections are fetched and added to the `list` property in the CollectionListModel’s initializer, it’s tricky to throw an error here: we call it as a property initializer in CollectionListView, which cannot throw.
Consider refactoring this logic such that we’re using, for example, a @FetchRequest in CollectionListView instead.
* Handle errors in (most) shared code
Two outliers to come back to are:
- the LocalStoreManager, where we can’t set a current error in the WriteFreelyModel in methods that can’t throw
- the CollectionListModel, where the initializer can’t throw because we use it as a property initializer in CollectionListView
* Add error handling to Mac app
* Revert "Add error handling to Mac app"
This reverts commit b1a8b8b29c
.
43 lines
1.2 KiB
Swift
43 lines
1.2 KiB
Swift
// 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())
|
|
}
|
|
}
|