-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProfileViewViewModel.swift
147 lines (132 loc) · 4.86 KB
/
ProfileViewViewModel.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//
// ProfileViewViewModel.swift
// group_app
//
// Created by Emily Markova on 7/31/23.
//
import FirebaseAuth
import FirebaseFirestore
import Foundation
class ProfileViewViewModel : ObservableObject{
private let userName: String
init(userName: String){
self.userName = userName
}
@Published var name: String? = nil
@Published var bio: String? = nil
func fetchUser() {
getUser(name: self.userName) { userId in
if userId.isEmpty {
print("Error: User not found")
return
}
let db = Firestore.firestore()
db.collection("users").document(userId).getDocument { snapshot, error in
guard let data = snapshot?.data(), error == nil else {
if let error = error {
print("Error fetching data:", error.localizedDescription)
}
return
}
DispatchQueue.main.async {
self.name = data["name"] as? String
self.bio = data["bio"] as? String
}
}
}
// getUser(name: self.userName) { userId in
// if userId.isEmpty {
// print("Error: User not found")
// return
// }
// let db = Firestore.firestore()
// db.collection("users").document(userId).getDocument{[weak self] snapshot, error in
// guard let data = snapshot?.data(), error == nil else {
// return
// }
// DispatchQueue.main.async {
// self.name = data["name"] as? String
// self.bio = data["bio"] as? String
//// self?.user = User (
//// id: data["id"] as? String ?? "",
//// bio: data["bio"] as? String ?? "",
//// name: data["name"] as? String ?? "",
//// email: data["email"] as? String ?? "",
//// joined: data["joined"] as? TimeInterval ?? 0,
//// following: data["following"] as? [String] ?? [],
//// followers: data["followers"] as? [String] ?? []
//// )
// }
//
// }
// }
}
func toggleIsDone(item: ToDoListItem){
var itemCopy = item
itemCopy.setDone(!item.isDone)
guard let uid = Auth.auth().currentUser?.uid else {
return
}
let db = Firestore.firestore()
db.collection("users")
.document(uid)
.collection("todos")
.document(itemCopy.id)
.setData(itemCopy.asDictionary())
}
func Follow(userName: String){
//get current user id
guard let uID = Auth.auth().currentUser?.uid else {
return
}
//create a model
let newItem = [userName]
//save a model as a sub collection of the current user
let db = Firestore.firestore()
db.collection("users").document(uID).setData([ "following": newItem ], merge: true)
getUser(name: userName) { userId in
// Use the userId here or perform any other tasks with it
db.collection("users").document(userId).setData([ "followers": newItem ], merge: true)
}
}
func getUser(name: String, completion: @escaping (String) -> Void){
let db = Firestore.firestore()
var userId = ""
db.collection("users").getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
for document in querySnapshot!.documents {
if let username = document.data()["name"] as? String {
if username == name {
userId = document.documentID
break
}
}
}
}
completion(userId)
}
}
func getFollowers(userId: String) -> [String] {
let db = Firestore.firestore()
var followersList = [String]() // Initialize an empty array to hold the followers
db.collection("users").document(userId).getDocument { (document, error) in
if let document = document, document.exists {
if let followers = document.data()?["followers"] as? [String] {
followersList = followers
}
} else {
print("Document does not exist")
}
}
return followersList
}
func logOut(){
do{
try Auth.auth().signOut()
} catch {
print(error)
}
}
}