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

hide navigation bar when web modals are displayed #23

Merged
merged 4 commits into from
Oct 27, 2023
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
4 changes: 4 additions & 0 deletions Sources/ShopifyCheckout/CheckoutBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ extension CheckoutBridge {
case checkoutComplete
case checkoutExpired
case checkoutUnavailable
case checkoutModalToggled(modalVisible: Bool)
case unsupported(String)

enum CodingKeys: String, CodingKey {
Expand All @@ -75,6 +76,9 @@ extension CheckoutBridge {
case "error":
// needs to support .checkoutUnavailable by parsing error payload on body
self = .checkoutExpired
case "checkoutBlockingEvent":
let modalVisible = try container.decode(String.self, forKey: .body)
self = .checkoutModalToggled(modalVisible: Bool(modalVisible)!)
default:
self = .unsupported(name)
}
Expand Down
3 changes: 3 additions & 0 deletions Sources/ShopifyCheckout/CheckoutView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ protocol CheckoutViewDelegate: AnyObject {
func checkoutViewDidFinishNavigation()
func checkoutViewDidClickLink(url: URL)
func checkoutViewDidFailWithError(error: CheckoutError)
func checkoutViewDidToggleModal(modalVisible: Bool)
}

class CheckoutView: WKWebView {
Expand Down Expand Up @@ -104,6 +105,8 @@ extension CheckoutView: WKScriptMessageHandler {
case .checkoutUnavailable:
CheckoutView.cache = nil
viewDelegate?.checkoutViewDidFailWithError(error: .checkoutUnavailable(message: "Checkout unavailable."))
case let .checkoutModalToggled(modalVisible):
viewDelegate?.checkoutViewDidToggleModal(modalVisible: modalVisible)
default:
()
}
Expand Down
5 changes: 5 additions & 0 deletions Sources/ShopifyCheckout/CheckoutViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,9 @@ extension CheckoutViewController: CheckoutViewDelegate {
func checkoutViewDidClickLink(url: URL) {
delegate?.checkoutDidClickLink(url: url)
}

func checkoutViewDidToggleModal(modalVisible: Bool) {
guard let navigationController = self.navigationController else { return }
navigationController.setNavigationBarHidden(modalVisible, animated: true)
}
}
15 changes: 15 additions & 0 deletions Tests/ShopifyCheckoutTests/CheckoutBridgeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,19 @@ class CheckoutBridgeTests: XCTestCase {
return XCTFail("expected CheckoutScriptMessage.checkoutExpired, got \(result)")
}
}

func testDecodeSupportsCheckoutBlockingEvent() throws {
let mock = WKScriptMessageMock(body: """
{
"name": "checkoutBlockingEvent",
"body": "true"
}
""")

let result = try CheckoutBridge.decode(mock)

guard case CheckoutBridge.WebEvent.checkoutModalToggled = result else {
return XCTFail("expected CheckoutScriptMessage.checkoutModalToggled, got \(result)")
}
}
}
13 changes: 13 additions & 0 deletions Tests/ShopifyCheckoutTests/CheckoutViewControllerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@ class CheckoutViewDelegateTests: XCTestCase {

private let checkoutURL = URL(string: "https://checkout-sdk.myshopify.com")!
private var viewController: CheckoutViewController!
private var navigationController: UINavigationController!

override func setUp() {
ShopifyCheckout.configure {
$0.preloading.enabled = true
}
viewController = CheckoutViewController(
checkoutURL: checkoutURL, delegate: ExampleDelegate())

navigationController = UINavigationController(rootViewController: viewController)
}

func testTitleIsSetToCheckout() {
Expand Down Expand Up @@ -97,4 +100,14 @@ class CheckoutViewDelegateTests: XCTestCase {
let three = CheckoutView.for(checkout: checkoutURL)
XCTAssertEqual(two, three)
}

func testCheckoutViewDidToggleModalAddsAndRemovesNavigationBar() {
XCTAssertFalse(viewController.navigationController!.isNavigationBarHidden)

viewController.checkoutViewDidToggleModal(modalVisible: true)
XCTAssertTrue(viewController.navigationController!.isNavigationBarHidden)

viewController.checkoutViewDidToggleModal(modalVisible: false)
XCTAssertFalse(viewController.navigationController!.isNavigationBarHidden)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class MockCheckoutViewDelegate: CheckoutViewDelegate {

var didFailWithErrorExpectation: XCTestExpectation?

var didToggleModalExpectation: XCTestExpectation?

func checkoutViewDidStartNavigation() {
didStartNavigationExpectation?.fulfill()
}
Expand All @@ -60,4 +62,8 @@ class MockCheckoutViewDelegate: CheckoutViewDelegate {
func checkoutViewDidFailWithError(error: CheckoutError) {
didFailWithErrorExpectation?.fulfill()
}

func checkoutViewDidToggleModal() {
didToggleModalExpectation?.fulfill()
}
}