Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added optional param to immediately dismiss after selecting location from SearchResultVC and an optional close navBar button item. #74

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ build/*
!default.xcworkspace
xcuserdata
profile
.DS_Store
**/.DS_Store

12 changes: 8 additions & 4 deletions LocationPicker.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -287,13 +287,15 @@
CLANG_ENABLE_MODULES = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "";
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
DYLIB_COMPATIBILITY_VERSION = 2;
DYLIB_CURRENT_VERSION = 2;
DYLIB_INSTALL_NAME_BASE = "@rpath";
INFOPLIST_FILE = LocationPicker/Info.plist;
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
MARKETING_VERSION = 1.4.3;
MODULE_VERSION = 2;
PRODUCT_BUNDLE_IDENTIFIER = "com.almassapargali.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
Expand All @@ -308,13 +310,15 @@
CLANG_ENABLE_MODULES = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "";
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
DYLIB_COMPATIBILITY_VERSION = 2;
DYLIB_CURRENT_VERSION = 2;
DYLIB_INSTALL_NAME_BASE = "@rpath";
INFOPLIST_FILE = LocationPicker/Info.plist;
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
MARKETING_VERSION = 1.4.3;
MODULE_VERSION = 2;
PRODUCT_BUNDLE_IDENTIFIER = "com.almassapargali.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
Expand Down
2 changes: 1 addition & 1 deletion LocationPicker/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.4.2</string>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
72 changes: 64 additions & 8 deletions LocationPicker/LocationPickerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ open class LocationPickerViewController: UIViewController {
}

public var completion: ((Location?) -> ())?

/// If this is true, the LocationPickerViewController will dismiss as soon as the user select a location from tableView search results. ie without showing the coordinates on a map.
/// Default is false.
public var dismissImmediatelyAfterTableViewSelection = false

/// If this is true, a close/Done button will be shown on the top right corner of the navigationBar. When a button is tapped it will simply dismiss the picker without passing any data.
public var showCloseButtonOnNavBar = false

// region distance to be used for creation region when user selects place from search results
public var resultRegionDistance: CLLocationDistance = 600
Expand Down Expand Up @@ -145,6 +152,7 @@ open class LocationPickerViewController: UIViewController {

open override func viewDidLoad() {
super.viewDidLoad()
addCancelNavBarButtonIfNeeded()

if #available(iOS 13.0, *), let navigationController = navigationController {
let appearance = navigationController.navigationBar.standardAppearance
Expand Down Expand Up @@ -340,15 +348,63 @@ extension LocationPickerViewController: UISearchResultsUpdating {
}

func selectedLocation(_ location: Location) {
// dismiss search results
dismiss(animated: true) {
// set location, this also adds annotation
self.location = location
self.showCoordinates(location.coordinate)
self.historyManager.addToHistory(location)
}
DispatchQueue.main.async { [weak self] in
guard let self = self else {return}
if self.dismissImmediatelyAfterTableViewSelection {
self.updateCoordinatesAndDismissBothSearchResultsAndPicker(location)
} else {
self.dismissSearchResultsAndUpdateCoordinatesOnMap(location, completion: {})
}
}
}

private func updateCoordinatesAndDismissBothSearchResultsAndPicker(_ location: Location) {
dismissSearchResultsAndUpdateCoordinatesOnMap(location,completion: {
self.completion?(location)
// Dismiss picker.
self.dismissOrPopSelf()
})
}
private func dismissSearchResultsAndUpdateCoordinatesOnMap(_ location: Location,completion:@escaping() -> Void){
// dismiss search results
dismiss(animated: true) {
// set location, this also adds annotation
self.location = location
self.showCoordinates(location.coordinate)

self.historyManager.addToHistory(location)
completion()
}

}

private func addCancelNavBarButtonIfNeeded(){

var closeButton:UIBarButtonItem!

if self.showCloseButtonOnNavBar {
if #available(iOS 13.0, *) {
closeButton = UIBarButtonItem(barButtonSystemItem: .close, target: self, action: #selector(dismissOrPopSelf))
} else {
// Fallback on earlier versions
closeButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(dismissOrPopSelf))
}
navigationItem.rightBarButtonItem = closeButton

}
}

@objc private func dismissOrPopSelf(){
DispatchQueue.main.async { [weak self] in
guard let self = self else {return}
if let navigation = self.navigationController, navigation.viewControllers.count > 1 {
navigation.popViewController(animated: true)
} else {
self.presentingViewController?.dismiss(animated: true, completion: nil)
}

}
}
}

// MARK: Selecting location with gesture
Expand Down
Loading