From 4651114dcbbf962c6e3b0dfa9f7511e61d49ec11 Mon Sep 17 00:00:00 2001 From: talksik Date: Sat, 25 Dec 2021 00:18:45 -0800 Subject: [PATCH] got alerts and actions working, but the ui isn't smooth with the grid because I have too many elements changing --- .../Repositories/FirestoreService.swift | 5 +-- .../Views/Contacts/ContactsViewModel.swift | 2 +- .../Views/Contacts/FindFriendsView.swift | 4 +- .../Views/InnerCircle/CircleGridView.swift | 40 +++++++++++++++---- .../InnerCircle/InnerCircleViewModel.swift | 19 +++++++++ 5 files changed, 57 insertions(+), 13 deletions(-) diff --git a/nirvana-ios/Repositories/FirestoreService.swift b/nirvana-ios/Repositories/FirestoreService.swift index 6bc1b62..82308da 100644 --- a/nirvana-ios/Repositories/FirestoreService.swift +++ b/nirvana-ios/Repositories/FirestoreService.swift @@ -108,7 +108,7 @@ class FirestoreService { return FieldValue.serverTimestamp() } - func createOrUpdateUserFriends(userFriend: UserFriends, completion: @escaping((_ state: ServiceState) -> ())) { + func createOrUpdateUserFriends(userFriend: UserFriends, activateOrDeactivate: Bool, completion: @escaping((_ state: ServiceState) -> ())) { do { let userFriendCollection = db.collection(Collection.userFriends.rawValue) @@ -131,14 +131,13 @@ class FirestoreService { 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(["isActive": true, "lastUpdatedTimestamp": self.getFirestoreServerTimestamp()], merge:true) + let _ = try? userFriendCollection.document(document.documentID).setData(["isActive": activateOrDeactivate, "lastUpdatedTimestamp": self.getFirestoreServerTimestamp()], merge:true) 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 e53f1e6..429208d 100644 --- a/nirvana-ios/Views/Contacts/ContactsViewModel.swift +++ b/nirvana-ios/Views/Contacts/ContactsViewModel.swift @@ -103,7 +103,7 @@ class ContactsViewModel : ObservableObject { // setting timestamps to nil to make sure that new server timestamp is set var userFriend = UserFriends(userId: userId, friendId: friendId, isActive: true, lastUpdatedTimestamp: nil) - self.firestoreService.createOrUpdateUserFriends(userFriend: userFriend) {[weak self] res in + self.firestoreService.createOrUpdateUserFriends(userFriend: userFriend, activateOrDeactivate: true) {[weak self] res in completion(res) } } diff --git a/nirvana-ios/Views/Contacts/FindFriendsView.swift b/nirvana-ios/Views/Contacts/FindFriendsView.swift index 85a25a7..7066fbb 100644 --- a/nirvana-ios/Views/Contacts/FindFriendsView.swift +++ b/nirvana-ios/Views/Contacts/FindFriendsView.swift @@ -21,9 +21,9 @@ struct FindFriendsView: View { // main content NavigationView { VStack { - Text("You must have someone in your phone contacts to add them. You can only add 10 people to your circle! 🥬") + Text("You must have someone in your phone contacts to add them. Remember: \(self.authSessionStore.getActiveFriendIds().count)/10 spots filled in your circle. 🥬") .font(.subheadline) - .foregroundColor(NirvanaColor.teal) + .foregroundColor(Color.gray) .padding(.horizontal) List { diff --git a/nirvana-ios/Views/InnerCircle/CircleGridView.swift b/nirvana-ios/Views/InnerCircle/CircleGridView.swift index 877608e..0f0de2f 100644 --- a/nirvana-ios/Views/InnerCircle/CircleGridView.swift +++ b/nirvana-ios/Views/InnerCircle/CircleGridView.swift @@ -45,6 +45,10 @@ struct CircleGridView: View { let longPressMinDuration = 0.5 + @State var alertActive = false + @State var alertText = "" + @State var alertSubtext = "" + var body: some View { // main communication hub // TODO: client side, sort the honeycomb from top left to bottom right @@ -178,10 +182,32 @@ struct CircleGridView: View { .frame(height: Self.size) .onTapGesture { // action to open alert to add this person to circle or reject + let inboxFriendName = self.authSessionStore.relevantUsersDict[inboxUserId]?.nickname ?? "" + let inboxFriendNumber = self.authSessionStore.relevantUsersDict[inboxUserId]?.phoneNumber - // create user_friend with isActive = false + self.alertText = "🌴Add to your circle?" + self.alertSubtext = "\(inboxFriendName ?? "") started a convo with you... \n \(inboxFriendNumber!) \n Remember: you have \(10 - activeFriends.count) spots left!" + self.alertActive.toggle() } .animation(Animation.spring()) + .alert(isPresented: self.$alertActive) { + Alert( + title: Text(self.alertText), + message: Text(self.alertSubtext), + primaryButton: .destructive(Text("Reject"), action: { + // create user friend but a rejected one + self.innerCircleVM.activateOrDeactiveInboxUser(activate: false, userId: self.authSessionStore.user!.id!, friendId: inboxUserId) { res in + print(res) + } + }), + secondaryButton: .default(Text("Add"), action: { + // create user friend + self.innerCircleVM.activateOrDeactiveInboxUser(activate: true, userId: self.authSessionStore.user!.id!, friendId: inboxUserId) { res in + print(res) + } + }) + ) + } } // stale state for adding a contact @@ -219,13 +245,13 @@ struct CircleGridView: View { .padding(.trailing, Self.size / 2 + Self.spacingBetweenColumns / 2) // because of the offset of last column .padding(.top, Self.size / 2 + Self.spacingBetweenRows / 2) // because we are going under the nav bar }// scrollview - .onAppear { - //TODO: was causing problems so commenting out - //may not need anymore with bottom nav activation -// scrollReaderValue.scrollTo(Self.numberOfItems / 2) - - } } // scrollview reader + .onAppear { + //TODO: was causing problems so commenting out + //may not need anymore with bottom nav activation +// scrollReaderValue.scrollTo(Self.numberOfItems / 2) + } + } diff --git a/nirvana-ios/Views/InnerCircle/InnerCircleViewModel.swift b/nirvana-ios/Views/InnerCircle/InnerCircleViewModel.swift index 87422c0..d15af13 100644 --- a/nirvana-ios/Views/InnerCircle/InnerCircleViewModel.swift +++ b/nirvana-ios/Views/InnerCircle/InnerCircleViewModel.swift @@ -109,3 +109,22 @@ extension InnerCircleViewModel { self.pushNotificationService.updateFirestorePushTokenIfNeeded() } } + +// handle activating or deactivating friends +extension InnerCircleViewModel { + func activateOrDeactiveInboxUser(activate: Bool, 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 yourself, 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) + + self.firestoreService.createOrUpdateUserFriends(userFriend: userFriend, activateOrDeactivate: activate) {[weak self] res in + completion(res) + } + } +}