diff --git a/nirvana-ios/Views/PhoneVerification/PhoneVerificationView.swift b/nirvana-ios/Views/PhoneVerification/PhoneVerificationView.swift index f1d5448..fd225aa 100644 --- a/nirvana-ios/Views/PhoneVerification/PhoneVerificationView.swift +++ b/nirvana-ios/Views/PhoneVerification/PhoneVerificationView.swift @@ -24,6 +24,14 @@ struct PhoneVerificationView: View { @State var isLoading = false + let formatter: NumberFormatter = { + let formatter = NumberFormatter() + formatter.numberStyle = .decimal + return formatter + }() + + @FocusState private var focusedInputField: Bool + // TODO: format the input so that it shows the parentheses and stuff (949)923-0445 var body: some View { ZStack(alignment: .topLeading) { @@ -44,11 +52,11 @@ struct PhoneVerificationView: View { self.showToast.toggle() } - TextField("(650) 555 - 1234", text: $phoneNumber) + TextField("(650) 555 - 1234", text: self.$phoneNumber) .padding() .background(.ultraThinMaterial) .clipShape(Capsule()) - .keyboardType(.decimalPad) + .keyboardType(.phonePad) .shadow(color: Color.black.opacity(0.3), radius: 20, x: 0, y: 20) .font(.subheadline) .onReceive(Just(self.phoneNumber)) {newValue in @@ -59,6 +67,7 @@ struct PhoneVerificationView: View { self.phoneNumber = filtered } } + .focused(self.$focusedInputField) Spacer() } @@ -69,40 +78,39 @@ struct PhoneVerificationView: View { Button { let concatPhoneNumber = "+"+self.ccode+self.phoneNumber + //disable focused textfield...important: draw the focus field out first and then the loading screen + self.focusedInputField.toggle() + //show loading screen self.isLoading.toggle() - DispatchQueue.main.async { - print("sending sms and/or redirecting") - - // do auth stuff from firebase - PhoneAuthProvider.provider() - .verifyPhoneNumber(concatPhoneNumber, uiDelegate: nil) { verificationID, error in - // got a response...done loading... either error to show or next page - self.isLoading.toggle() - - // problem verifying number showing alert...maybe fake number or something from user - if error != nil { - self.toastText = "⚠️ Please try again." - self.toastSubMessage = "There was an issue validating your phone number" - self.showToast.toggle() - - print((error?.localizedDescription)!) - - return - } - - // TODO: Sign in using the verificationID and the code sent to the user - // if there is no user with the phone number, then create one - - UserDefaults.standard.set(verificationID, forKey: "authVerificationID") - - // then async send to next page - // TODO: coming back after function end and then having 1 second before hitting this...need to use async and combine and all of that to make sure that the main thread is not affected and it's all smooth - self.navigationStack.push(PhoneVerificationCodeView()) // verify code page - } - } + print("sending sms and/or redirecting") + // TODO: do all of this in view model + // do auth stuff from firebase + PhoneAuthProvider.provider() + .verifyPhoneNumber(concatPhoneNumber, uiDelegate: nil) { verificationID, error in + print("firebase auth done, now running my callback") + + // got a response...done loading... either error to show or next page + self.isLoading.toggle() + + // problem verifying number showing alert...maybe fake number or something from user + if error != nil { + self.toastText = "⚠️ Please try again." + self.toastSubMessage = "There was an issue validating your phone number" + self.showToast.toggle() + + print((error?.localizedDescription)!) + + return + } + + // use this in the next screen to verify with the code provided + UserDefaults.standard.set(verificationID, forKey: "authVerificationID") + + self.navigationStack.push(PhoneVerificationCodeView()) // verify code page + } } label: { VStack { Text("Send Verification") @@ -149,8 +157,6 @@ struct PhoneVerificationView: View { if self.isLoading { SplashView() } - - SplashView() } .alert(self.toastText, isPresented: self.$showToast) { Button("OK", role: ButtonRole.cancel) { } @@ -179,6 +185,14 @@ struct PhoneVerificationCodeView: View { @State var isLoading = false + @FocusState private var focusedInputField: Bool + + let formatter: NumberFormatter = { + let formatter = NumberFormatter() + formatter.numberStyle = .decimal + return formatter + }() + var body: some View { ZStack(alignment: .topLeading) { Color.clear @@ -189,26 +203,26 @@ struct PhoneVerificationCodeView: View { .padding() .background(.ultraThinMaterial) .clipShape(Capsule()) - .keyboardType(.decimalPad) .shadow(color: Color.black.opacity(0.3), radius: 20, x: 0, y: 20) - .font(.largeTitle) + .font(.title) .multilineTextAlignment(.center) .padding(.vertical) -// .onReceive(Just(self.$verificationCode)) { newValue in -// let filtered = newValue.filter { "0123456789".contains($0) } -// -// if filtered != newValue { -// self.verificationCode = filtered -// } -// } + .keyboardType(.phonePad) + .focused(self.$focusedInputField) + if self.verificationCode.count == 6 { Button { print("started verification process of code") + + //disable focused textfield + self.focusedInputField.toggle() + // show loading screen self.isLoading.toggle() + // TODO: do all of this in view model authstore // get it from the previous screen but from storage let verificationID = UserDefaults.standard.string(forKey: "authVerificationID") @@ -217,44 +231,42 @@ struct PhoneVerificationCodeView: View { self.showToast.toggle() } - // TESTING: let it do it's thing but don't ruin the ui main thread - DispatchQueue.main.async { - // if we got a value, then continue with verifying their code - let credential = PhoneAuthProvider.provider().credential( - withVerificationID: verificationID!, - verificationCode: self.verificationCode - ) + // if we got a value, then continue with verifying their code + let credential = PhoneAuthProvider.provider().credential( + withVerificationID: verificationID!, + verificationCode: self.verificationCode + ) + + // firebase will now verify the credential + Auth.auth().signIn(with: credential) { (res, err) in + // done with the response here, turn off loading screen + self.isLoading.toggle() - // firebase will now verify the credential - Auth.auth().signIn(with: credential) { (res, err) in - // done with the response here, turn off loading screen - self.isLoading.toggle() + if err != nil { + self.toastText = "⚠️ Error with Verification" + self.toastSubMessage = "Code is invalid. Please try again or re-enter your phone number in the previous page." + self.showToast.toggle() - if err != nil { - self.toastText = "⚠️ Error with Verification" - self.toastSubMessage = "Code is invalid. Please try again or re-enter your phone number in the previous page." - self.showToast.toggle() - - print((err?.localizedDescription)!) - return - } - - //setting in key storage - UserDefaults.standard.set(true, forKey: "authVerificationID") - - //TODO: is the auth listener going to find that this person signed in? idk test it - - // firebase either created a new user or is giving back an existing user's id - let userId = res?.user.uid - let userPhoneNumber = res?.user.phoneNumber - print("user id that was authenticated is: \(userId)") - print("user id that was authenticated is: \(userPhoneNumber)") - - - // then sending to next page - self.navigationStack.push(PhoneVerificationCodeView()) // verify code page + print((err?.localizedDescription)!) + return } + + //setting in key storage + UserDefaults.standard.set(true, forKey: "authVerificationID") + + //TODO: is the auth listener going to find that this person signed in? idk test it + + // firebase either created a new user or is giving back an existing user's id + let userId = res?.user.uid + let userPhoneNumber = res?.user.phoneNumber + print("user id that was authenticated is: \(userId)") + print("user id that was authenticated is: \(userPhoneNumber)") + + + // then sending to next page + self.navigationStack.push(PhoneVerificationCodeView()) // verify code page } + } label: { Text("Verify") .bold() diff --git a/nirvana-ios/Views/PhoneVerification/PhoneVerificationViewModel.swift b/nirvana-ios/Views/PhoneVerification/PhoneVerificationViewModel.swift index 89537a2..0be9619 100644 --- a/nirvana-ios/Views/PhoneVerification/PhoneVerificationViewModel.swift +++ b/nirvana-ios/Views/PhoneVerification/PhoneVerificationViewModel.swift @@ -9,6 +9,8 @@ import Foundation final class PhoneVerificationViewModel : ObservableObject { + + } extension String {