From 261a85eceae1dac32d1c511864cc8c20fc10e596 Mon Sep 17 00:00:00 2001 From: Daniel Kift Date: Fri, 20 Oct 2023 11:58:04 +0100 Subject: [PATCH] parse visible property from event and use that to hide/show nav bar --- Sources/ShopifyCheckout/CheckoutBridge.swift | 5 +++-- Sources/ShopifyCheckout/CheckoutView.swift | 6 +++--- Sources/ShopifyCheckout/CheckoutViewController.swift | 6 +++--- .../ShopifyCheckoutTests/CheckoutViewControllerTests.swift | 4 ++-- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/Sources/ShopifyCheckout/CheckoutBridge.swift b/Sources/ShopifyCheckout/CheckoutBridge.swift index b1b48d41..0782bd5b 100644 --- a/Sources/ShopifyCheckout/CheckoutBridge.swift +++ b/Sources/ShopifyCheckout/CheckoutBridge.swift @@ -58,7 +58,7 @@ extension CheckoutBridge { case checkoutCanceled case checkoutExpired case checkoutUnavailable - case checkoutModalToggled + case checkoutModalToggled(modalVisible: Bool) case unsupported(String) enum CodingKeys: String, CodingKey { @@ -80,7 +80,8 @@ extension CheckoutBridge { // needs to support .checkoutUnavailable by parsing error payload on body self = .checkoutExpired case "checkoutBlockingEvent": - self = .checkoutModalToggled + let modalVisible = try container.decode(String.self, forKey: .body) + self = .checkoutModalToggled(modalVisible: Bool(modalVisible)!) default: self = .unsupported(name) } diff --git a/Sources/ShopifyCheckout/CheckoutView.swift b/Sources/ShopifyCheckout/CheckoutView.swift index 4b8180d9..afb7de6d 100644 --- a/Sources/ShopifyCheckout/CheckoutView.swift +++ b/Sources/ShopifyCheckout/CheckoutView.swift @@ -30,7 +30,7 @@ protocol CheckoutViewDelegate: AnyObject { func checkoutViewDidFinishNavigation() func checkoutViewDidClickLink(url: URL) func checkoutViewDidFailWithError(error: CheckoutError) - func checkoutViewDidToggleModal() + func checkoutViewDidToggleModal(modalVisible: Bool) } class CheckoutView: WKWebView { @@ -105,8 +105,8 @@ extension CheckoutView: WKScriptMessageHandler { case .checkoutUnavailable: CheckoutView.cache = nil viewDelegate?.checkoutViewDidFailWithError(error: .checkoutUnavailable(message: "Checkout unavailable.")) - case .checkoutModalToggled: - viewDelegate?.checkoutViewDidToggleModal() + case let .checkoutModalToggled(modalVisible): + viewDelegate?.checkoutViewDidToggleModal(modalVisible: modalVisible) default: () } diff --git a/Sources/ShopifyCheckout/CheckoutViewController.swift b/Sources/ShopifyCheckout/CheckoutViewController.swift index 661c4497..844c5b16 100644 --- a/Sources/ShopifyCheckout/CheckoutViewController.swift +++ b/Sources/ShopifyCheckout/CheckoutViewController.swift @@ -150,8 +150,8 @@ extension CheckoutViewController: CheckoutViewDelegate { delegate?.checkoutDidClickLink(url: url) } - func checkoutViewDidToggleModal() { - let hide = !self.navigationController!.isNavigationBarHidden - self.navigationController?.setNavigationBarHidden(hide, animated: true) + func checkoutViewDidToggleModal(modalVisible: Bool) { + guard let navigationController = self.navigationController else { return } + navigationController.setNavigationBarHidden(modalVisible, animated: true) } } diff --git a/Tests/ShopifyCheckoutTests/CheckoutViewControllerTests.swift b/Tests/ShopifyCheckoutTests/CheckoutViewControllerTests.swift index 45ee2add..d8141fa8 100644 --- a/Tests/ShopifyCheckoutTests/CheckoutViewControllerTests.swift +++ b/Tests/ShopifyCheckoutTests/CheckoutViewControllerTests.swift @@ -104,10 +104,10 @@ class CheckoutViewDelegateTests: XCTestCase { func testCheckoutViewDidToggleModalAddsAndRemovesNavigationBar() { XCTAssertFalse(viewController.navigationController!.isNavigationBarHidden) - viewController.checkoutViewDidToggleModal() + viewController.checkoutViewDidToggleModal(modalVisible: true) XCTAssertTrue(viewController.navigationController!.isNavigationBarHidden) - viewController.checkoutViewDidToggleModal() + viewController.checkoutViewDidToggleModal(modalVisible: false) XCTAssertFalse(viewController.navigationController!.isNavigationBarHidden) } }