-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSingleton.swift
345 lines (314 loc) · 16.3 KB
/
Singleton.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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
//
// SellerProfileSingleton.swift
// Inaldo&Tony
//
// Created by Antonio Sirica on 19/12/2017.
// Copyright © 2017 Antonio Sirica. All rights reserved.
//
import Foundation
import UIKit
import Firebase
import FirebaseAuth
class FriendSystem {
static let system = FriendSystem()
var timeCoins = 0
// MARK: - Firebase references
let ref = Database.database().reference()
let userRef = Database.database().reference().child("users")
/** The Firebase reference to the current user tree */
var currentUserRef: DatabaseReference {
let id = Auth.auth().currentUser!.uid
return userRef.child("\(id)")
}
/** The Firebase reference to the current user's friend tree */
var currentUserFriendsRef: DatabaseReference {
return currentUserRef.child("friends")
}
/** The Firebase reference to the current user's friend request tree */
var currentUserRequestRef: DatabaseReference {
return currentUserRef.child("requests")
}
/** The current user's id */
var uid: String {
let id = Auth.auth().currentUser!.uid
return id
}
var completedList = [User]()
func showUserCompleted(_ update: @escaping () -> Void) {
currentUserRef.child("requests").child("completedRequests").observe(DataEventType.value, with: { (snapshot) in
self.completedList.removeAll()
for child in snapshot.children.allObjects as! [DataSnapshot] {
let fullKey = child.key
let components = fullKey.components(separatedBy: ",")
let date = components[0]
let id = components[1]
self.getUser(id, { (user) in
let requestTime = snapshot.childSnapshot(forPath: "\(fullKey)/time").value as! Int
let requestDescription = snapshot.childSnapshot(forPath: "\(fullKey)/description").value as! String
let bool = snapshot.childSnapshot(forPath: "\(fullKey)/bool").value as! Bool
user.requestTime = requestTime //aggiunge all'utente creato in requestList anche quanto tempo ha chiesto
user.requestDate = date
user.requestDescription = requestDescription
user.bool = bool
self.completedList.append(user)
update()
})
}
// If there are no children, run completion here instead
if snapshot.childrenCount == 0 {
update()
}
})
}
func getUser(_ userID: String,_ completion: @escaping (User) -> Void) {
userRef.child(userID).observeSingleEvent(of: DataEventType.value, with: { (snapshot) in
let email = snapshot.childSnapshot(forPath: "email").value as! String
let username = snapshot.childSnapshot(forPath: "name").value as! String
let usersurname = snapshot.childSnapshot(forPath: "surname").value as! String
let id = snapshot.key
completion(User(userEmail: email, userName: username, userSurname: usersurname, userID: id))
})
}
func getTimeCoinsCurrentUser(_ uid: String,_ completion: @escaping (Int) -> Void) {
userRef.child(uid).observeSingleEvent(of: .value, with: {(snapshot) in
let timeCoins = snapshot.childSnapshot(forPath: "timeCoins").value as! Int
print("COINS RETURNED \(timeCoins)")
completion(timeCoins)
})
}
//manda una richiesta ad un utente aggiungendo a tale utente un figlio nella sezione request
func sendRequestToUser(_ userID: String, _ time: Int, _ date: String, _ description: String) {
self.userRef.child(userID).child("requests").child("newRequests").child(date + "," + self.uid).setValue(["time": time, "bool": true, "date": date, "description": description])
}
/** Accepts a friend request from the user with the specified id */
func acceptFriendRequest(_ date: String, _ id: String) {
var time = 0
var description = ""
// utilizziamo questo observe per spostare il tempo e la richiesta da newRequests a onGoingRequests
currentUserRef.child("requests").child("newRequests").child(date + "," + id).observeSingleEvent(of: .value, with: { (DataSnapshot) in
if let dictionary = DataSnapshot.value as? [String: AnyObject] {
time = dictionary["time"] as! Int
description = dictionary["description"] as! String
self.currentUserRef.child("requests").child("onGoingRequests").child(date + "," + id).setValue(["time": time, "bool": true, "description": description, "date": date])
}
}, withCancel: nil)
currentUserRef.child("requests").child("newRequests").child(date + "," + id).removeValue()
}
func declineFriendRequest(_ date: String, _ userID: String) {
currentUserRef.child("requests").child("newRequests").child(date + "," + userID).removeValue()
}
func showTimeRequest() {
currentUserRef.child("requests").child("newRequests").observe(.value, with: { (DataSnapshot) in
})
}
var requestDates: [String] = []
var requestList = [User]()
func showRequests(_ update: @escaping () -> Void) {
currentUserRequestRef.child("newRequests").observeSingleEvent(of: .value, with: { (snapshot) in
self.requestList.removeAll()
self.requestDates.removeAll()
for child in snapshot.children.allObjects as! [DataSnapshot] {
let fullKey = child.key
let components = fullKey.components(separatedBy: ",")
let date = components[0]
let id = components[1]
let requestTime = snapshot.childSnapshot(forPath: "\(fullKey)/time").value as! Int
let requestDescription = snapshot.childSnapshot(forPath: "\(fullKey)/description").value as! String
self.getUser(id, { (user) in
user.requestTime = requestTime //aggiunge all'utente creato in requestList anche quanto tempo ha chiesto
user.requestDate = date
user.requestDescription = requestDescription
self.requestList.append(user)
self.requestDates.append(date)
update()
})
}
if snapshot.childrenCount == 0 {
update()
}
})
// currentUserRequestRef.child("newRequests").observe(DataEventType.value, with: { (snapshot) in
// self.requestList.removeAll()
// for child in snapshot.children.allObjects as! [DataSnapshot] {
// let id = child.key
// let requestTime = snapshot.childSnapshot(forPath: "request\(id)/time").value as! Int
// let requestDate = snapshot.childSnapshot(forPath: "\(id)/date").value as! String
// let requestDescription = snapshot.childSnapshot(forPath: "\(id)/description").value as! String
// self.getUser(id, { (user) in
// user.requestTime = requestTime //aggiunge all'utente creato in requestList anche quanto tempo ha chiesto
// user.requestDate = requestDate
// user.requestDescription = requestDescription
// self.requestList.append(user)
// update()
// })
// }
// // If there are no children, run completion here instead
// if snapshot.childrenCount == 0 {
// update()
// }
// })
}
var userList = [User]()
func showUser(_ update: @escaping () -> Void) {
FriendSystem.system.userRef.observe(DataEventType.value, with: { (snapshot) in
self.userList.removeAll()
for child in snapshot.children.allObjects as! [DataSnapshot] {
let email = child.childSnapshot(forPath: "email").value as! String
let name = child.childSnapshot(forPath: "name").value as! String
let surname = child.childSnapshot(forPath: "surname").value as! String
let codingDescription = child.childSnapshot(forPath: "skill/coding").value as! String
let designDescription = child.childSnapshot(forPath: "skill/design").value as! String
let businessDescription = child.childSnapshot(forPath: "skill/business").value as! String
let languageDescription = child.childSnapshot(forPath: "skill/language").value as! String
let otherDescription = child.childSnapshot(forPath: "skill/other").value as! String
let scienceDescription = child.childSnapshot(forPath: "skill/science").value as! String
let descriptions = codingDescription + " " + designDescription + " " + businessDescription + " " + languageDescription + " " + otherDescription + " " + scienceDescription
if email != Auth.auth().currentUser?.email! {
self.userList.append(User(userEmail: email, userName: name, userSurname: surname, userID: child.key, descriptions: descriptions))
}
}
update()
})
}
func showUserCoding(_ update: @escaping () -> Void) {
FriendSystem.system.userRef.observe(DataEventType.value, with: { (snapshot) in
self.userList.removeAll()
for child in snapshot.children.allObjects as! [DataSnapshot] {
let email = child.childSnapshot(forPath: "email").value as! String
let name = child.childSnapshot(forPath: "name").value as! String
let coding = child.childSnapshot(forPath: "skill/coding").value as! String
let surname = child.childSnapshot(forPath: "surname").value as! String
if email != Auth.auth().currentUser?.email! && coding != "" {
self.userList.append(User(userEmail: email, userName: name, userSurname: surname, userID: child.key))
}
}
update()
})
}
func showUserDesign(_ update: @escaping () -> Void) {
FriendSystem.system.userRef.observe(DataEventType.value, with: { (snapshot) in
self.userList.removeAll()
for child in snapshot.children.allObjects as! [DataSnapshot] {
let email = child.childSnapshot(forPath: "email").value as! String
let name = child.childSnapshot(forPath: "name").value as! String
let design = child.childSnapshot(forPath: "skill/design").value as! String
let surname = child.childSnapshot(forPath: "surname").value as! String
if email != Auth.auth().currentUser?.email! && design != "" {
self.userList.append(User(userEmail: email, userName: name, userSurname: surname, userID: child.key))
}
}
update()
})
}
func showUserBusiness(_ update: @escaping () -> Void) {
FriendSystem.system.userRef.observe(DataEventType.value, with: { (snapshot) in
self.userList.removeAll()
for child in snapshot.children.allObjects as! [DataSnapshot] {
let email = child.childSnapshot(forPath: "email").value as! String
let name = child.childSnapshot(forPath: "name").value as! String
let business = child.childSnapshot(forPath: "skill/business").value as! String
let surname = child.childSnapshot(forPath: "surname").value as! String
if email != Auth.auth().currentUser?.email! && business != "" {
self.userList.append(User(userEmail: email, userName: name, userSurname: surname, userID: child.key))
}
}
update()
})
}
func showUserLanguage(_ update: @escaping () -> Void) {
FriendSystem.system.userRef.observe(DataEventType.value, with: { (snapshot) in
self.userList.removeAll()
for child in snapshot.children.allObjects as! [DataSnapshot] {
let email = child.childSnapshot(forPath: "email").value as! String
let name = child.childSnapshot(forPath: "name").value as! String
let language = child.childSnapshot(forPath: "skill/language").value as! String
let surname = child.childSnapshot(forPath: "surname").value as! String
if email != Auth.auth().currentUser?.email! && language != "" {
self.userList.append(User(userEmail: email, userName: name, userSurname: surname, userID: child.key))
}
}
update()
})
}
func showUserScience(_ update: @escaping () -> Void) {
FriendSystem.system.userRef.observe(DataEventType.value, with: { (snapshot) in
self.userList.removeAll()
for child in snapshot.children.allObjects as! [DataSnapshot] {
let email = child.childSnapshot(forPath: "email").value as! String
let name = child.childSnapshot(forPath: "name").value as! String
let science = child.childSnapshot(forPath: "skill/science").value as! String
let surname = child.childSnapshot(forPath: "surname").value as! String
if email != Auth.auth().currentUser?.email! && science != "" {
self.userList.append(User(userEmail: email, userName: name, userSurname: surname, userID: child.key))
}
}
update()
})
}
func showUserOther(_ update: @escaping () -> Void) {
FriendSystem.system.userRef.observe(DataEventType.value, with: { (snapshot) in
self.userList.removeAll()
for child in snapshot.children.allObjects as! [DataSnapshot] {
let email = child.childSnapshot(forPath: "email").value as! String
let name = child.childSnapshot(forPath: "name").value as! String
let other = child.childSnapshot(forPath: "skill/other").value as! String
let surname = child.childSnapshot(forPath: "surname").value as! String
if email != Auth.auth().currentUser?.email! && other != "" {
self.userList.append(User(userEmail: email, userName: name, userSurname: surname, userID: child.key))
}
}
update()
})
}
var onGoingList = [User]()
var onGoingDates: [String] = []
func showUserOnGoing(_ update: @escaping () -> Void) {
currentUserRef.child("requests").child("onGoingRequests").observe(DataEventType.value, with: { (snapshot) in
self.onGoingList.removeAll()
self.onGoingDates.removeAll()
for child in snapshot.children.allObjects as! [DataSnapshot] {
let fullKey = child.key
let components = fullKey.components(separatedBy: ",")
let date = components[0]
let id = components[1]
self.getUser(id, { (user) in
let requestTime = snapshot.childSnapshot(forPath: "\(fullKey)/time").value as! Int
let requestDescription = snapshot.childSnapshot(forPath: "\(fullKey)/description").value as! String
user.requestTime = requestTime //aggiunge all'utente creato in requestList anche quanto tempo ha chiesto
user.requestDate = date
user.requestDescription = requestDescription
self.onGoingList.append(user)
self.onGoingDates.append(date)
update()
})
}
// If there are no children, run completion here instead
if snapshot.childrenCount == 0 {
update()
}
})
}
}
class Singleton {
static var shared = Singleton()
var array: [ProfileSeller] = []
}
class ProfileSeller {
static var shared = ProfileSeller()
var profilePic = UIImage()
var name = ""
var surname = ""
var birthday = ""
var badge = 0
var timeCoins = 0
var codingDescription = ""
var designDescription = ""
var businessDescription = ""
var languageDescription = ""
var scienceDescription = ""
var othersDescription = ""
}
class listOfPeople {
static let shared = listOfPeople()
var allNames = [String]()
}