-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContentViewViewModel.swift
47 lines (42 loc) · 1.5 KB
/
ContentViewViewModel.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//
// ContentViewViewModel.swift
// group_app
//
// Created by Emily Markova on 7/31/23.
//
import FirebaseFirestore
import FirebaseAuth
import Foundation
class ContentViewViewModel: ObservableObject {
@Published var isSignedIn: Bool = false
@Published var currentUserId: String = ""
@Published var currentUserName: String = ""
//optional because it's originally nil
private var handler: AuthStateDidChangeListenerHandle?
init(){
self.fetchUserName()
//whenever a user is signed in or out, the listener will be triggered and change the currentUserId so we can updated the view
//self optional helps prevent memory leaks
let handler = Auth.auth().addStateDidChangeListener{[weak self] _, user in
DispatchQueue.main.async {
self?.currentUserId = user?.uid ?? ""
}
}
}
func fetchUserName() {
guard let uID = Auth.auth().currentUser?.uid else {
return
}
let db = Firestore.firestore()
db.collection("users").document(uID).getDocument { snapshot, error in
if let data = snapshot?.data(), error == nil {
if let userName = data["name"] as? String {
DispatchQueue.main.async {
self.currentUserName = userName
self.isSignedIn = true
}
}
}
}
}
}