-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAddContactViewModel.txt
194 lines (150 loc) · 7.53 KB
/
AddContactViewModel.txt
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
//
// AddContactViewModel.swift
// Safeye
//
// Created by FUKA on 1.4.2022.
// Edit by gintare on 10.4.2022.
import SwiftUI
class AddContactViewModel: ObservableObject {
static let shared = AddContactViewModel() ; private init() {}
let profileService = ProfileService.shared
let profileService2 = ProfileService.shared
@ObservedObject var ProfileVM = ProfileViewModel.shared
@Published var connectionDetails: ContactConnectionModel?
@Published var profileFound = false
@Published var trustedContactDetails: TrustedContactModel?
@Published var connectionsPending = false
@Published var pendingRequest: String? = nil
@Published var pendingArray = []
@Published var foundTrustedContacts = false
@Published var trustedContactList: [String] = [String]()
@Published var trustedContacts: [ProfileModel] = [ProfileModel]()
private var targetId: String = ""
/*func findProfile(searchCode: String?) {
//TODO: user shouldn't be able to find themselves or add themselves!!!
// Fetch profile data
profileService.collection("profiles").whereField("connectionCode", isEqualTo: searchCode!).getDocuments() { snapshot, error in
if let error = error {
print("Error fetching profile: \(error)")
} else {
if snapshot!.count < 1 {
self.profileFound = false
print("Profile not found")
return
}
for document in snapshot!.documents {
self.profileFound = true
print("Profile found with ID: \(document.documentID)")
self.targetId = document["userId"] as! String
let profileId = document.documentID
let fullName = document["fullName"]
// Update @Published object in main thread with Trusted Contact details
DispatchQueue.main.async {
self.trustedContactDetails = TrustedContactModel(id: profileId, userId: self.targetId , fullName: fullName as! String)
}
}
}
}
}*/ // end of findProfile()
// fetch all pending requests for logged in user
/*func getPendingConnectionRequests() {
self.pendingArray = []
profileService.collection("connections").whereField("status", isEqualTo: false).whereField("targetId", isEqualTo: AuthenticationService.getInstance.currentUser!.uid).getDocuments() { snapshot, error in
if let error = error {
print("Error fetching connection requests \(error)")
} else {
if snapshot!.count < 1 {
self.connectionsPending = false
print("There are no pending connection requests")
return
}
}
for document in snapshot!.documents {
self.connectionsPending = true
print("There is a pending request with Id: \(document.documentID)")
self.pendingRequest = document.documentID
let tcId = document["ownerId"]
self.pendingArray.append(tcId as! String)
print(self.pendingArray)
}
}
}*/ // end of getPendingConnectionRequests()
// confirm pending requests
/*func confirmConnectionRequest() {
if pendingRequest == nil {
print("There is nothing to confirm")
} else {
profileService.collection("connections").document(pendingRequest!).setData(["status": true], merge: true) { error in
if error == nil {
print("Request confirmed")
self.pendingRequest = nil
self.getPendingConnectionRequests()
} else {
print("Error confirming connection request")
}
}
}
}*/
// TODO: unfinished
// fetch all user's trusted contacts
/*func fetchAllUsersContacts() {
self.trustedContactList = []
let userId = AuthenticationService.getInstance.currentUser?.uid
// Round1: checks for those connections that were sent to current user and they accepted
profileService.collection("connections").whereField("targetId", isEqualTo: userId!).whereField("status", isEqualTo: true).getDocuments() { snapshot, error in
if let error = error {
print("Error fetching contacts: \(error)")
} else {
if snapshot!.count < 1 {
self.foundTrustedContacts = false
print("No trusted contacts yet")
return
}
print("Yes: \(snapshot!.count)")
for document in snapshot!.documents {
print("Snapshot: \(document["ownerId"] ?? "")")
self.foundTrustedContacts = true
let tcId = document["ownerId"]
self.trustedContactList.append(tcId! as! String)
print("Trusted contact list: \(self.trustedContactList)")
// self.trustedContacts = self.profileService2.getProfiles(for: tcId as! String)
/* DispatchQueue.main.async {
self.trustedContacts = self.profileService2.getProfiles(for: tcId as! String)
} */
}
}
print("before calling fetchProfiles")
self.profileService2.fetchProfiles(for: self.trustedContactList)
DispatchQueue.main.async {
self.trustedContacts = self.profileService2.getProfiles()!
}
// let x = self.profileService2.getProfiles()
// print("Finlay? : \(x!.count)")
}
// Round 2: checks for those connections that the user sent and other users accepted
profileService.collection("connections").whereField("ownerId", isEqualTo: userId!).whereField("status", isEqualTo: true).getDocuments() { snapshot, error in
if let error = error {
print("Error fetching contacts: \(error)")
} else {
if snapshot!.count < 1 {
self.foundTrustedContacts = false
print("No trusted contacts yet")
return
}
print("Yes: \(snapshot!.count)")
for document in snapshot!.documents {
print("Snapshot: \(document["targetId"] ?? "")")
self.foundTrustedContacts = true
let tcId = document["targetId"]
self.trustedContactList.append(tcId! as! String)
print("Trusted contact list: \(self.trustedContactList)")
}
}
print("before calling fetchProfiles")
self.profileService2.fetchProfiles(for: self.trustedContactList)
DispatchQueue.main.async {
self.trustedContacts = self.profileService2.getProfiles()!
}
}
}*/
}