Newer
Older
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
import ComposableArchitecture
import SwiftUI
public struct ConfirmRequestView: View {
public init(store: Store<ConfirmRequestState, ConfirmRequestAction>) {
self.store = store
}
let store: Store<ConfirmRequestState, ConfirmRequestAction>
struct ViewState: Equatable {
var username: String?
var email: String?
var phone: String?
var isConfirming: Bool
var result: ConfirmRequestState.Result?
init(state: ConfirmRequestState) {
username = try? state.contact.getFact(.username)?.value
email = try? state.contact.getFact(.email)?.value
phone = try? state.contact.getFact(.phone)?.value
isConfirming = state.isConfirming
result = state.result
}
}
public var body: some View {
WithViewStore(store, observe: ViewState.init) { viewStore in
Form {
Section {
Label(viewStore.username ?? "", systemImage: "person")
Label(viewStore.email ?? "", systemImage: "envelope")
Label(viewStore.phone ?? "", systemImage: "phone")
} header: {
Text("Facts")
}
Section {
Button {
viewStore.send(.confirmTapped)
} label: {
HStack {
Text("Confirm")
Spacer()
if viewStore.isConfirming {
ProgressView()
} else {
Image(systemName: "checkmark")
}
}
}
.disabled(viewStore.isConfirming)
}
if let result = viewStore.result {
Section {
HStack {
switch result {
case .success:
Text("Request confirmed")
Spacer()
Image(systemName: "person.fill.checkmark")
case .failure(_):
Text("Confirming request failed")
Spacer()
Image(systemName: "xmark")
}
}
if case .failure(let failure) = result {
Text(failure)
}
} header: {
Text("Result")
}
}
}
.navigationTitle("Confirm request")
}
}
}
#if DEBUG
public struct ConfirmRequestView_Previews: PreviewProvider {
public static var previews: some View {
ConfirmRequestView(store: Store(
initialState: ConfirmRequestState(
contact: .unimplemented("contact-data".data(using: .utf8)!)
),
reducer: .empty,
environment: ()
))
}
}
#endif