Skip to content

Commit

Permalink
Simplify open external function
Browse files Browse the repository at this point in the history
  • Loading branch information
olivaresf committed Nov 30, 2023
1 parent c304e59 commit 875a4c2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@ class TurboNavigationHierarchyController {
}
}

func openExternal(url: URL, navigationType: NavigationStackType) {
delegate.visit(externalURL: url, on: navigationType)
}

// MARK: Private

@available(*, unavailable)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ import WebKit
/// or to render a native controller instead of a Turbo web visit.
protocol TurboNavigationHierarchyControllerDelegate: AnyObject {
func visit(_ : Visitable, on: TurboNavigationHierarchyController.NavigationStackType, with: VisitOptions)
func visit(externalURL: URL, on: TurboNavigationHierarchyController.NavigationStackType)
func refresh(navigationStack: TurboNavigationHierarchyController.NavigationStackType)
}
43 changes: 19 additions & 24 deletions Source/Turbo Navigator/TurboNavigator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,25 @@ extension TurboNavigator: SessionDelegate {
}
}

public func session(_ session: Session, openExternalURL url: URL) {
let navigationType: TurboNavigationHierarchyController.NavigationStackType = session === modalSession ? .modal : .main
hierarchyController.openExternal(url: url, navigationType: navigationType)
public func session(_ session: Session, openExternalURL externalURL: URL) {

switch delegate.handle(externalURL: externalURL) {

case .openViaSystem:
UIApplication.shared.open(externalURL)

case .openViaSafariController:
let safariViewController = SFSafariViewController(url: externalURL)
safariViewController.modalPresentationStyle = .pageSheet
if #available(iOS 15.0, *) {
safariViewController.preferredControlTintColor = .tintColor
}

activeNavigationController.present(safariViewController, animated: true)

case .reject:
return
}
}

public func session(_ session: Session, didFailRequestForVisitable visitable: Visitable, error: Error) {
Expand Down Expand Up @@ -153,27 +169,6 @@ extension TurboNavigator: TurboNavigationHierarchyControllerDelegate {
case .modal: modalSession.visit(controller, action: .advance)
}
}

func visit(externalURL: URL, on: TurboNavigationHierarchyController.NavigationStackType) {

switch delegate.handle(externalURL: externalURL) {

case .openViaSystem:
UIApplication.shared.open(externalURL)

case .openViaSafariController:
let safariViewController = SFSafariViewController(url: externalURL)
safariViewController.modalPresentationStyle = .pageSheet
if #available(iOS 15.0, *) {
safariViewController.preferredControlTintColor = .tintColor
}

activeNavigationController.present(safariViewController, animated: true)

case .reject:
return
}
}

func refresh(navigationStack: TurboNavigationHierarchyController.NavigationStackType) {
switch navigationStack {
Expand Down

0 comments on commit 875a4c2

Please sign in to comment.