Skip to content

Commit 16d55cc

Browse files
committed
feat: Added new method the removeLast & removeLastRoot
1 parent eb1fddb commit 16d55cc

1 file changed

Lines changed: 58 additions & 4 deletions

File tree

Sources/LinkNavigator/Core/LinkNavigator.swift

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,36 @@ public protocol LinkNavigatorType {
254254
/// - paths: An array of ``RouteBuilder/matchPath`` for the specific page.
255255
func rootRemove(paths: [String])
256256

257+
258+
/// Remove last page in pages.
259+
///
260+
/// ```swift
261+
/// // current navigation stack == ["pageA", "pageB", "pageA", "pageC"]
262+
///
263+
/// navigator.removeLast(paths: "pageA", isAnimated: true)
264+
/// // then, you will have this stack, ["pageA", "pageB"]
265+
/// ```
266+
///
267+
/// - Parameters:
268+
/// - path: A string of ``RouteBuilder/matchPath`` for the specific page.
269+
/// - isAnimated: makes the transition to be animated.
270+
func removeLast(path: String, isAnimated: Bool)
271+
272+
/// Remove last page in pages.
273+
///
274+
/// ```swift
275+
/// // current navigation stack == ["pageA", "pageB", "pageA", "pageC"]
276+
///
277+
/// navigator.removeLastRoot(paths: "pageA", isAnimated: true)
278+
/// // then, you will have this stack, ["pageA", "pageB"]
279+
/// ```
280+
/// - Note: If the modal is inactive, this method does same thing as ``remove(paths:)``.
281+
///
282+
/// - Parameters:
283+
/// - path: A string of ``RouteBuilder/matchPath`` for the specific page.
284+
/// - isAnimated: makes the transition to be animated.
285+
func removeLastRoot(path: String, isAnimated: Bool)
286+
257287
/// Dismisses the modal and calls completion closure.
258288
///
259289
/// - Note: If the modal is inactive, this method will be ignored.
@@ -447,7 +477,7 @@ extension LinkNavigator: LinkNavigatorType {
447477

448478
public func backOrNext(path: String, items: [String: String], isAnimated: Bool) {
449479
if isCurrentContain(path: path) {
450-
guard let pick = findViewController(path: path) else { return }
480+
guard let pick = findFirstViewController(path: path) else { return }
451481
currentActivityNavigationController.popToViewController(pick, animated: isAnimated)
452482
return
453483
}
@@ -457,7 +487,7 @@ extension LinkNavigator: LinkNavigatorType {
457487

458488
public func rootBackOrNext(path: String, items: [String: String], isAnimated: Bool) {
459489
if isCurrentContainRootViewController(path: path) {
460-
guard let pick = findViewControllerRootView(path: path) else { return }
490+
guard let pick = findFirstViewControllerRootView(path: path) else { return }
461491
rootNavigationController.popToViewController(pick, animated: isAnimated)
462492
return
463493
}
@@ -495,6 +525,16 @@ extension LinkNavigator: LinkNavigatorType {
495525
rootNavigationController.setViewControllers(new, animated: false)
496526
}
497527

528+
public func removeLast(path: String, isAnimated: Bool) {
529+
guard let pick = findLastViewController(path: path) else { return }
530+
currentActivityNavigationController.popToViewController(pick, animated: isAnimated)
531+
}
532+
533+
public func removeLastRoot(path: String, isAnimated: Bool) {
534+
guard let pick = findLastViewControllerRootView(path: path) else { return }
535+
rootNavigationController.popToViewController(pick, animated: isAnimated)
536+
}
537+
498538
public func close(isAnimated: Bool, completeAction: @escaping () -> Void) {
499539
guard isSubNavigationControllerPresented else { return }
500540
rootNavigationController.dismiss(animated: isAnimated, completion: completeAction)
@@ -548,17 +588,31 @@ fileprivate extension LinkNavigator {
548588
.first(where: { $0.matchingKey == path }) != nil
549589
}
550590

551-
func findViewController(path: String) -> UIViewController? {
591+
func findFirstViewController(path: String) -> UIViewController? {
552592
currentActivityNavigationController
553593
.viewControllers
554594
.compactMap { $0 as? MatchingKeyUsable & UIViewController }
555595
.first(where: { $0.matchingKey == path })
556596
}
557597

558-
func findViewControllerRootView(path: String) -> UIViewController? {
598+
func findLastViewController(path: String) -> UIViewController? {
599+
currentActivityNavigationController
600+
.viewControllers
601+
.compactMap { $0 as? MatchingKeyUsable & UIViewController }
602+
.last(where: { $0.matchingKey == path })
603+
}
604+
605+
func findFirstViewControllerRootView(path: String) -> UIViewController? {
559606
rootNavigationController
560607
.viewControllers
561608
.compactMap { $0 as? MatchingKeyUsable & UIViewController }
562609
.first(where: { $0.matchingKey == path })
563610
}
611+
612+
func findLastViewControllerRootView(path: String) -> UIViewController? {
613+
rootNavigationController
614+
.viewControllers
615+
.compactMap { $0 as? MatchingKeyUsable & UIViewController }
616+
.last(where: { $0.matchingKey == path })
617+
}
564618
}

0 commit comments

Comments
 (0)