1. Detect a match everytime we swipe a card
  2. Remove cards we swiped before

Detect match everytime we swipe a card

Detecting will be invoked when swiping datas are saved in firestore successfully.

            if snapshot?.exists == true {
                Firestore.firestore().collection("swipes").document(uid).updateData(documentData, completion: { (err) in
                    ...
                    
                    print("Successfully updated swipe data...")
                    if didLike == 1 {
                        self.checkIfMatchExists()
                    }
                })
            } else {
                Firestore.firestore().collection("swipes").document(uid).setData(documentData, completion: { (err) in
                    ...
                    
                    print("Successfully saved swipe data...")
                    if didLike == 1 {
                        self.checkIfMatchExists()
                    }
                })
            }

After swiping a card to the right which means ‘like’ we need to detect a match

                    // didLike == 1 means Like
                    if didLike == 1 {
                        self.checkIfMatchExists()
                    }

checkingMatch function

we need a card(topCardView) uid, so make a parameter to get a cardUID All we need to do is get datas of a card we swiped in swiped collection in firestore

    fileprivate func checkIfMatchExists(cardUID: String) {
        
        Firestore.firestore().collection("swipes").document(cardUID).getDocument { (snapshot, err) in
            if let err = err {
                print("Failed to fetch document for card user:", err)
                return
            }
            
            guard let data = snapshot?.data() as? [String: Int] else { return }
            guard let uid = Auth.auth().currentUser?.uid else { return }
            
            let hasMatched = data[uid] == 1
            
            if hasMatched {
                print("It's matched")
                let hud = JGProgressHUD(style: .dark)
                hud.textLabel.text = "Matched!"
                hud.show(in: self.view)
                hud.dismiss(afterDelay: 3)
            }

        }
    }

Remove cards we swiped before

Fetch swipe info before fetch users from firestore

    var swipes = [String: Int]()

    fileprivate func fetchSwipes() {
        hud.textLabel.text = "Loading"
        hud.show(in: view)
        
        guard let uid = Auth.auth().currentUser?.uid else { return }
        Firestore.firestore().collection("swipes").document(uid).getDocument { (snapshot, err) in
            if let err = err {
                print("Failed to fetch swipes info for current user:", err)
                return
            }
            
            guard let data = snapshot?.data() as? [String: Int] else { return }
            self.swipes = data
            self.fetchUsersFromFirestore()
        }
    }

in fetchUsersFromFirestore()

we need to check current user, swiped users

            snapshot?.documents.forEach({ (documentSnapshot) in
                let userDictionary = documentSnapshot.data()
                let user = User(dictionary: userDictionary)
                
                let isNotCurrentUser = user.uid != Auth.auth().currentUser?.uid
                let hasNotSwipedBefore = self.swipes[user.uid!] == nil
                
                if isNotCurrentUser && hasNotSwipedBefore {
                    let cardView = self.setupCardFromUser(user: user)
                    
                    previousCardView?.nextCardView = cardView
                    previousCardView = cardView
                    
                    if self.topCardView == nil {
                        self.topCardView = cardView
                    }
                }

태그: ,

카테고리:

업데이트:

댓글남기기