Skip to content

Commit

Permalink
only expose one clickedLink func on delegate
Browse files Browse the repository at this point in the history
  • Loading branch information
cianbuckley committed Oct 2, 2023
1 parent ef53b10 commit bf5828c
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 40 deletions.
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,8 @@ extension MyViewController: ShopifyCheckoutDelegate {
// The buyer encountered an error during checkout.
}

func checkoutDidClickContactLink(url: URL) {
// Called when the buyer clicked a link which points to an email address or telephone number via `mailto:` or `tel:`.
}

func checkoutDidClickLink(url: URL) {
// Called when the buyer clicked a link which points to an external URL
// Called when the buyer clicked a link e.g mail address or telephone number via `mailto:` or `tel:` or `http` links.
}
}
```
Expand Down
16 changes: 3 additions & 13 deletions Sources/ShopifyCheckout/CheckoutDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,16 @@ public protocol CheckoutDelegate: AnyObject {
/// Tells the delegate that the checkout encoutered one or more errors.
func checkoutDidFail(errors: [CheckoutError])

/// Tells te delegate that the buyer clicked an external link
/// Tells te delegate that the buyer clicked a link
/// This may include email address or telephone number via `mailto:` or `tel:`.
func checkoutDidClickLink(url: URL)

/// Tells the delegate that the buyer clicked a link which points to an
/// email address or telephone number via `mailto:` or `tel:`.
func checkoutDidClickContactLink(url: URL)
}

extension CheckoutDelegate where Self: UIViewController {
/// Tells te delegate that the buyer clicked an external link
extension CheckoutDelegate {
public func checkoutDidClickLink(url: URL) {
handleUrl(url)
}

/// Tells the delegate that the buyer clicked a link which points to an
/// email address or telephone number via `mailto:` or `tel:`.
public func checkoutDidClickContactLink(url: URL) {
handleUrl(url)
}

private func handleUrl(_ url: URL) {
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url)
Expand Down
20 changes: 10 additions & 10 deletions Sources/ShopifyCheckout/CheckoutView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ protocol CheckoutViewDelegate: AnyObject {

func checkoutViewDidFinishNavigation()

func checkoutViewDidClickContactLink(url: URL)

func checkoutViewDidClickLink(url: URL)

func checkoutViewDidFailWithError(_ error: Error)
Expand Down Expand Up @@ -121,13 +119,7 @@ extension CheckoutView: WKNavigationDelegate {
return
}

if ["mailto", "tel"].contains(url.scheme) {
delegate?.checkoutViewDidClickContactLink(url: url)
decisionHandler(.cancel)
return
}

if action.navigationType == .linkActivated && action.targetFrame == nil {
if isExternalLink(action) || isMailOrTelLink(url) {
delegate?.checkoutViewDidClickLink(url: url)
decisionHandler(.cancel)
return
Expand All @@ -143,7 +135,15 @@ extension CheckoutView: WKNavigationDelegate {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
delegate?.checkoutViewDidFinishNavigation()
}
}

private func isExternalLink(_ action: WKNavigationAction) -> Bool {
return action.navigationType == .linkActivated && action.targetFrame == nil
}

private func isMailOrTelLink(_ url: URL) -> Bool {
return ["mailto", "tel"].contains(url.scheme)
}
}

extension CheckoutView {
fileprivate struct CacheEntry {
Expand Down
4 changes: 0 additions & 4 deletions Sources/ShopifyCheckout/CheckoutViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,6 @@ extension CheckoutViewController: CheckoutViewDelegate {
delegate?.checkoutDidComplete()
}

func checkoutViewDidClickContactLink(url: URL) {
delegate?.checkoutDidClickContactLink(url: url)
}

func checkoutViewDidFailWithError(_ error: Error) {
delegate?.checkoutDidFail(errors: [.internalError(underlying: error)])
}
Expand Down
16 changes: 8 additions & 8 deletions Tests/ShopifyCheckoutTests/CheckoutViewTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,34 +36,34 @@ class CheckoutViewTests: XCTestCase {
let link = URL(string: "mailto:[email protected]")!

let delegate = MockCheckoutViewDelegate()
let didClickContactLinkExpectation = expectation(
description: "checkoutViewDidClickContactLink was called"
let didClickLinkExpectation = expectation(
description: "checkoutViewDidClickLink was called"
)
delegate.didClickContactLinkExpectation = didClickContactLinkExpectation
delegate.didClickLinkExpectation = didClickLinkExpectation
view.delegate = delegate

view.webView(view, decidePolicyFor: MockNavigationAction(url: link)) { policy in
XCTAssertEqual(policy, .cancel)
}

wait(for: [didClickContactLinkExpectation], timeout: 1)
wait(for: [didClickLinkExpectation], timeout: 1)
}

func testPhoneContactLinkDelegation() {
let link = URL(string: "tel:1234567890")!

let delegate = MockCheckoutViewDelegate()
let didClickContactLinkExpectation = expectation(
description: "checkoutViewDidClickContactLink was called"
let didCLickLinkExpectation = expectation(
description: "checkoutViewDidClickLink was called"
)
delegate.didClickContactLinkExpectation = didClickContactLinkExpectation
delegate.didClickLinkExpectation = didCLickLinkExpectation
view.delegate = delegate

view.webView(view, decidePolicyFor: MockNavigationAction(url: link)) { policy in
XCTAssertEqual(policy, .cancel)
}

wait(for: [didClickContactLinkExpectation], timeout: 1)
wait(for: [didCLickLinkExpectation], timeout: 1)
}

func testURLLinkDelegation() {
Expand Down

0 comments on commit bf5828c

Please sign in to comment.