// // LoginView.swift // JobTrackerPro // // Created by Jake Steirer on 12/30/23. // import SwiftUI struct LoginView: View { @ObservedObject var viewModel: JobApplicationViewModel @State private var email: String = "" @State private var password: String = "" @State private var storedEmail: String = "" // Separate variable to hold the text after editing ends @State private var storedPassword: String = "" // Separate variable for password @State private var isShowingAlert = false @State private var alertMessage = "" @FocusState private var isInputActive: Bool // For managing keyboard focus var body: some View { NavigationView { VStack { TextField("Email", text: $email) .autocapitalization(.none) .keyboardType(.emailAddress) .disableAutocorrection(true) .padding() .border(Color(UIColor.separator)) .focused($isInputActive) // Binding focus state .onChange(of: email) { [email] in storedEmail = email } .onSubmit { storedEmail = email // Store the email when submit the field } SecureField("Password", text: $password) .padding() .border(Color(UIColor.separator)) .focused($isInputActive) // Binding focus state .onChange(of: password) { [password] in storedPassword = password } .onSubmit { storedPassword = password // Store the password when submit the field } Button("Log In") { isInputActive = false // Dismiss the keyboard before attempting to log in // Use storedEmail and storedPassword here for logging in viewModel.logIn(email: storedEmail, password: storedPassword) { success, message in if !success { alertMessage = message isShowingAlert = true } } } .padding() .alert(isPresented: $isShowingAlert) { Alert(title: Text("Login Failed"), message: Text(alertMessage), dismissButton: .default(Text("OK"))) } } .navigationBarTitle("Log In", displayMode: .large) .padding() } } }