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
@@ -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