Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions Sources/URLNavigator/NavigatorType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ public protocol NavigatorType {
/// parameters. This method eventually gets called when pushing a view controller with
/// an URL, so it's recommended to implement this method only for mocking.
@discardableResult
func pushURL(_ url: URLConvertible, context: Any?, from: UINavigationControllerType?, animated: Bool) -> UIViewController?
func pushURL(_ url: URLConvertible, context: Any?, from: UINavigationControllerType?, animated: Bool, completion: (() -> Void)?) -> UIViewController?

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

/// Presents a matching view controller.
///
Expand Down Expand Up @@ -86,19 +86,22 @@ extension NavigatorType {
public func handler(for url: URLConvertible) -> URLOpenHandler? {
return self.handler(for: url, context: nil)
}

@discardableResult
public func pushURL(_ url: URLConvertible, context: Any? = nil, from: UINavigationControllerType? = nil, animated: Bool = true) -> UIViewController? {
public func pushURL(_ url: URLConvertible, context: Any? = nil, from: UINavigationControllerType? = nil, animated: Bool = true, completion: (() -> Void)? = nil) -> UIViewController? {
guard let viewController = self.viewController(for: url, context: context) else { return nil }
return self.pushViewController(viewController, from: from, animated: animated)
return self.pushViewController(viewController, from: from, animated: animated, completion: completion)
}

@discardableResult
public func pushViewController(_ viewController: UIViewController, from: UINavigationControllerType?, animated: Bool) -> UIViewController? {
public func pushViewController(_ viewController: UIViewController, from: UINavigationControllerType?, animated: Bool, completion: (() -> Void)?) -> UIViewController? {
guard (viewController is UINavigationController) == false else { return nil }
guard let navigationController = from ?? UIViewController.topMost?.navigationController else { return nil }
guard self.delegate?.shouldPush(viewController: viewController, from: navigationController) != false else { return nil }
CATransaction.begin()
CATransaction.setCompletionBlock(completion)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this trick safe?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's safe, i reference from SwifterSwift library.

navigationController.pushViewController(viewController, animated: animated)
CATransaction.commit()
return viewController
}

Expand Down Expand Up @@ -136,13 +139,13 @@ extension NavigatorType {

extension NavigatorType {
@discardableResult
public func push(_ url: URLConvertible, context: Any? = nil, from: UINavigationControllerType? = nil, animated: Bool = true) -> UIViewController? {
return self.pushURL(url, context: context, from: from, animated: animated)
public func push(_ url: URLConvertible, context: Any? = nil, from: UINavigationControllerType? = nil, animated: Bool = true, completion: (() -> Void)? = nil) -> UIViewController? {
return self.pushURL(url, context: context, from: from, animated: animated, completion: completion)
}

@discardableResult
public func push(_ viewController: UIViewController, from: UINavigationControllerType? = nil, animated: Bool = true) -> UIViewController? {
return self.pushViewController(viewController, from: from, animated: animated)
public func push(_ viewController: UIViewController, from: UINavigationControllerType? = nil, animated: Bool = true, completion: (() -> Void)? = nil) -> UIViewController? {
return self.pushViewController(viewController, from: from, animated: animated, completion: completion)
}

@discardableResult
Expand Down