Page Menu
Home
Musing Studio
Search
Configure Global Search
Log In
Files
F12142629
ErrorHandling.swift
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
1 KB
Subscribers
None
ErrorHandling.swift
View Options
// 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
())
}
}
File Metadata
Details
Attached
Mime Type
text/x-c++
Expires
Thu, Oct 9, 11:32 PM (1 d, 6 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3440345
Attached To
rWFSUI WriteFreely SwiftUI
Event Timeline
Log In to Comment