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...