Skip to content

Commit

Permalink
Merge pull request #21 from hotwired/pop-and-clearAll
Browse files Browse the repository at this point in the history
Expose `pop()` and `clearAll()` on `Navigator`
  • Loading branch information
joemasilotti authored May 22, 2024
2 parents aa529a0 + ae4f2a2 commit f4db7fb
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 20 deletions.
40 changes: 20 additions & 20 deletions Source/Turbo/Navigator/NavigationHierarchyController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ class NavigationHierarchyController {
case .default:
navigate(with: controller, via: proposal)
case .pop:
pop(via: proposal)
pop(animated: proposal.animated)
case .replace:
replace(with: controller, via: proposal)
case .refresh:
refresh(via: proposal)
case .clearAll:
clearAll(via: proposal)
clearAll(animated: proposal.animated)
case .replaceRoot:
replaceRoot(with: controller, via: proposal)
case .none:
Expand All @@ -60,6 +60,24 @@ class NavigationHierarchyController {
}
}

func pop(animated: Bool) {
if navigationController.presentedViewController != nil {
if modalNavigationController.viewControllers.count == 1 {
navigationController.dismiss(animated: animated)
} else {
modalNavigationController.popViewController(animated: animated)
}
} else {
navigationController.popViewController(animated: animated)
}
}

func clearAll(animated: Bool) {
delegate.refresh(navigationStack: .main)
navigationController.dismiss(animated: animated)
navigationController.popToRootViewController(animated: animated)
}

// MARK: Private

@available(*, unavailable)
Expand Down Expand Up @@ -130,18 +148,6 @@ class NavigationHierarchyController {
return type(of: previousController) == type(of: controller)
}

private func pop(via proposal: VisitProposal) {
if navigationController.presentedViewController != nil {
if modalNavigationController.viewControllers.count == 1 {
navigationController.dismiss(animated: proposal.animated)
} else {
modalNavigationController.popViewController(animated: proposal.animated)
}
} else {
navigationController.popViewController(animated: proposal.animated)
}
}

private func replace(with controller: UIViewController, via proposal: VisitProposal) {
switch proposal.context {
case .default:
Expand Down Expand Up @@ -179,12 +185,6 @@ class NavigationHierarchyController {
}
}

private func clearAll(via proposal: VisitProposal) {
delegate.refresh(navigationStack: .main)
navigationController.dismiss(animated: proposal.animated)
navigationController.popToRootViewController(animated: proposal.animated)
}

private func replaceRoot(with controller: UIViewController, via proposal: VisitProposal) {
if let visitable = controller as? Visitable {
delegate.visit(visitable, on: .main, with: .init(action: .replace))
Expand Down
17 changes: 17 additions & 0 deletions Source/Turbo/Navigator/Navigator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,23 @@ public class Navigator {
hierarchyController.route(controller: controller, proposal: proposal)
}

/// Pops the top controller on the presented navigation stack.
/// If a modal is presented with a single controller in the navigation
/// stack then the modal is dismissed instead.
/// - Parameter animated: Pass true to animate the presentation;
/// otherwise, pass false.
public func pop(animated: Bool = true) {
hierarchyController.pop(animated: animated)
}

/// Dismisses a modally presented controller if present, then pops the
/// entire navigation stack.
/// - Parameter animated: Pass true to animate the presentation;
/// otherwise, pass false.
public func clearAll(animated: Bool = false) {
hierarchyController.clearAll(animated: animated)
}

/// Navigate to an external URL.
///
/// - Parameters:
Expand Down

0 comments on commit f4db7fb

Please sign in to comment.