Skip to content

Commit

Permalink
Merge pull request #11 from kizitonwose/presentation-delegate
Browse files Browse the repository at this point in the history
Add view controller presentation delegates
  • Loading branch information
kizitonwose authored Jul 1, 2018
2 parents 10bd3b9 + 349625c commit 7df589f
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 32 deletions.
12 changes: 9 additions & 3 deletions CountryPickerView/CountryPickerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,16 @@ public class CountryPickerView: NibView {
let countryVc = CountryPickerViewController(style: .grouped)
countryVc.countryPickerView = self
if let viewController = viewController as? UINavigationController {
viewController.pushViewController(countryVc, animated: true)
delegate?.countryPickerView(self, willShow: countryVc)
viewController.pushViewController(countryVc, animated: true) {
self.delegate?.countryPickerView(self, didShow: countryVc)
}
} else {
viewController.present(UINavigationController(rootViewController: countryVc),
animated: true)
let navigationVC = UINavigationController(rootViewController: countryVc)
delegate?.countryPickerView(self, willShow: countryVc)
viewController.present(navigationVC, animated: true) {
self.delegate?.countryPickerView(self, didShow: countryVc)
}
}
}

Expand Down
24 changes: 16 additions & 8 deletions CountryPickerView/CountryPickerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ class CountryPickerViewController: UITableViewController {

}


// UI Setup
extension CountryPickerViewController {

Expand Down Expand Up @@ -104,7 +103,9 @@ extension CountryPickerViewController {
searchController?.searchResultsUpdater = self
searchController?.dimsBackgroundDuringPresentation = false
searchController?.hidesNavigationBarDuringPresentation = searchBarPosition == .tableViewHeader
searchController?.definesPresentationContext = true
searchController?.searchBar.delegate = self
searchController?.delegate = self

switch searchBarPosition {
case .tableViewHeader: tableView.tableHeaderView = searchController?.searchBar
Expand All @@ -118,7 +119,6 @@ extension CountryPickerViewController {
}
}


//MARK:- UITableViewDataSource
extension CountryPickerViewController {

Expand Down Expand Up @@ -164,7 +164,6 @@ extension CountryPickerViewController {
}
}


//MARK:- UITableViewDelegate
extension CountryPickerViewController {

Expand Down Expand Up @@ -193,10 +192,9 @@ extension CountryPickerViewController {
}
}


// MARK:- UISearchResultsUpdating
extension CountryPickerViewController: UISearchResultsUpdating {
public func updateSearchResults(for searchController: UISearchController) {
func updateSearchResults(for searchController: UISearchController) {
isSearchMode = false
if let text = searchController.searchBar.text, text.count > 0 {
isSearchMode = true
Expand All @@ -217,21 +215,31 @@ extension CountryPickerViewController: UISearchResultsUpdating {
}
}


// MARK:- UISearchBarDelegate
extension CountryPickerViewController: UISearchBarDelegate {
public func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
// Hide the back/left navigationItem button
navigationItem.leftBarButtonItem = nil
navigationItem.hidesBackButton = true
}

public func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
// Show the back/left navigationItem button
prepareNavItem()
navigationItem.hidesBackButton = false
}

}

// MARK:- UISearchControllerDelegate
// Fixes an issue where the search bar goes off screen sometimes.
extension CountryPickerViewController: UISearchControllerDelegate {
func willPresentSearchController(_ searchController: UISearchController) {
self.navigationController?.navigationBar.isTranslucent = true
}

func willDismissSearchController(_ searchController: UISearchController) {
self.navigationController?.navigationBar.isTranslucent = false
}
}

25 changes: 25 additions & 0 deletions CountryPickerView/Delegate+DataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ import Foundation
public protocol CountryPickerViewDelegate: class {
/// Called when the user selects a country from the list.
func countryPickerView(_ countryPickerView: CountryPickerView, didSelectCountry country: Country)

/// Called before the internal UITableViewController is presented or pushed.
/// If the presenting view controller is not a UINavigationController, the UITableViewController
/// is embedded in a UINavigationController.
func countryPickerView(_ countryPickerView: CountryPickerView, willShow viewController: UITableViewController)

/// Called after the internal UITableViewController is presented or pushed.
/// If the presenting view controller is not a UINavigationController, the UITableViewController
/// is embedded in a UINavigationController.
func countryPickerView(_ countryPickerView: CountryPickerView, didShow viewController: UITableViewController)
}

public protocol CountryPickerViewDataSource: class {
Expand Down Expand Up @@ -72,3 +82,18 @@ public extension CountryPickerViewDataSource {
return false
}
}


// MARK:- CountryPickerViewDelegate default implementations
public extension CountryPickerViewDelegate {

func countryPickerView(_ countryPickerView: CountryPickerView, willShow viewController: UITableViewController) {

}

func countryPickerView(_ countryPickerView: CountryPickerView, didShow viewController: UITableViewController) {

}

}

12 changes: 12 additions & 0 deletions CountryPickerView/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,16 @@ extension UINavigationController {
completion()
}
}

func pushViewController(_ viewController: UIViewController, animated: Bool, completion: @escaping () -> Void) {
pushViewController(viewController, animated: animated)

if animated, let coordinator = transitionCoordinator {
coordinator.animate(alongsideTransition: nil) { _ in
completion()
}
} else {
completion()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@
004CA2FC1F705DD600B690B8 /* Frameworks */,
004CA2FD1F705DD600B690B8 /* Resources */,
D073A94C45743AB4CB4D944D /* [CP] Embed Pods Frameworks */,
B81A81F5EA364DFEE7FEEDCA /* [CP] Copy Pods Resources */,
509720AE1FDEE189001244B0 /* Embed Frameworks */,
);
buildRules = (
Expand Down Expand Up @@ -174,21 +173,6 @@
/* End PBXResourcesBuildPhase section */

/* Begin PBXShellScriptBuildPhase section */
B81A81F5EA364DFEE7FEEDCA /* [CP] Copy Pods Resources */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
);
name = "[CP] Copy Pods Resources";
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-CountryPickerViewDemo/Pods-CountryPickerViewDemo-resources.sh\"\n";
showEnvVarsInLog = 0;
};
D073A94C45743AB4CB4D944D /* [CP] Embed Pods Frameworks */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
Expand Down Expand Up @@ -372,7 +356,7 @@
isa = XCBuildConfiguration;
baseConfigurationReference = 459706724723319B379BF2E4 /* Pods-CountryPickerViewDemo.debug.xcconfig */;
buildSettings = {
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = "$(inherited)";
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = K3JWY7DT4K;
INFOPLIST_FILE = CountryPickerViewDemo/Info.plist;
Expand All @@ -388,7 +372,7 @@
isa = XCBuildConfiguration;
baseConfigurationReference = 476008F23554AD41FFB937C1 /* Pods-CountryPickerViewDemo.release.xcconfig */;
buildSettings = {
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = "$(inherited)";
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = K3JWY7DT4K;
INFOPLIST_FILE = CountryPickerViewDemo/Info.plist;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
6 changes: 3 additions & 3 deletions CountryPickerViewDemo/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ DEPENDENCIES:

EXTERNAL SOURCES:
CountryPickerView:
:path: ../
:path: "../"

SPEC CHECKSUMS:
CountryPickerView: db05b3dbd2c6bd4c04c388ee53b5bedf8d84a5fd
CountryPickerView: f23e96fe13908b00b861d5dd18c3688632500bdc

PODFILE CHECKSUM: 649829ad9a32abd953e33364dc48a4a40a615047

COCOAPODS: 1.3.1
COCOAPODS: 1.5.3

0 comments on commit 7df589f

Please sign in to comment.