0

In the Tab Bar Controller here are four tabs namely home, discover, notification and user profile. The discover tab controller lists all the users in Firebase. The users are listed with username and follow button. If the current user taps on follow, the title is set to following.

protocol PeopleTableViewCellDelegate {
func goToProfileUserVC(userId: String)
}



@IBOutlet weak var profileImage: UIImageView!
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var followButton: UIButton!

var delegate: PeopleTableViewCellDelegate?
var user: User? {
    didSet {
        updateView()
    }
}

func updateView() {
    nameLabel.text = user?.username
    if let photoUrlString = user?.profileImageUrl {
        let photoUrl = URL(string: photoUrlString)
        profileImage.sd_setImage(with: photoUrl, placeholderImage: UIImage(named: "placeholderImg"))

    }


    if user!.isFollowing! {
        configureUnFollowButton()
    } else {
        configureFollowButton()
    }

}

func configureFollowButton() {
    followButton.layer.borderWidth = 1
    followButton.layer.borderColor = UIColor(red: 226/255, green: 228/255, blue: 232.255, alpha: 1).cgColor
    followButton.layer.cornerRadius = 5
    followButton.clipsToBounds = true

    followButton.setTitleColor(UIColor.white, for: UIControlState.normal)
    followButton.backgroundColor = UIColor(red: 69/255, green: 142/255, blue: 255/255, alpha: 1)
    followButton.setTitle("Follow", for: UIControlState.normal)
    followButton.addTarget(self, action: #selector(self.followAction), for: UIControlEvents.touchUpInside)
}

func configureUnFollowButton() {
    followButton.layer.borderWidth = 1
    followButton.layer.borderColor = UIColor(red: 226/255, green: 228/255, blue: 232.255, alpha: 1).cgColor
    followButton.layer.cornerRadius = 5
    followButton.clipsToBounds = true

    followButton.setTitleColor(UIColor.black, for: UIControlState.normal)
    followButton.backgroundColor = UIColor.clear
    followButton.setTitle("Following", for: UIControlState.normal)
    followButton.addTarget(self, action: #selector(self.unFollowAction), for: UIControlEvents.touchUpInside)
}

func followAction() {
    if user!.isFollowing! == false {
        Api.Follow.followAction(withUser: user!.id!)
        configureUnFollowButton()
        user!.isFollowing! = true
    }
}

func unFollowAction() {
    if user!.isFollowing! == true {
        Api.Follow.unFollowAction(withUser: user!.id!)
        configureFollowButton()
        user!.isFollowing! = false
    }
}

override func awakeFromNib() {
    super.awakeFromNib()
    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.nameLabel_TouchUpInside))
    nameLabel.addGestureRecognizer(tapGesture)
    nameLabel.isUserInteractionEnabled = true
}

func nameLabel_TouchUpInside() {
    if let id = user?.id {
        delegate?.goToProfileUserVC(userId: id)
    }
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)


}

}

Here code for duplicate user:

     func loadUsers() {

  //        self.users = []

    API.User.observeUser { (user) in

        self.isFollowing(userId: user.id!, completed: { (value) in
            user.isFollowing = value

            self.users.append(user)
            self.tableView.reloadData()

        })
    }

}

My issue: The list show at Discover tab is duplicated and when user taps on the follow button, it's title is set to following. But if the user closes and reopens the app, upon return to Discover tab the previously followed user's button remains follow despite the data having been inserted correctly in Firebase.

Any help much appreciated please...

Do2
  • 1,751
  • 1
  • 16
  • 28
BizDev
  • 371
  • 1
  • 4
  • 21
  • So, are you adding user objects again to the array locally? How do you ensure that any user that is added doesn't already exist in the array? – cnbecom Jun 20 '18 at 14:20
  • How is the list duplicated? Do you see it duplicated in Firebase? – Do2 Jun 20 '18 at 14:44
  • @Do2 No in firebase its not duplicated – BizDev Jun 21 '18 at 16:07
  • Duplicated list issue fixed...Follow and following button issue is pending.. – BizDev Jun 21 '18 at 16:21
  • Are you using follow and unfollow `Instagram API`?. I am working on `follow` user feature. Those [API deprecated](https://www.instagram.com/developer/changelog/). Can you explain how do you using those features? – Mathi Arasan Jun 28 '18 at 08:11

1 Answers1

1

I am writting this as an answer as there is (probably) not enough space to write it as a comment. First of all your set up looks a bit messy, it seems that some details are handled locally and some on Firebase, and nothing in your code is triggering loadUsers().

I would advice you to set up as following:

  1. Create a class named followingUsers give them the necessary properties e.g following and set your initializer. Then add var followingUsersArray = [followingUsers]() inside your ViewController.
  2. Put the obeserve Firebase function in your viewDidLoad(), get all the data and put it in your followingUsersArray then reload your tableView.
  3. Create a UITableViewCell class and give it the desired properties, then set a method to configure the cell. Call that method from cellAtRow and pass it the values from the followingUsersArray.
Do2
  • 1,751
  • 1
  • 16
  • 28