From 4dc7b422e9c42c9b606e80c689238f8cff0df956 Mon Sep 17 00:00:00 2001 From: talksik Date: Sun, 19 Dec 2021 17:38:33 -0800 Subject: [PATCH] fixing updating current user log in: but not completely done --- .../Repositories/FirestoreService.swift | 12 ++++---- nirvana-ios/Services/AuthSessionStore.swift | 14 ++++----- .../Views/IntroView/OnboardingTrioView.swift | 2 +- .../PhoneVerificationViewModel.swift | 29 +++++++++++-------- 4 files changed, 30 insertions(+), 27 deletions(-) diff --git a/nirvana-ios/Repositories/FirestoreService.swift b/nirvana-ios/Repositories/FirestoreService.swift index d703651..a64244f 100644 --- a/nirvana-ios/Repositories/FirestoreService.swift +++ b/nirvana-ios/Repositories/FirestoreService.swift @@ -18,20 +18,18 @@ class FirestoreService { private var db = Firestore.firestore() - func getUser(userId: String) -> User? { + func getUser(userId: String, completion: @escaping((_ user: User?) -> ())) { let docRef = db.collection(Collection.users.rawValue).document(userId) - var user:User? = nil - docRef.getDocument { (document, error) in if let document = document, document.exists { - user = try? document.data(as: User.self) + let returnedUser = try? document.data(as: User.self) + completion(returnedUser) } else { - user = nil + print("user doesn't exist") + completion(nil) } } - - return user } func createUser(user: User) { diff --git a/nirvana-ios/Services/AuthSessionStore.swift b/nirvana-ios/Services/AuthSessionStore.swift index 59d6069..3c68018 100644 --- a/nirvana-ios/Services/AuthSessionStore.swift +++ b/nirvana-ios/Services/AuthSessionStore.swift @@ -143,13 +143,13 @@ final class AuthSessionStore: ObservableObject, SessionStore { } private func getAndSetEnvironmentUserDetails(userId: String) { - let firestoreUser: User? = self.firestoreService.getUser(userId: userId) - - // if we get a user back, then set this to our instance to allow UI to get published with all user details - if firestoreUser != nil { - // TODO: prolly useless since we never set up a background thread for the rest of this code before - DispatchQueue.main.async { - self.user = firestoreUser + self.firestoreService.getUser(userId: userId) {firestoreUser in + // if we get a user back, then set this to our instance to allow UI to get published with all user details + if firestoreUser != nil { + // TODO: prolly useless since we never set up a background thread for the rest of this code before + DispatchQueue.main.async { + self.user = firestoreUser + } } } } diff --git a/nirvana-ios/Views/IntroView/OnboardingTrioView.swift b/nirvana-ios/Views/IntroView/OnboardingTrioView.swift index 3bc21f9..3e123ff 100644 --- a/nirvana-ios/Views/IntroView/OnboardingTrioView.swift +++ b/nirvana-ios/Views/IntroView/OnboardingTrioView.swift @@ -14,7 +14,7 @@ struct OnboardingTrioView: View { var body: some View { VStack { TabView { - OnboardingTemplateView(imgName: "undraw_through_the_park_lxnl", mainLeadingActText: "Be", mainHighlightedActText: "yourself", mainTrailingActText: "again.", subActText: "No more consuming endless feeds, media, fomo, anxiety...focus on you and be more present.") + OnboardingTemplateView(imgName: "undraw_through_the_park_lxnl", mainLeadingActText: "Be", mainHighlightedActText: "yourself", mainTrailingActText: "again.", subActText: "No more endless feeds, media, fomo, anxiety...focus on you and be more present.") OnboardingTemplateView(imgName: "undraw_connection_b-38-q", mainLeadingActText: "Be picky about your ", mainHighlightedActText: "inner circle.", mainTrailingActText: "", subActText: "You are who you hang out with. We kept things minimal because less is more...and we care.") diff --git a/nirvana-ios/Views/PhoneVerification/PhoneVerificationViewModel.swift b/nirvana-ios/Views/PhoneVerification/PhoneVerificationViewModel.swift index 3c94681..0ae1800 100644 --- a/nirvana-ios/Views/PhoneVerification/PhoneVerificationViewModel.swift +++ b/nirvana-ios/Views/PhoneVerification/PhoneVerificationViewModel.swift @@ -24,19 +24,24 @@ final class PhoneVerificationViewModel : ObservableObject { // TODO: set all in a transaction or batch write // get user - 1 result set cost - var user = firestoreService.getUser(userId: userId) - print("user that was received from get: \(user?.id)") - - if user == nil {// if empty, create user - 1 result set cost..prolly higher cost - print("creating new user with id: \(userId)") - let newUser = User(id: userId, phoneNumber: phoneNumber) - print("verify the id stayed the same: \(newUser.id)") - firestoreService.createUser(user: newUser) - } else { // if not, change last logged in value - // assign to nil so that server can fill it in - user!.lastLoggedInTimestamp = nil + self.firestoreService.getUser(userId: userId) {[weak self] resultingUser in + print("user that was received from get: \(resultingUser?.id)") - let _ = firestoreService.updateUser(user: user!) + + if resultingUser == nil {// if empty, create user - 1 result set cost..prolly higher cost + print("creating new user with id: \(userId)") + let newUser = User(id: userId, phoneNumber: phoneNumber) + print("verify the id stayed the same: \(newUser.id)") + self?.firestoreService.createUser(user: newUser) + } else { // if not, change last logged in value + var user = resultingUser + + // assign to nil so that server can fill it in + user!.lastLoggedInTimestamp = nil + + let res = self?.firestoreService.updateUser(user: user!) + print(res) + } } } }