Skip to content

Commit

Permalink
Delegates and protocols 1/2 github profile
Browse files Browse the repository at this point in the history
  • Loading branch information
grenos committed Apr 5, 2020
1 parent f16d6d7 commit e9ff529
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 10 deletions.
6 changes: 3 additions & 3 deletions GHFollowers.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,14 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
7D7C66592438A2B300733687 /* ItemInfoVCs */ = {
7D7C66592438A2B300733687 /* ItemInfoVCs-SuperClass+SubClass */ = {
isa = PBXGroup;
children = (
7D7C6653243897F300733687 /* GFItemInfoVC.swift */,
7D7C665524389E0600733687 /* GFRepoItemVC.swift */,
7D7C66572438A22C00733687 /* GFFollowerItemVC.swift */,
);
path = ItemInfoVCs;
path = "ItemInfoVCs-SuperClass+SubClass";
sourceTree = "<group>";
};
7D98BB1B2428FE73006C53E9 = {
Expand Down Expand Up @@ -258,7 +258,7 @@
7DA45F7D2433DCE700AB426F /* ViewControllers */ = {
isa = PBXGroup;
children = (
7D7C66592438A2B300733687 /* ItemInfoVCs */,
7D7C66592438A2B300733687 /* ItemInfoVCs-SuperClass+SubClass */,
7D98BB49242A55ED006C53E9 /* GFAlertVC.swift */,
7DA45F842433FAF800AB426F /* GFUserInfoheaderVC.swift */,
);
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class GFItemInfoVC: UIViewController {
let actionButton = GFButton()

var user: User!
var delegate: UserInfoVCDelegate!


// create a custom initializer
// so when we initialize the VC we can pass the User object
Expand All @@ -35,6 +37,7 @@ class GFItemInfoVC: UIViewController {
configureBackgroundView()
layoutUI()
configureStackView()
configureActionButton()
}


Expand All @@ -51,7 +54,16 @@ class GFItemInfoVC: UIViewController {
stackView.addArrangedSubview(itemInfoView1)
stackView.addArrangedSubview(itemInfoView2)
}



// Add button actions
private func configureActionButton() {
actionButton.addTarget(self, action: #selector(actionButtonTapped), for: .touchUpInside)
}
// this is empty because we are going to override and call it in the subClasses repos/followers
@objc func actionButtonTapped() {}



private func layoutUI() {
view.addSubview(stackView)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,11 @@ class GFRepoItemVC: GFItemInfoVC {
itemInfoView2.set(itemInfoType: .gists, withCount: user.publicGists)
actionButton.set(backgroundColor: .systemPurple, title: "GitHub Profile")
}


// call the func from the parent class
override func actionButtonTapped() {
// call the didTapGithubProfile func declared in the delegate UserInfoVC
delegate.didTapGithubProfile(for: user)
}
}
61 changes: 55 additions & 6 deletions GHFollowers/Screens/UserInfoVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
//

import UIKit
import SafariServices

// setup the protocol for the delegate
protocol UserInfoVCDelegate: class {
func didTapGithubProfile(for user: User)
func didTapGetFollowers(for user: User)
}


class UserInfoVC: UIViewController {

Expand Down Expand Up @@ -49,12 +57,7 @@ class UserInfoVC: UIViewController {

switch result {
case .success(let user):
DispatchQueue.main.async {
self.add(childVC: GFUserInfoheaderVC(user: user), to: self.headerView)
self.add(childVC: GFRepoItemVC(user: user), to: self.itemViewOne)
self.add(childVC: GFFollowerItemVC(user: user), to: self.itemViewTwo)
self.dateLabel.text = "GitHub since \(user.createdAt.convertToDisplayFormat())"
}
DispatchQueue.main.async { self.configureUIElements(with: user) }

case .failure(let error):
self.presentGFAlertOnMainThread(title: "Something went wrong", message: error.rawValue, buttonTitle: "Ok")
Expand All @@ -64,6 +67,22 @@ class UserInfoVC: UIViewController {



func configureUIElements(with user: User) {

let repoItemVC = GFRepoItemVC(user: user)
repoItemVC.delegate = self

let followerItemVC = GFFollowerItemVC(user: user)
followerItemVC.delegate = self

self.add(childVC: GFUserInfoheaderVC(user: user), to: self.headerView)
self.add(childVC: repoItemVC, to: self.itemViewOne)
self.add(childVC: followerItemVC, to: self.itemViewTwo)
self.dateLabel.text = "GitHub since \(user.createdAt.convertToDisplayFormat())"
}



// MARK: UI layout
func layoutUI() {
itemViews = [headerView, itemViewOne, itemViewTwo, dateLabel]
Expand Down Expand Up @@ -105,3 +124,33 @@ class UserInfoVC: UIViewController {


}



// MARK: Delegates
// we set the ItemInfoVC as the delegate

// Conform to the protocol
extension UserInfoVC: UserInfoVCDelegate {
// this function is called in the GFRepoItemVC
func didTapGithubProfile(for user: User) {
// show safari view controller
guard let url = URL(string: user.htmlUrl) else {
presentGFAlertOnMainThread(title: "Invalid URL.", message: "The url attached to this user is invalid.", buttonTitle: "Ok")

return
}

let safariVC = SFSafariViewController(url: url)
safariVC.preferredControlTintColor = .systemGreen
present(safariVC, animated: true)
}



// this function is called in the GFFOllowerItemVC
func didTapGetFollowers(for user: User) {
// Dismiss this VC
// Pass the follower list VC the new username so it can search for followers
}
}

0 comments on commit e9ff529

Please sign in to comment.