Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
insub4067 committed Oct 14, 2023
1 parent 65d3b8d commit 4b59742
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions Sources/SwiftUINavigator/Navigator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import UIKit
public class Navigator {

public weak var viewController: UIViewController?
var navigationController: UINavigationController? {
viewController?.navigationController
}

public func set(_ viewController: UIViewController?) {
self.viewController = viewController
Expand All @@ -26,52 +29,51 @@ public extension Navigator {

func present(_ destination: UIViewController, style: UIModalPresentationStyle = .fullScreen, animated: Bool = true) {
destination.modalPresentationStyle = style
self.viewController?.navigationController?.topViewController?.present(destination, animated: animated)
navigationController?.topViewController?.present(destination, animated: animated)
}
}

// Dismiss, Dismiss
public extension Navigator {

func popToRootViewController(_ animated: Bool = true) {
viewController?.navigationController?.popToRootViewController(animated: animated)
navigationController?.popToRootViewController(animated: animated)
}

func pop(animated: Bool = true) {
self.viewController?.navigationController?.popViewController(animated: animated)
navigationController?.popViewController(animated: animated)
}

func pop(to controller: AnyClass, animated: Bool = true) {

let target = self.viewController?.navigationController?.viewControllers
let target = self.navigationController?
.viewControllers
.filter { $0.isMember(of: controller) }
.first

guard let target = target else { return }
self.viewController?.navigationController?.popToViewController(target, animated: animated)
popToViewController(target, animated: animated)
}

func pop(to identifier: String, animated: Bool = true) {
let target = self.viewController?.navigationController?.viewControllers
let target = self.navigationController?
.viewControllers
.filter { $0.identifier == identifier }
.first

guard let target = target else { return }
self.viewController?.navigationController?.popToViewController(target, animated: animated)
popToViewController(target, animated: animated)
}

func pop(toOneOf controllers: AnyClass..., animated: Bool = true) {

guard let viewControllers = self
.viewController?
.navigationController?
.viewControllers
guard let viewControllers = navigationController?.viewControllers
else { return }

for controller in viewControllers {
let shouldPop = controllers.contains(where: { controller.isMember(of: $0) })
guard shouldPop else { continue }
self.viewController?.navigationController?.popToViewController(controller, animated: animated)
popToViewController(controller, animated: animated)
}
}

Expand All @@ -88,15 +90,23 @@ public extension Navigator {
.first

guard let target = target else { return }
self.viewController?.navigationController?.popToViewController(target, animated: animated)
popToViewController(target, animated: animated)
}

func popToViewController(_ controller: UIViewController, animated: Bool = true) {
navigationController?.popToViewController(controller, animated: animated)
}


func dismiss(animated: Bool = true) {
self.viewController?.dismiss(animated: animated)
viewController?.dismiss(animated: animated)
}

func removeFromParentAndSuperView() {
self.viewController?.navigationController?.topViewController?.removeFromParentAndSuperView()
viewController?
.navigationController?
.topViewController?
.removeFromParentAndSuperView()
}
}
#endif

0 comments on commit 4b59742

Please sign in to comment.