fixing some ui things
This commit is contained in:
@@ -24,6 +24,14 @@ struct PhoneVerificationView: View {
|
|||||||
|
|
||||||
@State var isLoading = false
|
@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
|
// TODO: format the input so that it shows the parentheses and stuff (949)923-0445
|
||||||
var body: some View {
|
var body: some View {
|
||||||
ZStack(alignment: .topLeading) {
|
ZStack(alignment: .topLeading) {
|
||||||
@@ -44,11 +52,11 @@ struct PhoneVerificationView: View {
|
|||||||
self.showToast.toggle()
|
self.showToast.toggle()
|
||||||
}
|
}
|
||||||
|
|
||||||
TextField("(650) 555 - 1234", text: $phoneNumber)
|
TextField("(650) 555 - 1234", text: self.$phoneNumber)
|
||||||
.padding()
|
.padding()
|
||||||
.background(.ultraThinMaterial)
|
.background(.ultraThinMaterial)
|
||||||
.clipShape(Capsule())
|
.clipShape(Capsule())
|
||||||
.keyboardType(.decimalPad)
|
.keyboardType(.phonePad)
|
||||||
.shadow(color: Color.black.opacity(0.3), radius: 20, x: 0, y: 20)
|
.shadow(color: Color.black.opacity(0.3), radius: 20, x: 0, y: 20)
|
||||||
.font(.subheadline)
|
.font(.subheadline)
|
||||||
.onReceive(Just(self.phoneNumber)) {newValue in
|
.onReceive(Just(self.phoneNumber)) {newValue in
|
||||||
@@ -59,6 +67,7 @@ struct PhoneVerificationView: View {
|
|||||||
self.phoneNumber = filtered
|
self.phoneNumber = filtered
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
.focused(self.$focusedInputField)
|
||||||
|
|
||||||
Spacer()
|
Spacer()
|
||||||
}
|
}
|
||||||
@@ -69,40 +78,39 @@ struct PhoneVerificationView: View {
|
|||||||
Button {
|
Button {
|
||||||
let concatPhoneNumber = "+"+self.ccode+self.phoneNumber
|
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
|
//show loading screen
|
||||||
self.isLoading.toggle()
|
self.isLoading.toggle()
|
||||||
|
|
||||||
DispatchQueue.main.async {
|
print("sending sms and/or redirecting")
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// 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: {
|
} label: {
|
||||||
VStack {
|
VStack {
|
||||||
Text("Send Verification")
|
Text("Send Verification")
|
||||||
@@ -149,8 +157,6 @@ struct PhoneVerificationView: View {
|
|||||||
if self.isLoading {
|
if self.isLoading {
|
||||||
SplashView()
|
SplashView()
|
||||||
}
|
}
|
||||||
|
|
||||||
SplashView()
|
|
||||||
}
|
}
|
||||||
.alert(self.toastText, isPresented: self.$showToast) {
|
.alert(self.toastText, isPresented: self.$showToast) {
|
||||||
Button("OK", role: ButtonRole.cancel) { }
|
Button("OK", role: ButtonRole.cancel) { }
|
||||||
@@ -179,6 +185,14 @@ struct PhoneVerificationCodeView: View {
|
|||||||
|
|
||||||
@State var isLoading = false
|
@State var isLoading = false
|
||||||
|
|
||||||
|
@FocusState private var focusedInputField: Bool
|
||||||
|
|
||||||
|
let formatter: NumberFormatter = {
|
||||||
|
let formatter = NumberFormatter()
|
||||||
|
formatter.numberStyle = .decimal
|
||||||
|
return formatter
|
||||||
|
}()
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
ZStack(alignment: .topLeading) {
|
ZStack(alignment: .topLeading) {
|
||||||
Color.clear
|
Color.clear
|
||||||
@@ -189,26 +203,26 @@ struct PhoneVerificationCodeView: View {
|
|||||||
.padding()
|
.padding()
|
||||||
.background(.ultraThinMaterial)
|
.background(.ultraThinMaterial)
|
||||||
.clipShape(Capsule())
|
.clipShape(Capsule())
|
||||||
.keyboardType(.decimalPad)
|
|
||||||
.shadow(color: Color.black.opacity(0.3), radius: 20, x: 0, y: 20)
|
.shadow(color: Color.black.opacity(0.3), radius: 20, x: 0, y: 20)
|
||||||
.font(.largeTitle)
|
.font(.title)
|
||||||
.multilineTextAlignment(.center)
|
.multilineTextAlignment(.center)
|
||||||
.padding(.vertical)
|
.padding(.vertical)
|
||||||
// .onReceive(Just(self.$verificationCode)) { newValue in
|
.keyboardType(.phonePad)
|
||||||
// let filtered = newValue.filter { "0123456789".contains($0) }
|
.focused(self.$focusedInputField)
|
||||||
//
|
|
||||||
// if filtered != newValue {
|
|
||||||
// self.verificationCode = filtered
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
if self.verificationCode.count == 6 {
|
if self.verificationCode.count == 6 {
|
||||||
Button {
|
Button {
|
||||||
print("started verification process of code")
|
print("started verification process of code")
|
||||||
|
|
||||||
|
//disable focused textfield
|
||||||
|
self.focusedInputField.toggle()
|
||||||
|
|
||||||
// show loading screen
|
// show loading screen
|
||||||
self.isLoading.toggle()
|
self.isLoading.toggle()
|
||||||
|
|
||||||
|
|
||||||
|
// TODO: do all of this in view model authstore
|
||||||
// get it from the previous screen but from storage
|
// get it from the previous screen but from storage
|
||||||
let verificationID = UserDefaults.standard.string(forKey: "authVerificationID")
|
let verificationID = UserDefaults.standard.string(forKey: "authVerificationID")
|
||||||
|
|
||||||
@@ -217,44 +231,42 @@ struct PhoneVerificationCodeView: View {
|
|||||||
self.showToast.toggle()
|
self.showToast.toggle()
|
||||||
}
|
}
|
||||||
|
|
||||||
// TESTING: let it do it's thing but don't ruin the ui main thread
|
// if we got a value, then continue with verifying their code
|
||||||
DispatchQueue.main.async {
|
let credential = PhoneAuthProvider.provider().credential(
|
||||||
// if we got a value, then continue with verifying their code
|
withVerificationID: verificationID!,
|
||||||
let credential = PhoneAuthProvider.provider().credential(
|
verificationCode: self.verificationCode
|
||||||
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
|
if err != nil {
|
||||||
Auth.auth().signIn(with: credential) { (res, err) in
|
self.toastText = "⚠️ Error with Verification"
|
||||||
// done with the response here, turn off loading screen
|
self.toastSubMessage = "Code is invalid. Please try again or re-enter your phone number in the previous page."
|
||||||
self.isLoading.toggle()
|
self.showToast.toggle()
|
||||||
|
|
||||||
if err != nil {
|
print((err?.localizedDescription)!)
|
||||||
self.toastText = "⚠️ Error with Verification"
|
return
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//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: {
|
} label: {
|
||||||
Text("Verify")
|
Text("Verify")
|
||||||
.bold()
|
.bold()
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ import Foundation
|
|||||||
|
|
||||||
final class PhoneVerificationViewModel : ObservableObject {
|
final class PhoneVerificationViewModel : ObservableObject {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
extension String {
|
extension String {
|
||||||
|
|||||||
Reference in New Issue
Block a user