Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update refresh with last visitable #22

Merged
merged 2 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions Source/Turbo/Navigator/NavigationHierarchyController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ class NavigationHierarchyController {
}

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

// MARK: Private
Expand Down Expand Up @@ -173,15 +173,15 @@ class NavigationHierarchyController {
private func refresh(via proposal: VisitProposal) {
if navigationController.presentedViewController != nil {
if modalNavigationController.viewControllers.count == 1 {
delegate.refresh(navigationStack: .main)
navigationController.dismiss(animated: proposal.animated)
refreshIfTopViewControllerIsVisitable(from: .main)
} else {
delegate.refresh(navigationStack: .modal)
modalNavigationController.popViewController(animated: proposal.animated)
refreshIfTopViewControllerIsVisitable(from: .modal)
}
} else {
delegate.refresh(navigationStack: .main)
navigationController.popViewController(animated: proposal.animated)
refreshIfTopViewControllerIsVisitable(from: .main)
}
}

Expand All @@ -193,4 +193,11 @@ class NavigationHierarchyController {
navigationController.dismiss(animated: proposal.animated)
navigationController.setViewControllers([controller], animated: proposal.animated)
}

private func refreshIfTopViewControllerIsVisitable(from stack: NavigationStackType) {
if let navControllerTopmostVisitable = navController(for: stack).topViewController as? Visitable {
delegate.refreshVisitable(navigationStack: stack,
newTopmostVisitable: navControllerTopmostVisitable)
}
}
}
22 changes: 18 additions & 4 deletions Source/Turbo/Navigator/NavigationHierarchyControllerDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
import SafariServices
import WebKit

/// Implement to be notified when certain navigations are performed
/// or to render a native controller instead of a Turbo web visit.
protocol NavigationHierarchyControllerDelegate: AnyObject {
func visit(_: Visitable, on: NavigationHierarchyController.NavigationStackType, with: VisitOptions)
func refresh(navigationStack: NavigationHierarchyController.NavigationStackType)

/// Once the navigation hierarchy is modified, begin a visit on a navigation controller.
///
/// - Parameters:
/// - _: the Visitable destination
/// - on: the navigation controller that was modified
/// - with: the visit options
func visit(_ : Visitable,
on: NavigationHierarchyController.NavigationStackType,
with: VisitOptions)

/// A refresh will pop (or dismiss) then ask the session to refresh the previous (or underlying) Visitable.
///
/// - Parameters:
/// - navigationStack: the stack where the refresh is happening
/// - newTopmostVisitable: the visitable to be refreshed
func refreshVisitable(navigationStack: NavigationHierarchyController.NavigationStackType,
newTopmostVisitable: Visitable)
}
10 changes: 6 additions & 4 deletions Source/Turbo/Navigator/Navigator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,13 @@ extension Navigator: NavigationHierarchyControllerDelegate {
case .modal: modalSession.visit(controller, options: options)
}
}

func refresh(navigationStack: NavigationHierarchyController.NavigationStackType) {
func refreshVisitable(navigationStack: NavigationHierarchyController.NavigationStackType, newTopmostVisitable: any Visitable) {
switch navigationStack {
case .main: session.reload()
case .modal: modalSession.reload()
case .main:
session.visit(newTopmostVisitable, action: .restore)
case .modal:
modalSession.visit(newTopmostVisitable, action: .restore)
}
}
}
Expand Down
57 changes: 56 additions & 1 deletion Tests/Turbo/Navigator/NavigationHierarchyControllerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,61 @@ final class NavigationHierarchyControllerTests: XCTestCase {
XCTAssert(navigationController.viewControllers.last is VisitableViewController)
assertVisited(url: proposal.url, on: .main)
}

func test_default_default_refresh_refreshesPreviousController() {
navigator.route(oneURL)
XCTAssertEqual(navigationController.viewControllers.count, 1)

navigator.route(twoURL)
XCTAssertEqual(navigator.rootViewController.viewControllers.count, 2)

/// Refreshing should pop the view controller and refresh the underlying controller.
let proposal = VisitProposal(presentation: .refresh)
navigator.route(proposal)

let visitable = navigator.session.activeVisitable as! VisitableViewController
XCTAssertEqual(visitable.visitableURL, oneURL)
XCTAssertEqual(navigator.rootViewController.viewControllers.count, 1)
}

func test_default_modal_refresh_refreshesPreviousController() {
navigationController.pushViewController(UIViewController(), animated: false)
XCTAssertEqual(navigationController.viewControllers.count, 1)

let oneURLProposal = VisitProposal(path: "/one", context: .modal)
navigator.route(oneURLProposal)

let twoURLProposal = VisitProposal(path: "/two", context: .modal)
navigator.route(twoURLProposal)
XCTAssertEqual(modalNavigationController.viewControllers.count, 2)

/// Refreshing should pop the view controller and refresh the underlying controller.
let proposal = VisitProposal(presentation: .refresh)
navigator.route(proposal)

let visitable = navigator.modalSession.activeVisitable as! VisitableViewController
XCTAssertEqual(visitable.visitableURL, oneURL)
XCTAssertEqual(modalNavigationController.viewControllers.count, 1)
}

func test_default_modal_refresh_dismissesAndRefreshesMainStackTopViewController() {
navigator.route(oneURL)
XCTAssertEqual(navigationController.viewControllers.count, 1)

let twoURLProposal = VisitProposal(path: "/two", context: .modal)
navigator.route(twoURLProposal)
XCTAssertEqual(modalNavigationController.viewControllers.count, 1)

/// Refreshing should dismiss the view controller and refresh the underlying controller.
let proposal = VisitProposal(context: .modal, presentation: .refresh)
navigator.route(proposal)

let visitable = navigator.session.activeVisitable as! VisitableViewController
XCTAssertEqual(visitable.visitableURL, oneURL)

XCTAssertNil(navigationController.presentedViewController)
XCTAssertEqual(navigator.rootViewController.viewControllers.count, 1)
}

func test_default_modal_default_presentsModal() {
navigationController.pushViewController(UIViewController(), animated: false)
Expand Down Expand Up @@ -309,7 +364,7 @@ final class NavigationHierarchyControllerTests: XCTestCase {

private class EmptyNavigationDelegate: NavigationHierarchyControllerDelegate {
func visit(_: Visitable, on: NavigationHierarchyController.NavigationStackType, with: VisitOptions) {}
func refresh(navigationStack: NavigationHierarchyController.NavigationStackType) {}
func refreshVisitable(navigationStack: NavigationHierarchyController.NavigationStackType, newTopmostVisitable: any Visitable) { }
}

// MARK: - VisitProposal extension
Expand Down