Swift&Firebase关注/取消关注按钮

时间:2019-03-24 18:05:13

标签: swift firebase

我目前正在使用Firebase构建应用程序,我有一个关注按钮,该按钮可以关注用户,如果已经关注,则取消关注用户。

在我无法关注用户之后,当我单击按钮时,数据将添加到Firebase中,然后再次被删除。.

当我再次运行该应用程序时,当我再次关注时,一切正常都可以找到与同一个bug不同的东西 这是我的Firebase数据库[FirebaseDatabase] [1]

这是我的代码

   var follower = false

   override func viewDidLoad() {
        super.viewDidLoad()

        refreshControl.addTarget(self, action: #selector(refresh), for: UIControl.Event.valueChanged)

        self.tableView.addSubview(refreshControl)


        if guest == true {



        buttonRight.setImage(UIImage(named:"icons8-user-menu-male-filled-30"), for: .normal)

        firebaseControllerHandle.delegate = self
        firebaseControllerHandle.getUserInfo(uid:guestuid)
        firebaseControllerHandle.userDelegate = self
        firebaseControllerHandle.getUserProducts(userID: guestuid)

            NotificationCenter.default.addObserver(self, selector: #selector(self.filterRefresh(_:)), name: NSNotification.Name(rawValue: "followRefresh"), object: nil)



        } else {


            if let user = Auth.auth().currentUser {
                if !user.isEmailVerified{


                    let alert = JDropDownAlert()
                    alert.alertWith("Please Verify your Email Address")

                    alert.didTapBlock = {

                        let myVC = self.storyboard?.instantiateViewController(withIdentifier: "verify") as! VerifyEmailViewController

                        let navController = UINavigationController(rootViewController: myVC)

                        self.navigationController?.present(navController, animated: true, completion: nil)


                    }

                    } else {
                        print ("Email verified. Signing in...")
                }

            }



            firebaseControllerHandle.delegate = self
            firebaseControllerHandle.userDelegate = self
            firebaseControllerHandle.getUserInfo(uid:(Auth.auth().currentUser?.uid)!)
            firebaseControllerHandle.getUserProducts(userID: (Auth.auth().currentUser?.uid)!)
        }





        self.navigationController?.navigationBar.shadowImage = UIImage()


    }



    @objc func filterRefresh(_ notification:Notification) {

        firebaseControllerHandle.delegate = self
        firebaseControllerHandle.getUserInfo(uid:guestuid)
        firebaseControllerHandle.userDelegate = self
        firebaseControllerHandle.getUserProducts(userID: guestuid)
    }




    @objc func refresh() {
        // Code to refresh table view

        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "refresh"), object: nil)
        // `default` is now a property, not a method call

        if guest == true {

            firebaseControllerHandle.delegate = self
            firebaseControllerHandle.getUserInfo(uid:guestuid)
            firebaseControllerHandle.userDelegate = self
            firebaseControllerHandle.getUserProducts(userID: guestuid)

        } else {

             print("ont")

        }
        refreshControl.endRefreshing()
    }

    func loadFollowings(followings: [String]) {
        let myVC = self.storyboard?.instantiateViewController(withIdentifier: "Following") as! FollowingsViewController

        let navController = UINavigationController(rootViewController: myVC)

        self.navigationController?.present(navController, animated: true, completion: nil)
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }



    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return 2
    }



    func userInfo(user: UserModel) {


        self.user = user


        if guest == true {

        self.guestUser = user

        follower = false

            for x in user.followers {



                if x == Auth.auth().currentUser?.uid {


                    follower = true

                } else {

                    follower = false
                }
            }

            print("guest")
            self.tableView.reloadData()
        } else {

            //self.user = user
            print("test")
            self.tableView.reloadData()
        }
    }


    var follower = false
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


        switch indexPath.row {
        case 0:
            let cell = tableView.dequeueReusableCell(withIdentifier: "profile", for: indexPath) as! profileCell




            if guest == true {


               cell.guestUserID = guestuid


                if follower == true {


                    cell.button.setTitle("Following", for: .normal)


                    cell.buttonChecked = true


                } else {

                    cell.button.setTitle("Follow", for: .normal)

                    cell.buttonChecked = false


                }

                cell.bio.text = guestUser.bio
                cell.sendMessageButton.setTitle("Send Message", for: .normal)



            } else {


                let url = URL(string: user.profilePhoto)

                print(user.profilePhoto)


                cell.img.kf.setImage(with: url)
                cell.bio.text = user.bio
                cell.button.setTitle("Edit Profile", for: .normal)
                cell.sendMessageButton.setTitle("My Orders", for: .normal)
                cell.fullname.text = user.fullName
            }

            cell.delegate = self
            return cell
        case 1 :
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! profileInfo


            return cell
        default:
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! profileCell


            return cell
        }

    }



    ```

    ```
this is my cell button action 

   @IBAction func followedit(_ sender: Any) {


        if buttonChecked == true {

            print("REMOVE FOLLOW")
            self.delegate?.myTableDelegate()

            let appearance = SCLAlertView.SCLAppearance(
                showCloseButton: false, showCircularIcon: false


            )

            // Initialize SCLAlertView using custom Appearance
            let alert = SCLAlertView(appearance: appearance)


            alert.addButton("Unfollow",  backgroundColor: UIColor.black, textColor: UIColor.white) {

                let userRef = self.ref.child("Users").child(CurrentUserID.uid).child("Followings")


                let query = userRef.queryOrdered(byChild: "userid").queryEqual(toValue: self.guestUserID)

                let guestRemove = self.ref.child("Users").child(self.guestUserID).child("Followers")


                let guestRemoveQuery = guestRemove.queryOrdered(byChild: "userid").queryEqual(toValue: CurrentUserID.uid)



                guestRemoveQuery.observe(.childAdded, with: { snapshot in

                    if snapshot.exists() {


                        snapshot.ref.removeValue()




                    } else {
                        print("snapshot doesn't exist")
                    }
                })


                query.observe(.childAdded, with: { snapshot in

                    if snapshot.exists() {


                        snapshot.ref.removeValue()


                    } else {
                        print("snapshot doesn't exist")
                    }
                })

            }

            alert.addButton("Cancel",  backgroundColor: UIColor.black, textColor: UIColor.white) {

                alert.dismiss(animated: true, completion: nil)

            }
            alert.showEdit("Unfollow \(guestUserID)", subTitle: "Are you sure you want to unfollow \(guestUserID)?")


        } else {

            print("ADD FOLLOW")

            ref.child("Users").child(CurrentUserID.uid).child("Followings").childByAutoId().setValue(["userid" : guestUserID])

            ref.child("Users").child(guestUserID).child("Followers").childByAutoId().setValue(["userid" : CurrentUserID.uid])

            print(buttonChecked)

            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "followRefresh"), object: nil)


        }



    }


    ```
  [1]: https://i.stack.imgur.com/5sMqa.png

0 个答案:

没有答案
相关问题