adding logic for the friend relationship to be solid

This commit is contained in:
talksik
2021-12-20 23:44:58 -08:00
parent 78ece9d243
commit 46ccd91335
3 changed files with 58 additions and 18 deletions
@@ -110,12 +110,35 @@ class FirestoreService {
func createOrUpdateUserFriends(userFriend: UserFriends, completion: @escaping((_ state: ServiceState) -> ())) { func createOrUpdateUserFriends(userFriend: UserFriends, completion: @escaping((_ state: ServiceState) -> ())) {
do { do {
if userFriend.id != nil { let userFriendCollection = db.collection(Collection.userFriends.rawValue)
let _ = try db.collection(Collection.userFriends.rawValue).addDocument(from: userFriend)
completion(ServiceState.success("created/updated userfriend in firestore service")) // get the userFriend based on...if not exists, then update
} else { let userFriendDocRef = userFriendCollection.whereField("userId", isEqualTo: userFriend.userId).whereField("friendId", isEqualTo: userFriend.friendId)
completion(ServiceState.error(ServiceError(description: "No userfriend id given to firestore service")))
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 { } catch {
print("error in creating user friend \(error.localizedDescription)") print("error in creating user friend \(error.localizedDescription)")
completion(ServiceState.error(ServiceError(description: error.localizedDescription))) completion(ServiceState.error(ServiceError(description: error.localizedDescription)))
@@ -82,7 +82,6 @@ class ContactsViewModel : ObservableObject {
} }
self?.contacts[contactVm.sortingProp] = contactVm 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 // 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) var userFriend = UserFriends(userId: userId, friendId: friendId, isActive: true, lastUpdatedTimestamp: nil, createdTimestamp: nil)
self.firestoreService.createOrUpdateUserFriends(userFriend: userFriend) {[weak self] res in self.firestoreService.createOrUpdateUserFriends(userFriend: userFriend) {[weak self] res in
print(res) completion(res)
} }
} }
} }
@@ -99,16 +99,27 @@ struct ListContactRow: View {
.foregroundColor(NirvanaColor.solidTeal) .foregroundColor(NirvanaColor.solidTeal)
} }
} }
.alert(self.alertText, isPresented: self.$showAlert) { .alert(isPresented: self.$showAlert) {
Button("Add \(contact.cnName)") { Alert(
print("adding contact to circle") title: Text(self.alertText),
// call method in vm to get it done, then navigate to the circle message: Text(self.alertMessage),
self.contactsVM.addOrActivateFriendToCircle(userId: self.authSessionStore.user!.id!, friendId: (contact.user!.id)!) 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)
self.navigationStack.push(InnerCircleView()) switch res {
} case .error(let err):
} message: { print(err)
Text(self.alertMessage) case .success(let str):
print(str)
self.navigationStack.push(InnerCircleView())
}
}
}
)
} }
} }
else { // invite button to text the person else { // invite button to text the person