got alerts and actions working, but the ui isn't smooth with the grid because I have too many elements changing

This commit is contained in:
talksik
2021-12-25 00:18:45 -08:00
parent 70b869f08d
commit 4651114dcb
5 changed files with 57 additions and 13 deletions
@@ -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)))
@@ -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)
}
}
@@ -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 {
@@ -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)
}
}
@@ -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)
}
}
}