having real time user listened to in authlistener
This commit is contained in:
@@ -18,6 +18,25 @@ class FirestoreService {
|
||||
|
||||
private var db = Firestore.firestore()
|
||||
|
||||
func getUserRealtime(userId: String, completion: @escaping((_ user: User?) -> ())) {
|
||||
db.collection(Collection.users.rawValue).document(userId).addSnapshotListener { documentSnapshot, error in
|
||||
guard let document = documentSnapshot else {
|
||||
print("Error fetching getting user realtime listener: \(error!)")
|
||||
completion(nil)
|
||||
return
|
||||
}
|
||||
|
||||
guard let updatedOrNewUser = try? document.data(as: User.self)
|
||||
else {
|
||||
completion(nil)
|
||||
print("Document data was empty. tried fetching updated user document in firestore service")
|
||||
return
|
||||
}
|
||||
// up to date user
|
||||
completion(updatedOrNewUser)
|
||||
}
|
||||
}
|
||||
|
||||
func getUser(userId: String, completion: @escaping((_ user: User?) -> ())) {
|
||||
let docRef = db.collection(Collection.users.rawValue).document(userId)
|
||||
|
||||
|
||||
@@ -116,7 +116,6 @@ final class AuthSessionStore: ObservableObject, SessionStore {
|
||||
|
||||
func setupAuthListen() {
|
||||
// monitor authentication changes using firebase
|
||||
print("let's see what user looks like in authsession at at the start: \(self.user)")
|
||||
self.handler = Auth.auth().addStateDidChangeListener { [weak self] res, user in
|
||||
print("auth listener activated")
|
||||
|
||||
@@ -130,13 +129,11 @@ final class AuthSessionStore: ObservableObject, SessionStore {
|
||||
print("auth listener: Got user: \(uid)")
|
||||
print("auth listener: phone number: \(user?.phoneNumber)")
|
||||
|
||||
// get user from firestore and store in environment
|
||||
self.firestoreService.getUser(userId: uid) {[weak self] firestoreUser in
|
||||
// if we get a user back, then set this to our authsessionstore for our views
|
||||
print("user from firestore in auth listener: \(firestoreUser)")
|
||||
if firestoreUser != nil {
|
||||
self?.user = firestoreUser
|
||||
print("lets see if authsessionstore user is being set: \(self?.user)")
|
||||
// when we have a user get signed in, we can activate the listener for fetching the user's data to keep our auth session store always up to date for all of the ui
|
||||
self.firestoreService.getUserRealtime(userId: uid) {[weak self] realtimeUpdatedUser in
|
||||
if realtimeUpdatedUser != nil {
|
||||
print("up to date user: \(realtimeUpdatedUser)")
|
||||
self?.user = realtimeUpdatedUser
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
||||
@@ -90,7 +90,7 @@ struct AddBasicInfoView: View {
|
||||
.font(.subheadline)
|
||||
.multilineTextAlignment(.center)
|
||||
|
||||
Text("What does your friends call you?")
|
||||
Text("What do your friends call you?")
|
||||
.font(.footnote)
|
||||
.foregroundColor(Color.black.opacity(0.7))
|
||||
|
||||
@@ -129,6 +129,13 @@ struct AddBasicInfoView: View {
|
||||
}
|
||||
.frame(maxHeight: .infinity)
|
||||
}
|
||||
.onAppear {
|
||||
// get current user nickname and avatar if they have one
|
||||
self.nickname = self.authSessionStore.user?.nickname ?? ""
|
||||
if self.authSessionStore.user?.avatar != nil { // if user has previously selected an avatar
|
||||
self.selectedAvatarIndex = Avatars.avatarSystemNames.firstIndex(of: (self.authSessionStore.user?.avatar)!) ?? 0 // 0 in case the system names doesn't have it anymore
|
||||
}
|
||||
}
|
||||
//TODO: hook up the alert with the view model
|
||||
// .alert(isPresented: Binding<Bool>(
|
||||
// get: { self.addInfoViewModel.state == ProfileRegistrationState.failed},
|
||||
|
||||
@@ -41,6 +41,8 @@ class AddBasicInfoViewModel : ObservableObject {
|
||||
|
||||
// save in firestore
|
||||
self.firestoreService.updateUser(user: currUser) {res in
|
||||
print(res)
|
||||
|
||||
switch res {
|
||||
case .success:
|
||||
self.state = .successful
|
||||
|
||||
@@ -344,17 +344,21 @@ struct InnerCircleView: View {
|
||||
.font(.title2)
|
||||
|
||||
Menu {
|
||||
Button("Edit Profile") {
|
||||
self.navigationStack.push(AddBasicInfoView())
|
||||
}
|
||||
|
||||
Button("Friends") {
|
||||
print("manage friends page")
|
||||
}
|
||||
|
||||
Button("Log out") {
|
||||
print("log out button clicked")
|
||||
|
||||
// have this send it
|
||||
self.authSessionStore.logOut()
|
||||
|
||||
// once logged out, then the listener will listen and change the page but we will go there first
|
||||
self.navigationStack.push(ContentView())
|
||||
}
|
||||
Button("Friends") {
|
||||
print("manage friends page")
|
||||
// once logged out, then the listener will listen and router view will take care of us thereafter
|
||||
}
|
||||
} label: {
|
||||
Image("Artboards_Diversity_Avatars_by_Netguru-51")
|
||||
|
||||
@@ -35,19 +35,24 @@ struct RouterView: View {
|
||||
if self.authSessionStore.user != nil {
|
||||
if self.authSessionStore.user?.nickname == nil && self.authSessionStore.user?.avatar == nil {
|
||||
OnboardingTrioView()
|
||||
.transition(.slide)
|
||||
} else {// user is existing user/up and running user -> signs in is pushed nicely right to hub
|
||||
InnerCircleView()
|
||||
.transition(.slide)
|
||||
}
|
||||
} else {
|
||||
OnboardingTrioView()
|
||||
.transition(.slide)
|
||||
}
|
||||
|
||||
} else if self.authSessionStore.sessionState == SessionState.isLoggedOut {
|
||||
// go to welcome page at which point they can go and follow the programmatic
|
||||
// line to the sigin and so on and so forth
|
||||
WelcomeView()
|
||||
.transition(.slide)
|
||||
} else {
|
||||
SplashView()
|
||||
.transition(.slide)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user