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) -> ())) {
|
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")) {
|
||||||
self.navigationStack.push(InnerCircleView())
|
print("adding contact to circle")
|
||||||
}
|
// call method in vm to get it done, then navigate to the circle
|
||||||
} message: {
|
self.contactsVM.addOrActivateFriendToCircle(userId: self.authSessionStore.user!.id!, friendId: (contact.user!.id)!) {res in
|
||||||
Text(self.alertMessage)
|
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
|
else { // invite button to text the person
|
||||||
|
|||||||
Reference in New Issue
Block a user