Skip to content

Commit 4b59742

Browse files
committed
refactor
1 parent 65d3b8d commit 4b59742

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

Sources/SwiftUINavigator/Navigator.swift

+25-15
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import UIKit
1111
public class Navigator {
1212

1313
public weak var viewController: UIViewController?
14+
var navigationController: UINavigationController? {
15+
viewController?.navigationController
16+
}
1417

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

2730
func present(_ destination: UIViewController, style: UIModalPresentationStyle = .fullScreen, animated: Bool = true) {
2831
destination.modalPresentationStyle = style
29-
self.viewController?.navigationController?.topViewController?.present(destination, animated: animated)
32+
navigationController?.topViewController?.present(destination, animated: animated)
3033
}
3134
}
3235

3336
// Dismiss, Dismiss
3437
public extension Navigator {
3538

3639
func popToRootViewController(_ animated: Bool = true) {
37-
viewController?.navigationController?.popToRootViewController(animated: animated)
40+
navigationController?.popToRootViewController(animated: animated)
3841
}
3942

4043
func pop(animated: Bool = true) {
41-
self.viewController?.navigationController?.popViewController(animated: animated)
44+
navigationController?.popViewController(animated: animated)
4245
}
4346

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

46-
let target = self.viewController?.navigationController?.viewControllers
49+
let target = self.navigationController?
50+
.viewControllers
4751
.filter { $0.isMember(of: controller) }
4852
.first
4953

5054
guard let target = target else { return }
51-
self.viewController?.navigationController?.popToViewController(target, animated: animated)
55+
popToViewController(target, animated: animated)
5256
}
5357

5458
func pop(to identifier: String, animated: Bool = true) {
55-
let target = self.viewController?.navigationController?.viewControllers
59+
let target = self.navigationController?
60+
.viewControllers
5661
.filter { $0.identifier == identifier }
5762
.first
5863

5964
guard let target = target else { return }
60-
self.viewController?.navigationController?.popToViewController(target, animated: animated)
65+
popToViewController(target, animated: animated)
6166
}
6267

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

65-
guard let viewControllers = self
66-
.viewController?
67-
.navigationController?
68-
.viewControllers
70+
guard let viewControllers = navigationController?.viewControllers
6971
else { return }
7072

7173
for controller in viewControllers {
7274
let shouldPop = controllers.contains(where: { controller.isMember(of: $0) })
7375
guard shouldPop else { continue }
74-
self.viewController?.navigationController?.popToViewController(controller, animated: animated)
76+
popToViewController(controller, animated: animated)
7577
}
7678
}
7779

@@ -88,15 +90,23 @@ public extension Navigator {
8890
.first
8991

9092
guard let target = target else { return }
91-
self.viewController?.navigationController?.popToViewController(target, animated: animated)
93+
popToViewController(target, animated: animated)
94+
}
95+
96+
func popToViewController(_ controller: UIViewController, animated: Bool = true) {
97+
navigationController?.popToViewController(controller, animated: animated)
9298
}
9399

100+
94101
func dismiss(animated: Bool = true) {
95-
self.viewController?.dismiss(animated: animated)
102+
viewController?.dismiss(animated: animated)
96103
}
97104

98105
func removeFromParentAndSuperView() {
99-
self.viewController?.navigationController?.topViewController?.removeFromParentAndSuperView()
106+
viewController?
107+
.navigationController?
108+
.topViewController?
109+
.removeFromParentAndSuperView()
100110
}
101111
}
102112
#endif

0 commit comments

Comments
 (0)