-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClosurePage.swift
107 lines (94 loc) · 2.43 KB
/
ClosurePage.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import Async
import SwiftUI
struct ClosurePage: View {
var body: some View {
List {
Section("Use Async Property") {
UseAsyncPropertyWrapper()
}
Section("Use AsyncView") {
UseAsyncView()
}
}
.listStyle(.grouped)
}
}
private struct UseAsyncPropertyWrapper: View {
@Async<String, Error> var async
@Async<String, Error> var asyncWithError
var body: some View {
VStack(alignment: .leading, spacing: 4) {
Text("Run basically")
switch async(Self.run).state {
case .success(let value):
Text(value)
case .failure(let error):
Text(error.errorMessage)
case .loading:
ProgressView()
.progressViewStyle(.circular)
}
}
VStack(alignment: .leading, spacing: 4) {
Text("Run with error")
switch asyncWithError(Self.runWithError).state {
case .success(let value):
Text(value)
case .failure(let error):
Text(error.errorMessage)
case .loading:
ProgressView()
.progressViewStyle(.circular)
}
}
.alert(isPresented: .constant(asyncWithError.error != nil), error: asyncWithError.error?.toAlertError()) {
Button("Reload") {
asyncWithError.resetState()
}
}
}
@Sendable private static func run() async throws -> String {
return "Done run()"
}
private static var i = 0
@Sendable private static func runWithError() async throws -> String {
if i == 0 {
i += 1
throw "Error"
} else {
return "Done runWithError()"
}
}
}
private struct UseAsyncView: View {
var body: some View {
VStack(alignment: .leading, spacing: 4) {
Text("Run basically")
AsyncView(Self.run, when: (
success: { Text($0) },
failure: { Text($0.errorMessage) },
loading: { ProgressView().progressViewStyle(.circular) }
))
}
VStack(alignment: .leading, spacing: 4) {
Text("Run with error")
AsyncView(Self.runWithError, when: (
success: { Text($0) },
failure: { Text($0.errorMessage) },
loading: { ProgressView().progressViewStyle(.circular) }
))
}
}
@Sendable private static func run() async throws -> String {
return "Done run()"
}
private static var i = 0
@Sendable private static func runWithError() async throws -> String {
if i == 0 {
i += 1
throw "Error"
} else {
return "Done runWithError()"
}
}
}