Skip to content

Commit 768e248

Browse files
authored
Merge pull request #87 from devxoul/protocol-requirements
Separate protocol requirement methods
2 parents 815598b + b71609e commit 768e248

File tree

1 file changed

+58
-10
lines changed

1 file changed

+58
-10
lines changed

Sources/URLNavigator/NavigatorType.swift

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,45 @@ public protocol NavigatorType {
3434
/// - returns: A matching handler factory or `nil` if not matched.
3535
func handler(for url: URLConvertible, context: Any?) -> URLOpenHandler?
3636

37+
/// Pushes a matching view controller to the navigation controller stack.
38+
///
39+
/// - note: It is not a good idea to use this method directly because this method requires all
40+
/// parameters. This method eventually gets called when pushing a view controller with
41+
/// an URL, so it's recommended to implement this method only for mocking.
3742
@discardableResult
38-
func push(_ url: URLConvertible, context: Any?, from: UINavigationControllerType?, animated: Bool) -> UIViewController?
43+
func pushURL(_ url: URLConvertible, context: Any?, from: UINavigationControllerType?, animated: Bool) -> UIViewController?
3944

45+
/// Pushes the view controller to the navigation controller stack.
46+
///
47+
/// - note: It is not a good idea to use this method directly because this method requires all
48+
/// parameters. This method eventually gets called when pushing a view controller, so
49+
/// it's recommended to implement this method only for mocking.
4050
@discardableResult
41-
func push(_ viewController: UIViewController, from: UINavigationControllerType?, animated: Bool) -> UIViewController?
51+
func pushViewController(_ viewController: UIViewController, from: UINavigationControllerType?, animated: Bool) -> UIViewController?
4252

53+
/// Presents a matching view controller.
54+
///
55+
/// - note: It is not a good idea to use this method directly because this method requires all
56+
/// parameters. This method eventually gets called when presenting a view controller with
57+
/// an URL, so it's recommended to implement this method only for mocking.
4358
@discardableResult
44-
func present(_ url: URLConvertible, context: Any?, wrap: UINavigationController.Type?, from: UIViewControllerType?, animated: Bool, completion: (() -> Void)?) -> UIViewController?
59+
func presentURL(_ url: URLConvertible, context: Any?, wrap: UINavigationController.Type?, from: UIViewControllerType?, animated: Bool, completion: (() -> Void)?) -> UIViewController?
4560

61+
/// Presents the view controller.
62+
///
63+
/// - note: It is not a good idea to use this method directly because this method requires all
64+
/// parameters. This method eventually gets called when presenting a view controller, so
65+
/// it's recommended to implement this method only for mocking.
4666
@discardableResult
47-
func present(_ viewController: UIViewController, wrap: UINavigationController.Type?, from: UIViewControllerType?, animated: Bool, completion: (() -> Void)?) -> UIViewController?
67+
func presentViewController(_ viewController: UIViewController, wrap: UINavigationController.Type?, from: UIViewControllerType?, animated: Bool, completion: (() -> Void)?) -> UIViewController?
4868

4969
@discardableResult
5070
func open(_ url: URLConvertible, context: Any?) -> Bool
5171
}
5272

73+
74+
// MARK: - Protocol Requirements
75+
5376
extension NavigatorType {
5477
public func viewController(for url: URLConvertible) -> UIViewController? {
5578
return self.viewController(for: url, context: nil)
@@ -60,13 +83,13 @@ extension NavigatorType {
6083
}
6184

6285
@discardableResult
63-
public func push(_ url: URLConvertible, context: Any? = nil, from: UINavigationControllerType? = nil, animated: Bool = true) -> UIViewController? {
86+
public func pushURL(_ url: URLConvertible, context: Any? = nil, from: UINavigationControllerType? = nil, animated: Bool = true) -> UIViewController? {
6487
guard let viewController = self.viewController(for: url, context: context) else { return nil }
65-
return self.push(viewController, from: from, animated: animated)
88+
return self.pushViewController(viewController, from: from, animated: animated)
6689
}
6790

6891
@discardableResult
69-
public func push(_ viewController: UIViewController, from: UINavigationControllerType? = nil, animated: Bool = true) -> UIViewController? {
92+
public func pushViewController(_ viewController: UIViewController, from: UINavigationControllerType?, animated: Bool) -> UIViewController? {
7093
guard (viewController is UINavigationController) == false else { return nil }
7194
guard let navigationController = from ?? UIViewController.topMost?.navigationController else { return nil }
7295
guard self.delegate?.shouldPush(viewController: viewController, from: navigationController) != false else { return nil }
@@ -75,13 +98,13 @@ extension NavigatorType {
7598
}
7699

77100
@discardableResult
78-
public func present(_ url: URLConvertible, context: Any? = nil, wrap: UINavigationController.Type? = nil, from: UIViewControllerType? = nil, animated: Bool = true, completion: (() -> Void)? = nil) -> UIViewController? {
101+
public func presentURL(_ url: URLConvertible, context: Any? = nil, wrap: UINavigationController.Type? = nil, from: UIViewControllerType? = nil, animated: Bool = true, completion: (() -> Void)? = nil) -> UIViewController? {
79102
guard let viewController = self.viewController(for: url, context: context) else { return nil }
80-
return self.present(viewController, wrap: wrap, from: from, animated: animated, completion: completion)
103+
return self.presentViewController(viewController, wrap: wrap, from: from, animated: animated, completion: completion)
81104
}
82105

83106
@discardableResult
84-
public func present(_ viewController: UIViewController, wrap: UINavigationController.Type? = nil, from: UIViewControllerType? = nil, animated: Bool = true, completion: (() -> Void)? = nil) -> UIViewController? {
107+
public func presentViewController(_ viewController: UIViewController, wrap: UINavigationController.Type?, from: UIViewControllerType?, animated: Bool, completion: (() -> Void)?) -> UIViewController? {
85108
guard let fromViewController = from ?? UIViewController.topMost else { return nil }
86109

87110
let viewControllerToPresent: UIViewController
@@ -102,4 +125,29 @@ extension NavigatorType {
102125
return handler()
103126
}
104127
}
128+
129+
130+
// MARK: - Syntactic Sugars for Optional Parameters
131+
132+
extension NavigatorType {
133+
@discardableResult
134+
public func push(_ url: URLConvertible, context: Any? = nil, from: UINavigationControllerType? = nil, animated: Bool = true) -> UIViewController? {
135+
return self.pushURL(url, context: context, from: from, animated: animated)
136+
}
137+
138+
@discardableResult
139+
public func push(_ viewController: UIViewController, from: UINavigationControllerType? = nil, animated: Bool = true) -> UIViewController? {
140+
return self.pushViewController(viewController, from: from, animated: animated)
141+
}
142+
143+
@discardableResult
144+
public func present(_ url: URLConvertible, context: Any? = nil, wrap: UINavigationController.Type? = nil, from: UIViewControllerType? = nil, animated: Bool = true, completion: (() -> Void)? = nil) -> UIViewController? {
145+
return self.presentURL(url, context: context, wrap: wrap, from: from, animated: animated, completion: completion)
146+
}
147+
148+
@discardableResult
149+
public func present(_ viewController: UIViewController, wrap: UINavigationController.Type? = nil, from: UIViewControllerType? = nil, animated: Bool = true, completion: (() -> Void)? = nil) -> UIViewController? {
150+
return self.presentViewController(viewController, wrap: wrap, from: from, animated: animated, completion: completion)
151+
}
152+
}
105153
#endif

0 commit comments

Comments
 (0)