// // LoginView.swift // WekoutThotViewer // // Created by Trey Tartt on 6/18/24. // import SwiftUI struct LoginView: View { @State var email: String = "" @State var password: String = "" @Environment(\.dismiss) var dismiss let completion: (() -> Void) @State var isLoggingIn: Bool = false @State var errorTitle = "" @State var errorMessage = "" @State var hasError: Bool = false var body: some View { VStack { TextField("Email", text: $email) .textContentType(.username) .textInputAutocapitalization(.never) .autocorrectionDisabled(true) .keyboardType(.emailAddress) .padding() .background(Color.white) .cornerRadius(8) .padding(.horizontal) .padding(.top, 25) .foregroundStyle(.black) .accessibilityLabel("Email") .submitLabel(.next) SecureField("Password", text: $password) .textContentType(.password) .padding() .background(Color.white) .cornerRadius(8) .padding(.horizontal) .textInputAutocapitalization(.never) .autocorrectionDisabled(true) .foregroundStyle(.black) .accessibilityLabel("Password") .submitLabel(.go) if isLoggingIn { ProgressView("Logging In") .padding() .foregroundColor(.white) .progressViewStyle(CircularProgressViewStyle(tint: .white)) .scaleEffect(1.5, anchor: .center) } else { Button("Login", action: { login() }) .frame(maxWidth: .infinity, alignment: .center) .frame(height: 44) .foregroundColor(.blue) .background(.yellow) .cornerRadius(16) .padding() .frame(maxWidth: .infinity) .disabled(password.isEmpty || email.isEmpty) .accessibilityLabel("Log in") .accessibilityHint("Logs in using the entered email and password") } Spacer() } .padding() .background( Image("icon") .resizable() .edgesIgnoringSafeArea(.all) .scaledToFill() .accessibilityHidden(true) ) .alert(errorTitle, isPresented: $hasError, actions: { }, message: { Text(errorMessage) }) } func login() { let postData = [ "email": email, "password": password ] isLoggingIn = true UserStore.shared.login(postData: postData, completion: { success in isLoggingIn = false if success { completion() dismiss() } else { errorTitle = "error logging in" errorMessage = "invalid credentials" hasError = true } }) } } struct LoginView_Previews: PreviewProvider { static var previews: some View { LoginView(completion: { }) } }