diff --git a/GHFollowers.xcodeproj/project.pbxproj b/GHFollowers.xcodeproj/project.pbxproj index e1fdc26..c99c946 100644 --- a/GHFollowers.xcodeproj/project.pbxproj +++ b/GHFollowers.xcodeproj/project.pbxproj @@ -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 = ""; }; 7D98BB1B2428FE73006C53E9 = { @@ -258,7 +258,7 @@ 7DA45F7D2433DCE700AB426F /* ViewControllers */ = { isa = PBXGroup; children = ( - 7D7C66592438A2B300733687 /* ItemInfoVCs */, + 7D7C66592438A2B300733687 /* ItemInfoVCs-SuperClass+SubClass */, 7D98BB49242A55ED006C53E9 /* GFAlertVC.swift */, 7DA45F842433FAF800AB426F /* GFUserInfoheaderVC.swift */, ); diff --git a/GHFollowers.xcodeproj/project.xcworkspace/xcuserdata/vasilis.xcuserdatad/UserInterfaceState.xcuserstate b/GHFollowers.xcodeproj/project.xcworkspace/xcuserdata/vasilis.xcuserdatad/UserInterfaceState.xcuserstate index 3748e32..48b63f2 100644 Binary files a/GHFollowers.xcodeproj/project.xcworkspace/xcuserdata/vasilis.xcuserdatad/UserInterfaceState.xcuserstate and b/GHFollowers.xcodeproj/project.xcworkspace/xcuserdata/vasilis.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/GHFollowers/Components/ViewControllers/ItemInfoVCs/GFFollowerItemVC.swift b/GHFollowers/Components/ViewControllers/ItemInfoVCs-SuperClass+SubClass/GFFollowerItemVC.swift similarity index 100% rename from GHFollowers/Components/ViewControllers/ItemInfoVCs/GFFollowerItemVC.swift rename to GHFollowers/Components/ViewControllers/ItemInfoVCs-SuperClass+SubClass/GFFollowerItemVC.swift diff --git a/GHFollowers/Components/ViewControllers/ItemInfoVCs/GFItemInfoVC.swift b/GHFollowers/Components/ViewControllers/ItemInfoVCs-SuperClass+SubClass/GFItemInfoVC.swift similarity index 85% rename from GHFollowers/Components/ViewControllers/ItemInfoVCs/GFItemInfoVC.swift rename to GHFollowers/Components/ViewControllers/ItemInfoVCs-SuperClass+SubClass/GFItemInfoVC.swift index 1cf3f32..ba94220 100644 --- a/GHFollowers/Components/ViewControllers/ItemInfoVCs/GFItemInfoVC.swift +++ b/GHFollowers/Components/ViewControllers/ItemInfoVCs-SuperClass+SubClass/GFItemInfoVC.swift @@ -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 @@ -35,6 +37,7 @@ class GFItemInfoVC: UIViewController { configureBackgroundView() layoutUI() configureStackView() + configureActionButton() } @@ -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) diff --git a/GHFollowers/Components/ViewControllers/ItemInfoVCs/GFRepoItemVC.swift b/GHFollowers/Components/ViewControllers/ItemInfoVCs-SuperClass+SubClass/GFRepoItemVC.swift similarity index 82% rename from GHFollowers/Components/ViewControllers/ItemInfoVCs/GFRepoItemVC.swift rename to GHFollowers/Components/ViewControllers/ItemInfoVCs-SuperClass+SubClass/GFRepoItemVC.swift index 23d589c..dd704bc 100644 --- a/GHFollowers/Components/ViewControllers/ItemInfoVCs/GFRepoItemVC.swift +++ b/GHFollowers/Components/ViewControllers/ItemInfoVCs-SuperClass+SubClass/GFRepoItemVC.swift @@ -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) + } } diff --git a/GHFollowers/Screens/UserInfoVC.swift b/GHFollowers/Screens/UserInfoVC.swift index ab7a467..023bd92 100644 --- a/GHFollowers/Screens/UserInfoVC.swift +++ b/GHFollowers/Screens/UserInfoVC.swift @@ -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 { @@ -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") @@ -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] @@ -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 + } +}