1919import SwiftUI
2020import AuthenticationServices
2121
22- enum AuthenticationState {
23- case unauthenticated
24- case authenticating
25- case authenticated
22+ private enum FocusableField : Hashable {
23+ case email
24+ case password
25+ case confirmPassword
2626}
2727
28- enum AuthenticationFlow {
28+ private enum AuthenticationFlow {
2929 case login
3030 case signUp
3131}
3232
33- @ Observable
34- class AuthenticationViewModel {
35- var presentingAuthenticationDialog = false
36- var presentingAccountDialog = false
33+ struct AuthenticationScreen : View {
34+ @ Environment ( AuthenticationService . self ) var authenticationService
35+ @ Environment ( \ . colorScheme ) private var colorScheme
36+ @ Environment ( \ . dismiss ) private var dismiss
3737
38- var email = " "
39- var password = " "
40- var confirmPassword = " "
38+ @ State private var email = " "
39+ @ State private var password = " "
40+ @ State private var confirmPassword = " "
4141
42- var flow : AuthenticationFlow = . login
42+ @ State private var flow : AuthenticationFlow = . login
4343
44- var authenticationState : AuthenticationState = . unauthenticated
45- var errorMessage = " "
46- var displayName = " "
44+ @State private var errorMessage = " "
45+ @State private var displayName = " "
4746
48- var isValid : Bool {
47+ private var isValid : Bool {
4948 return if flow == . login {
5049 !email. isEmpty && !password. isEmpty
5150 }
@@ -54,53 +53,39 @@ class AuthenticationViewModel {
5453 }
5554 }
5655
57- func switchFlow( ) {
56+ private func switchFlow( ) {
5857 flow = flow == . login ? . signUp : . login
5958 errorMessage = " "
6059 }
6160
62- func signOut( ) {
63- authenticationState = . unauthenticated
64- }
65- }
66-
67- private enum FocusableField : Hashable {
68- case email
69- case password
70- case confirmPassword
71- }
72-
73- struct AuthenticationScreen : View {
74- @Environment ( AuthenticationViewModel . self) var viewModel
75- @Environment ( \. colorScheme) private var colorScheme
76- @Environment ( \. dismiss) private var dismiss
77-
7861 @FocusState private var focus : FocusableField ?
7962
8063 private func signInWithEmailPassword( ) {
81- if viewModel. authenticationState == . authenticated {
82- viewModel. authenticationState = . unauthenticated
83- dismiss ( )
84- }
85- else {
86- viewModel. authenticationState = . authenticated
87- dismiss ( )
64+ Task {
65+ do {
66+ try await authenticationService. signInWithEmailPassword ( email: email, password: password)
67+ dismiss ( )
68+ } catch {
69+ print ( error. localizedDescription)
70+ errorMessage = error. localizedDescription
71+ }
8872 }
8973 }
9074
9175 private func signUpWithEmailPassword( ) {
92- if viewModel . authenticationState == . authenticated {
93- viewModel . authenticationState = . unauthenticated
76+ if authenticationService . authenticationState == . authenticated {
77+ authenticationService . authenticationState = . unauthenticated
9478 dismiss ( )
9579 }
9680 else {
97- viewModel . authenticationState = . authenticated
81+ authenticationService . authenticationState = . authenticated
9882 dismiss ( )
9983 }
10084 }
85+ }
10186
87+ extension AuthenticationScreen {
10288 var body : some View {
103- @Bindable var viewModel = viewModel
10489 VStack {
10590// Image("login")
10691// .resizable()
@@ -114,7 +99,7 @@ struct AuthenticationScreen: View {
11499
115100 HStack {
116101 Image ( systemName: " at " )
117- TextField ( " Email " , text: $viewModel . email)
102+ TextField ( " Email " , text: $email)
118103 . textInputAutocapitalization ( . never)
119104 . disableAutocorrection ( true )
120105 . focused ( $focus, equals: . email)
@@ -129,7 +114,7 @@ struct AuthenticationScreen: View {
129114
130115 HStack {
131116 Image ( systemName: " lock " )
132- SecureField ( " Password " , text: $viewModel . password)
117+ SecureField ( " Password " , text: $password)
133118 . focused ( $focus, equals: . password)
134119 . submitLabel ( . go)
135120 . onSubmit {
@@ -140,10 +125,10 @@ struct AuthenticationScreen: View {
140125 . background ( Divider ( ) , alignment: . bottom)
141126 . padding ( . bottom, 8 )
142127
143- if viewModel . flow == . signUp {
128+ if flow == . signUp {
144129 HStack {
145130 Image ( systemName: " lock " )
146- SecureField ( " Confirm password " , text: $viewModel . confirmPassword)
131+ SecureField ( " Confirm password " , text: $confirmPassword)
147132 . focused ( $focus, equals: . confirmPassword)
148133 . submitLabel ( . go)
149134 . onSubmit {
@@ -155,16 +140,16 @@ struct AuthenticationScreen: View {
155140 . padding ( . bottom, 8 )
156141 }
157142
158- if !viewModel . errorMessage. isEmpty {
143+ if !errorMessage. isEmpty {
159144 VStack {
160- Text ( viewModel . errorMessage)
145+ Text ( errorMessage)
161146 . foregroundColor ( Color ( UIColor . systemRed) )
162147 }
163148 }
164149
165150 Button ( action: signInWithEmailPassword) {
166- if viewModel . authenticationState != . authenticating {
167- Text ( viewModel . flow == . login ? " Log in with password " : " Sign up " )
151+ if authenticationService . authenticationState != . authenticating {
152+ Text ( flow == . login ? " Log in with password " : " Sign up " )
168153 . padding ( . vertical, 8 )
169154 . frame ( maxWidth: . infinity)
170155 }
@@ -175,7 +160,7 @@ struct AuthenticationScreen: View {
175160 . frame ( maxWidth: . infinity)
176161 }
177162 }
178- . disabled ( !viewModel . isValid)
163+ . disabled ( !isValid)
179164 . frame ( maxWidth: . infinity)
180165 . buttonStyle ( . borderedProminent)
181166
@@ -185,7 +170,7 @@ struct AuthenticationScreen: View {
185170 VStack { Divider ( ) }
186171 }
187172
188- if viewModel . flow == . login {
173+ if flow == . login {
189174 SignInWithAppleButton ( . signIn) { request in
190175 } onCompletion: { result in
191176 }
@@ -203,13 +188,13 @@ struct AuthenticationScreen: View {
203188 }
204189
205190 HStack {
206- Text ( viewModel . flow == . login ? " Don't have an account yet? " : " Already have an account? " )
191+ Text ( flow == . login ? " Don't have an account yet? " : " Already have an account? " )
207192 Button ( action: {
208193 withAnimation {
209- viewModel . switchFlow ( )
194+ switchFlow ( )
210195 }
211196 } ) {
212- Text ( viewModel . flow == . signUp ? " Log in " : " Sign up " )
197+ Text ( flow == . signUp ? " Log in " : " Sign up " )
213198 . fontWeight ( . semibold)
214199 . foregroundColor ( . blue)
215200 }
@@ -223,5 +208,5 @@ struct AuthenticationScreen: View {
223208
224209#Preview {
225210 AuthenticationScreen ( )
226- . environment ( AuthenticationViewModel ( ) )
211+ . environment ( AuthenticationService ( ) )
227212}
0 commit comments