iOS Follow with Profile Header
Fullstack Social iOS NodeJS REST
In the last lesson, we were able to list out all the post objects when tapping into a user's profile. We are now ready to render out a header at the very top of the screen that will allow us to tap on follow or unfollow. Upon tapping, we'll fire off a follow request to the server to make sure this action is persisted. Once a response is returned, we'll flip the value in our app and refresh the button's state.

Comments (3)
mdoor1123
3 years ago
Hey Brian - First off fantastic video. Can you provide a bit of guidance on how you would use the delegate to refresh the UserSearchController? You mentioned this at the end of the video that maybe you'd include this in the "Downloads Project." Great content as always. Thanks, Mike
mreaybeaton
3 years ago
Hi mdoor, I looked into this, and one way is to move the searchForUsers into viewWillAppear, so: override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) Service.shared.searchForUsers { (response) in switch response { case .failure(let error): print("Failed to find users:", error) case .success(let users): self.items = users self.collectionView.reloadData() } } } But I don't know if Brian is referring to something a bit more slicker than this i.e. refreshing only the user that had changed maybe, as opposed to this solution which refreshes the whole view, even though it would only ever be one user. I will look into this and see if that can be done.
AlfieLBTA
3 years ago
Hey mdoor: First you add the protocol in the Profile controller: protocol ProfileControllerDelegate: class { func toggleFollowBtnState(isFollowing: Bool) } then you add the method on a UserSearchController extension: extension UserSearchController: ProfileControllerDelegate { func toggleFollowBtnState(isFollowing: Bool) { guard let index = self.items.firstIndex(where: {$0.id == selectedUser?.id}) else { return } self.items[index].isFollowing = !isFollowing } } On the UserSearchController collection view didselect method you add pass self as delegate nad declare the selected user: selectedUser = user controller.delegate = self And on the Profile controller you catch the delegate and run the delegate method once the Api response returns a success: weak var delegate: ProfileControllerDelegate? self.delegate?.toggleFollowBtnState(isFollowing: isFollowing) <- this one invokes the protocol that delegates the task to the previous controller.
Christopher J. Roura
2 years ago
in the code you show the line self.delegate?.toggleFollowBtnState(isFollowing: isFollowing) is giving me problems. I have to use self.delegate?.toggleFollowBtnState(isFollowing: user.isFollowing ?? false) and specify the user and because it is optional have to nil coalesce it. This for me is leading to a very buggy ui where it always marks it false first and then reverts back to being whatever the value is. Could you possibly elaborate more on that line? I know it was a very long time ago but I feel that would still be relevant and helpful to a lot of people.
AlfieLBTA
2 years ago
Hey Christopher In deed it's been a while since i made this tut, but nevertheless let me try to help you out: I dunno if the coalescence has to do with a new swift version i would start by looking if you are assigning the user to the controller once it loads : selectedUser = user, else i don't see why it is asking you for it since the toggle function doesn't requiere it. Here you can download my code for reference, i haven't touched it since march 2020 so maybe is already syntax outdated but it should help as reference. https://www.dropbox.com/s/98qdkfgbakavorx/Controllers.zip?dl=0
Lazaro Ambrosio
2 years ago
Im stuck on when I click on the user search view, and it takes me to their profile information correctly; however, the following button is set as unfollow by default even though I am not following them.
BrianVoongLBTA
2 years ago
Lazaro Ambrosio
2 years ago
Thank you, Brain! Yes, it was a database issue in my publicprofile.js. It was configured incorrectly. I was able to resolve it. I am looking forward to finishing this awesome course. Cheers!
Christopher J. Roura
2 years ago
Could you please go over the delegate methods you were referring to to show the correct following state in the Users Search Controller. I have tried implementing a few methods but they all seem to be very buggy and occur after the view loads.
HELP & SUPPORT