From 46ccd913357a7a31783f53cc78abacf3b821265b Mon Sep 17 00:00:00 2001 From: talksik Date: Mon, 20 Dec 2021 23:44:58 -0800 Subject: [PATCH] adding logic for the friend relationship to be solid --- .../Repositories/FirestoreService.swift | 33 ++++++++++++++++--- .../Views/Contacts/ContactsViewModel.swift | 12 +++++-- .../Views/Contacts/FindFriendsView.swift | 31 +++++++++++------ 3 files changed, 58 insertions(+), 18 deletions(-) diff --git a/nirvana-ios/Repositories/FirestoreService.swift b/nirvana-ios/Repositories/FirestoreService.swift index abca4ca..e569e9b 100644 --- a/nirvana-ios/Repositories/FirestoreService.swift +++ b/nirvana-ios/Repositories/FirestoreService.swift @@ -110,12 +110,35 @@ class FirestoreService { func createOrUpdateUserFriends(userFriend: UserFriends, completion: @escaping((_ state: ServiceState) -> ())) { do { - if userFriend.id != nil { - let _ = try db.collection(Collection.userFriends.rawValue).addDocument(from: userFriend) - completion(ServiceState.success("created/updated userfriend in firestore service")) - } else { - completion(ServiceState.error(ServiceError(description: "No userfriend id given to firestore service"))) + let userFriendCollection = db.collection(Collection.userFriends.rawValue) + + // get the userFriend based on...if not exists, then update + let userFriendDocRef = userFriendCollection.whereField("userId", isEqualTo: userFriend.userId).whereField("friendId", isEqualTo: userFriend.friendId) + + userFriendDocRef.getDocuments() { (querySnapshot, err) in + if let err = err { + print("error in trying to check if user friend relationship exists \(err.localizedDescription)") + completion(ServiceState.error(ServiceError(description: err.localizedDescription))) + } else { + // TODO: there shoudn't be multiple, see if this would cause issues + if querySnapshot!.documents.isEmpty { //not existing already, create one + let _ = try? userFriendCollection.addDocument(from: userFriend) + + print("created new user friend! no existing relationship") + + completion(ServiceState.success("created userfriend in firestore service")) + } + else { // there seems to be something existing + for document in querySnapshot!.documents { + // update this userFriend that is existing + let _ = try? userFriendCollection.document(document.documentID).setData(from: userFriend) + print("already existing, just updated") + completion(ServiceState.success("updated userfriend in firestore service")) + } + } + } } + } catch { print("error in creating user friend \(error.localizedDescription)") completion(ServiceState.error(ServiceError(description: error.localizedDescription))) diff --git a/nirvana-ios/Views/Contacts/ContactsViewModel.swift b/nirvana-ios/Views/Contacts/ContactsViewModel.swift index 9ded3e2..8e9c404 100644 --- a/nirvana-ios/Views/Contacts/ContactsViewModel.swift +++ b/nirvana-ios/Views/Contacts/ContactsViewModel.swift @@ -82,7 +82,6 @@ class ContactsViewModel : ObservableObject { } self?.contacts[contactVm.sortingProp] = contactVm - print(self?.contacts) } } } @@ -93,12 +92,19 @@ class ContactsViewModel : ObservableObject { } } - func addOrActivateFriendToCircle(userId: String, friendId: String) { + func addOrActivateFriendToCircle(userId: String, friendId: String, completion: @escaping((_ state: ServiceState) -> ())) { + // validation + // make sure userId is not the same as friendId...don't want people friending themselves + if userId == friendId { + completion(ServiceState.error(ServiceError(description: "You cannot friend himself! Silly!"))) + return + } + // setting timestamps to nil to make sure that new server timestamp is set var userFriend = UserFriends(userId: userId, friendId: friendId, isActive: true, lastUpdatedTimestamp: nil, createdTimestamp: nil) self.firestoreService.createOrUpdateUserFriends(userFriend: userFriend) {[weak self] res in - print(res) + completion(res) } } } diff --git a/nirvana-ios/Views/Contacts/FindFriendsView.swift b/nirvana-ios/Views/Contacts/FindFriendsView.swift index 611c05a..f1a1f55 100644 --- a/nirvana-ios/Views/Contacts/FindFriendsView.swift +++ b/nirvana-ios/Views/Contacts/FindFriendsView.swift @@ -99,16 +99,27 @@ struct ListContactRow: View { .foregroundColor(NirvanaColor.solidTeal) } } - .alert(self.alertText, isPresented: self.$showAlert) { - Button("Add \(contact.cnName)") { - print("adding contact to circle") - // call method in vm to get it done, then navigate to the circle - self.contactsVM.addOrActivateFriendToCircle(userId: self.authSessionStore.user!.id!, friendId: (contact.user!.id)!) - - self.navigationStack.push(InnerCircleView()) - } - } message: { - Text(self.alertMessage) + .alert(isPresented: self.$showAlert) { + Alert( + title: Text(self.alertText), + message: Text(self.alertMessage), + primaryButton: .default(Text("Cancel")), + secondaryButton: .default(Text("Confirm")) { + print("adding contact to circle") + // call method in vm to get it done, then navigate to the circle + self.contactsVM.addOrActivateFriendToCircle(userId: self.authSessionStore.user!.id!, friendId: (contact.user!.id)!) {res in + print(res) + + switch res { + case .error(let err): + print(err) + case .success(let str): + print(str) + self.navigationStack.push(InnerCircleView()) + } + } + } + ) } } else { // invite button to text the person