adding logic for the friend relationship to be solid
This commit is contained in:
@@ -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)))
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user