Skip to content

Commit

Permalink
Undo change to CheckoutDelegate requirement
Browse files Browse the repository at this point in the history
  • Loading branch information
markmur committed Apr 19, 2024
1 parent 7d91a70 commit 5fbd045
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
6 changes: 3 additions & 3 deletions Sources/ShopifyCheckoutSheetKit/CheckoutViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import UIKit
import SwiftUI

public class CheckoutViewController: UINavigationController {
public init(checkout url: URL, delegate: CheckoutDelegate) {
public init(checkout url: URL, delegate: CheckoutDelegate? = nil) {
let rootViewController = CheckoutWebViewController(checkoutURL: url, delegate: delegate)
rootViewController.notifyPresented()
super.init(rootViewController: rootViewController)
Expand All @@ -46,13 +46,13 @@ extension CheckoutViewController {

let delegate: CheckoutDelegate?

public init(checkout url: Binding<URL?>, delegate: CheckoutDelegate) {
public init(checkout url: Binding<URL?>, delegate: CheckoutDelegate? = nil) {
self._checkoutURL = url
self.delegate = delegate
}

public func makeUIViewController(context: Self.Context) -> CheckoutViewController {
return CheckoutViewController(checkout: checkoutURL!, delegate: delegate!)
return CheckoutViewController(checkout: checkoutURL!, delegate: delegate)
}

public func updateUIViewController(_ uiViewController: CheckoutViewController, context: Self.Context) {
Expand Down
16 changes: 8 additions & 8 deletions Sources/ShopifyCheckoutSheetKit/CheckoutWebViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class CheckoutWebViewController: UIViewController, UIAdaptivePresentationControl

// MARK: Properties

var delegate: CheckoutDelegate
var delegate: CheckoutDelegate?

internal var checkoutView: CheckoutWebView

Expand All @@ -87,7 +87,7 @@ class CheckoutWebViewController: UIViewController, UIAdaptivePresentationControl

// MARK: Initializers

public init(checkoutURL url: URL, delegate: CheckoutDelegate) {
public init(checkoutURL url: URL, delegate: CheckoutDelegate? = nil) {
self.checkoutURL = url
self.delegate = delegate

Expand Down Expand Up @@ -189,7 +189,7 @@ class CheckoutWebViewController: UIViewController, UIAdaptivePresentationControl
CheckoutWebView.invalidate()
}

delegate.checkoutDidCancel()
delegate?.checkoutDidCancel()
}
}

Expand All @@ -208,23 +208,23 @@ extension CheckoutWebViewController: CheckoutWebViewDelegate {
func checkoutViewDidCompleteCheckout(event: CheckoutCompletedEvent) {
ConfettiCannon.fire(in: view)
CheckoutWebView.invalidate()
delegate.checkoutDidComplete(event: event)
delegate?.checkoutDidComplete(event: event)
}

func checkoutViewDidFailWithError(error: CheckoutError) {
CheckoutWebView.invalidate()

let shouldAttemptRecovery = delegate.shouldRecoverFromError(error: error)
let shouldAttemptRecovery = delegate?.shouldRecoverFromError(error: error) ?? false

if shouldAttemptRecovery {
self.presentFallbackViewController(url: self.checkoutURL)
} else {
delegate.checkoutDidFail(error: error)
delegate?.checkoutDidFail(error: error)
}
}

func checkoutViewDidClickLink(url: URL) {
delegate.checkoutDidClickLink(url: url)
delegate?.checkoutDidClickLink(url: url)
}

func checkoutViewDidToggleModal(modalVisible: Bool) {
Expand All @@ -236,7 +236,7 @@ extension CheckoutWebViewController: CheckoutWebViewDelegate {
}

func checkoutViewDidEmitWebPixelEvent(event: PixelEvent) {
delegate.checkoutDidEmitWebPixelEvent(event: event)
delegate?.checkoutDidEmitWebPixelEvent(event: event)
}

private func isErrorRecoverable(error: CheckoutError) -> Bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public func preload(checkout url: URL) {

/// Presents the checkout from a given `UIViewController`.
@discardableResult
public func present(checkout url: URL, from: UIViewController, delegate: CheckoutDelegate) -> CheckoutViewController {
public func present(checkout url: URL, from: UIViewController, delegate: CheckoutDelegate?) -> CheckoutViewController {
let viewController = CheckoutViewController(checkout: url, delegate: delegate)
from.present(viewController, animated: true)
return viewController
Expand Down

0 comments on commit 5fbd045

Please sign in to comment.