1
1
import FirebaseCore
2
2
import SwiftUI
3
3
4
- enum PasswordRecoveryResult : Identifiable {
5
- case success
6
- case failure
7
-
8
- var id : String {
9
- switch self {
10
- case . success: return " success "
11
- case . failure: return " failure "
12
- }
13
- }
4
+ private struct ResultWrapper : Identifiable {
5
+ let id = UUID ( )
6
+ let value : Result < Void , Error >
14
7
}
15
8
16
9
public struct PasswordRecoveryView {
17
10
@Environment ( AuthService . self) private var authService
18
11
@State private var email = " "
19
- @State private var result : PasswordRecoveryResult ?
12
+ @State private var resultWrapper : ResultWrapper ?
20
13
21
14
public init ( ) { }
22
15
23
16
private func sendPasswordRecoveryEmail( ) async {
17
+ let recoveryResult : Result < Void , Error >
18
+
24
19
do {
25
20
try await authService. sendPasswordRecoveryEmail ( to: email)
26
- result = . success
21
+ resultWrapper = ResultWrapper ( value : . success( ( ) ) )
27
22
} catch {
28
- result = . failure
23
+ resultWrapper = ResultWrapper ( value : . failure( error ) )
29
24
}
30
25
}
31
26
}
@@ -47,9 +42,11 @@ extension PasswordRecoveryView: View {
47
42
. submitLabel ( . next)
48
43
} label: {
49
44
Image ( systemName: " at " )
50
- } . padding ( . vertical, 6 )
51
- . background ( Divider ( ) , alignment: . bottom)
52
- . padding ( . bottom, 4 )
45
+ }
46
+ . padding ( . vertical, 6 )
47
+ . background ( Divider ( ) , alignment: . bottom)
48
+ . padding ( . bottom, 4 )
49
+
53
50
Button ( action: {
54
51
Task {
55
52
await sendPasswordRecoveryEmail ( )
@@ -63,45 +60,9 @@ extension PasswordRecoveryView: View {
63
60
. padding ( [ . top, . bottom, . horizontal] , 8 )
64
61
. frame ( maxWidth: . infinity)
65
62
. buttonStyle ( . borderedProminent)
66
- } . sheet ( item: $result) { result in
67
- VStack {
68
- switch result {
69
- case . success:
70
- Text ( authService. string. passwordRecoveryEmailSentTitle)
71
- . font ( . largeTitle)
72
- . fontWeight ( . bold)
73
- . padding ( )
74
- Text ( authService. string. passwordRecoveryHelperMessage)
75
- . padding ( )
76
-
77
- Divider ( )
78
-
79
- Text ( authService. string. passwordRecoveryEmailSentMessage)
80
- . padding ( )
81
- Button ( authService. string. okButtonLabel) {
82
- self . result = nil
83
- }
84
- . padding ( )
85
- case . failure:
86
- Text ( authService. string. alertErrorTitle)
87
- . font ( . title)
88
- . fontWeight ( . semibold)
89
- . padding ( )
90
-
91
- Divider ( )
92
-
93
- Text ( authService. errorMessage)
94
- . padding ( )
95
-
96
- Divider ( )
97
-
98
- Button ( authService. string. okButtonLabel) {
99
- self . result = nil
100
- }
101
- . padding ( )
102
- }
103
- }
104
- . padding ( )
63
+ }
64
+ . sheet ( item: $resultWrapper) { wrapper in
65
+ resultSheet ( wrapper. value)
105
66
}
106
67
. navigationBarItems ( leading: Button ( action: {
107
68
authService. authView = . authPicker
@@ -112,6 +73,46 @@ extension PasswordRecoveryView: View {
112
73
. foregroundColor ( . blue)
113
74
} )
114
75
}
76
+
77
+ @ViewBuilder
78
+ @MainActor
79
+ private func resultSheet( _ result: Result < Void , Error > ) -> some View {
80
+ VStack {
81
+ switch result {
82
+ case . success:
83
+ Text ( authService. string. passwordRecoveryEmailSentTitle)
84
+ . font ( . largeTitle)
85
+ . fontWeight ( . bold)
86
+ . padding ( )
87
+ Text ( authService. string. passwordRecoveryHelperMessage)
88
+ . padding ( )
89
+
90
+ Divider ( )
91
+
92
+ Text ( String ( format: authService. string. passwordRecoveryEmailSentMessage, email) )
93
+ . padding ( )
94
+
95
+ case . failure:
96
+ Text ( authService. string. alertErrorTitle)
97
+ . font ( . title)
98
+ . fontWeight ( . semibold)
99
+ . padding ( )
100
+
101
+ Divider ( )
102
+
103
+ Text ( authService. errorMessage)
104
+ . padding ( )
105
+ }
106
+
107
+ Divider ( )
108
+
109
+ Button ( authService. string. okButtonLabel) {
110
+ self . resultWrapper = nil
111
+ }
112
+ . padding ( )
113
+ }
114
+ . padding ( )
115
+ }
115
116
}
116
117
117
118
#Preview {
0 commit comments