diff --git a/Example/Base.lproj/Main.storyboard b/Example/Base.lproj/Main.storyboard index f760844..7d15766 100644 --- a/Example/Base.lproj/Main.storyboard +++ b/Example/Base.lproj/Main.storyboard @@ -1,5 +1,5 @@ - + @@ -26,23 +26,5 @@ - - - - - - - - - - - - - - - - - - diff --git a/Example/ViewController.swift b/Example/ViewController.swift index a5a8e9f..665d786 100644 --- a/Example/ViewController.swift +++ b/Example/ViewController.swift @@ -21,7 +21,7 @@ class ViewController: FormViewController { row.placeholder = "Location" }.onPresent({ (_, presentingViewController) in presentingViewController.title = "Location" - presentingViewController.placeholder = "Enter Location" + presentingViewController.searchPlaceholder = "Enter Location" }) } } diff --git a/LocationRow.xcodeproj/project.pbxproj b/LocationRow.xcodeproj/project.pbxproj index da27388..a9512d6 100644 --- a/LocationRow.xcodeproj/project.pbxproj +++ b/LocationRow.xcodeproj/project.pbxproj @@ -8,7 +8,6 @@ /* Begin PBXBuildFile section */ 2823556C221CC5B400DF4771 /* LocationRow.h in Headers */ = {isa = PBXBuildFile; fileRef = 2823556A221CC5B400DF4771 /* LocationRow.h */; settings = {ATTRIBUTES = (Public, ); }; }; - 28235575221CC62100DF4771 /* LocationCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 28235572221CC62100DF4771 /* LocationCell.xib */; }; 28235576221CC62100DF4771 /* LocationRow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 28235573221CC62100DF4771 /* LocationRow.swift */; }; 28235577221CC62100DF4771 /* LocationViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 28235574221CC62100DF4771 /* LocationViewController.swift */; }; 2823557A221CC80700DF4771 /* Eureka.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 28235579221CC80700DF4771 /* Eureka.framework */; }; @@ -20,7 +19,6 @@ 28235567221CC5B400DF4771 /* LocationRow.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = LocationRow.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 2823556A221CC5B400DF4771 /* LocationRow.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = LocationRow.h; sourceTree = ""; }; 2823556B221CC5B400DF4771 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; - 28235572221CC62100DF4771 /* LocationCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = LocationCell.xib; sourceTree = ""; }; 28235573221CC62100DF4771 /* LocationRow.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LocationRow.swift; sourceTree = ""; }; 28235574221CC62100DF4771 /* LocationViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LocationViewController.swift; sourceTree = ""; }; 28235579221CC80700DF4771 /* Eureka.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Eureka.framework; path = Carthage/Build/iOS/Eureka.framework; sourceTree = ""; }; @@ -60,7 +58,6 @@ 28235569221CC5B400DF4771 /* LocationRow */ = { isa = PBXGroup; children = ( - 28235572221CC62100DF4771 /* LocationCell.xib */, 28235573221CC62100DF4771 /* LocationRow.swift */, 28235574221CC62100DF4771 /* LocationViewController.swift */, 2823556A221CC5B400DF4771 /* LocationRow.h */, @@ -150,7 +147,6 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( - 28235575221CC62100DF4771 /* LocationCell.xib in Resources */, 28235598221D006500DF4771 /* Assets.xcassets in Resources */, 2823557C221CCD8B00DF4771 /* LocationViewController.xib in Resources */, ); diff --git a/LocationRow/LocationCell.xib b/LocationRow/LocationCell.xib deleted file mode 100644 index 74fbb12..0000000 --- a/LocationRow/LocationCell.xib +++ /dev/null @@ -1,42 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/LocationRow/LocationRow.swift b/LocationRow/LocationRow.swift index d6b310e..ad448ae 100644 --- a/LocationRow/LocationRow.swift +++ b/LocationRow/LocationRow.swift @@ -5,43 +5,60 @@ import Eureka import Foundation public final class LocationCell: PushSelectorCell { - @IBOutlet public var clearButton: UIButton! { - didSet { - clearButton.setImage(UIImage(named: "Clear", in: Bundle.current, compatibleWith: nil)?.withRenderingMode(.alwaysTemplate), for: .normal) - clearButton.tintColor = .lightGray - } - } + @IBOutlet public weak var clearButton: UIButton! required init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) + + let clearButton = UIButton(frame: CGRect(x: 0, y: 0, width: 18, height: 18)) + clearButton.setImage(UIImage(named: "Clear", in: Bundle.current, compatibleWith: nil)?.withRenderingMode(.alwaysTemplate), for: .normal) + clearButton.tintColor = .lightGray + + self.clearButton = clearButton + + accessoryView = clearButton + editingAccessoryView = accessoryView } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } + deinit { + clearButton?.removeTarget(self, action: nil, for: .allEvents) + } + + public override func setup() { + super.setup() + + clearButton.addTarget(self, action: #selector(LocationCell.clear), for: .touchUpInside) + } + public override func update() { super.update() detailTextLabel?.text = nil - accessoryType = .none guard let row = self.row as? LocationRow else { return } + let isEmpty = row.value?.name?.isEmpty ?? true + textLabel?.text = row.value?.name ?? row.placeholder - textLabel?.textColor = row.value?.name != nil ? .black : UIColor(red: 198 / 255, green: 198 / 255, blue: 204 / 255, alpha: 1) + textLabel?.textColor = isEmpty ? UIColor(red: 198 / 255, green: 198 / 255, blue: 204 / 255, alpha: 1) : row.isDisabled ? .gray : .black - clearButton.isHidden = row.value?.name == nil + clearButton.isHidden = isEmpty + clearButton.isEnabled = !row.isDisabled } - @IBAction func clear(_: Any) { + @objc + func clear() { row.value = nil update() } } public final class LocationRow: Row, RowType, PresenterRowType { - public typealias PresenterRow = LocationViewController + public typealias PresenterRow = LocationNavigationController public var presentationMode: PresentationMode? public var onPresentCallback: ((FormViewController, PresenterRow) -> Void)? @@ -50,35 +67,34 @@ public final class LocationRow: Row, RowType, PresenterRowType { public required init(tag: String?) { super.init(tag: tag) - - cellProvider = CellProvider(nibName: "LocationCell", bundle: Bundle.current) } public override func customDidSelect() { super.customDidSelect() - guard let controller = makeController() else { return } + guard !isDisabled else { return } - presentationMode = PresentationMode.show(controllerProvider: ControllerProvider.callback { - controller - }, onDismiss: { viewController in - _ = viewController.navigationController?.popViewController(animated: true) + presentationMode = PresentationMode.presentModally(controllerProvider: ControllerProvider.callback { + let bar = LocationViewController(nibName: "LocationViewController", bundle: Bundle.current) + return LocationNavigationController(rootViewController: bar) + }, onDismiss: { viewController in + viewController.presentingViewController?.dismiss(animated: true) }) guard let presentationMode = presentationMode, !isDisabled else { return } if let controller = presentationMode.makeController() { controller.row = self + onPresentCallback?(cell.formViewController()!, controller) + + controller.topViewController?.title = controller.title + presentationMode.present(controller, row: self, presentingController: cell.formViewController()!) } else { presentationMode.present(nil, row: self, presentingController: cell.formViewController()!) } } - - private func makeController() -> LocationViewController? { - return LocationViewController(nibName: "LocationViewController", bundle: Bundle.current) - } } private extension Bundle { diff --git a/LocationRow/LocationViewController.swift b/LocationRow/LocationViewController.swift index 216555b..e77c6a7 100644 --- a/LocationRow/LocationViewController.swift +++ b/LocationRow/LocationViewController.swift @@ -4,14 +4,13 @@ import Eureka import Foundation import MapKit -public final class LocationViewController: UIViewController, TypedRowControllerType { - public var row: RowOf! - public var onDismissCallback: ((UIViewController) -> Void)? - - public var placeholder: String? - +public final class LocationViewController: UIViewController { @IBOutlet var tableView: UITableView! + var searchBar: UISearchBar? { + return searchController.searchBar + } + private let searchController = UISearchController(searchResultsController: nil) private var searchResults: [MKMapItem] = [] @@ -24,7 +23,7 @@ public final class LocationViewController: UIViewController, TypedRowControllerT searchController.obscuresBackgroundDuringPresentation = false searchController.hidesNavigationBarDuringPresentation = false - searchController.searchBar.placeholder = placeholder ?? "Enter Location" + navigationItem.setRightBarButton(UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(LocationViewController.dismiss(_:))), animated: false) navigationItem.searchController = searchController navigationItem.hidesSearchBarWhenScrolling = false @@ -35,6 +34,11 @@ public final class LocationViewController: UIViewController, TypedRowControllerT definesPresentationContext = true } + @objc + func dismiss(_ sender: Any) { + (navigationController as? LocationNavigationController)?.onDismissCallback?(self) + } + private func address(for placemark: MKPlacemark) -> String { let address: [String?] = [ [placemark.subThoroughfare, placemark.thoroughfare].compactMap { $0 }.joined(separator: " "), @@ -88,8 +92,20 @@ extension LocationViewController: UITableViewDataSource { extension LocationViewController: UITableViewDelegate { public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { - row.value = searchResults[indexPath.row].placemark + guard let navigationController = navigationController as? LocationNavigationController else { return } - navigationController?.popViewController(animated: true) + navigationController.row.value = searchResults[indexPath.row].placemark + navigationController.onDismissCallback?(self) + } +} + +public class LocationNavigationController: UINavigationController, TypedRowControllerType { + public var row: RowOf! + public var onDismissCallback: ((UIViewController) -> Void)? + + public var searchPlaceholder: String? { + didSet { + (topViewController as? LocationViewController)?.searchBar?.placeholder = searchPlaceholder + } } } diff --git a/LocationRow/LocationViewController.xib b/LocationRow/LocationViewController.xib index eaa6d40..73949e3 100644 --- a/LocationRow/LocationViewController.xib +++ b/LocationRow/LocationViewController.xib @@ -12,7 +12,7 @@ - + @@ -21,29 +21,23 @@ - - + - - + + + - +