Skip to content

Commit

Permalink
Unify pop and refresh logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Fernando Olivares committed Jul 4, 2024
1 parent 75d9a57 commit 66e623b
Showing 1 changed file with 12 additions and 17 deletions.
29 changes: 12 additions & 17 deletions Source/Turbo Navigator/TurboNavigationHierarchyController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class TurboNavigationHierarchyController {
case .replace:
replace(with: controller, via: proposal)
case .refresh:
refresh(via: proposal)
pop(via: proposal, refresh: true)
case .clearAll:
clearAll(via: proposal)
case .replaceRoot:
Expand Down Expand Up @@ -130,15 +130,25 @@ class TurboNavigationHierarchyController {
return type(of: previousController) == type(of: controller)
}

private func pop(via proposal: VisitProposal) {
private func pop(via proposal: VisitProposal, refresh: Bool = false) {
if navigationController.presentedViewController != nil {
if modalNavigationController.viewControllers.count == 1 {
navigationController.dismiss(animated: proposal.animated)

if refresh {
refreshIfTopViewControllerIsVisitable(from: .main)
}
} else {
modalNavigationController.popViewController(animated: proposal.animated)
if refresh {
refreshIfTopViewControllerIsVisitable(from: .modal)
}
}
} else {
navigationController.popViewController(animated: proposal.animated)
if refresh {
refreshIfTopViewControllerIsVisitable(from: .main)
}
}
}

Expand All @@ -163,21 +173,6 @@ class TurboNavigationHierarchyController {
}
}
}

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

private func refreshIfTopViewControllerIsVisitable(from stack: NavigationStackType) {
if let navControllerTopmostVisitable = navController(for: stack).topViewController as? Visitable {
Expand Down

0 comments on commit 66e623b

Please sign in to comment.