11import FirebaseCore
22import SwiftUI
33
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 >
147}
158
169public struct PasswordRecoveryView {
1710 @Environment ( AuthService . self) private var authService
1811 @State private var email = " "
19- @State private var result : PasswordRecoveryResult ?
12+ @State private var resultWrapper : ResultWrapper ?
2013
2114 public init ( ) { }
2215
2316 private func sendPasswordRecoveryEmail( ) async {
17+ let recoveryResult : Result < Void , Error >
18+
2419 do {
2520 try await authService. sendPasswordRecoveryEmail ( to: email)
26- result = . success
21+ resultWrapper = ResultWrapper ( value : . success( ( ) ) )
2722 } catch {
28- result = . failure
23+ resultWrapper = ResultWrapper ( value : . failure( error ) )
2924 }
3025 }
3126}
@@ -47,9 +42,11 @@ extension PasswordRecoveryView: View {
4742 . submitLabel ( . next)
4843 } label: {
4944 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+
5350 Button ( action: {
5451 Task {
5552 await sendPasswordRecoveryEmail ( )
@@ -63,45 +60,9 @@ extension PasswordRecoveryView: View {
6360 . padding ( [ . top, . bottom, . horizontal] , 8 )
6461 . frame ( maxWidth: . infinity)
6562 . 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)
10566 }
10667 . navigationBarItems ( leading: Button ( action: {
10768 authService. authView = . authPicker
@@ -112,6 +73,46 @@ extension PasswordRecoveryView: View {
11273 . foregroundColor ( . blue)
11374 } )
11475 }
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+ }
115116}
116117
117118#Preview {
0 commit comments