-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathShopifyCheckoutKit.swift
More file actions
77 lines (66 loc) · 2.76 KB
/
Copy pathShopifyCheckoutKit.swift
File metadata and controls
77 lines (66 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#if !COCOAPODS
import EmbeddedCheckoutProtocol
#endif
import UIKit
/// The version of the `ShopifyCheckoutKit` library.
public let version = "4.0.0-alpha.2"
private let lockedCheckoutKitConfiguration = LockedValue(Configuration())
/// The configuration options for the `ShopifyCheckoutKit` library.
public var configuration: Configuration {
get { lockedCheckoutKitConfiguration.get() }
set {
let previousConfiguration = lockedCheckoutKitConfiguration.get()
lockedCheckoutKitConfiguration.set(newValue)
applyConfigurationChange(
configuration: newValue,
previousConfiguration: previousConfiguration
)
}
}
/// A convienence function for configuring the `ShopifyCheckoutKit` library.
public func configure(_ block: (inout Configuration) -> Void) {
let previousConfiguration = lockedCheckoutKitConfiguration.get()
lockedCheckoutKitConfiguration.update(block)
applyConfigurationChange(
configuration: lockedCheckoutKitConfiguration.get(),
previousConfiguration: previousConfiguration
)
}
private func applyConfigurationChange(configuration: Configuration, previousConfiguration: Configuration) {
OSLogger.shared.logLevel = configuration.logLevel
if configuration.preloading.enabled != previousConfiguration.preloading.enabled {
Task { @MainActor in
CheckoutWebView.invalidate()
}
}
}
/// Preloads the checkout for faster presentation.
@MainActor
public func preload(checkout url: URL) {
guard configuration.preloading.enabled else {
return
}
let decorated = CheckoutURLDecorator.decorate(url)
CheckoutWebView.preload(checkout: decorated)
}
/// Invalidates any cached checkout created by preload calls.
@MainActor
public func invalidate() {
CheckoutWebView.invalidate(disconnect: true)
}
@MainActor
@discardableResult
public func present(checkout url: URL, from: UIViewController, delegate: (any CheckoutDelegate)? = nil, client: (any CheckoutCommunicationProtocol)? = nil) -> CheckoutViewController {
let decorated = CheckoutURLDecorator.decorate(url)
let viewController = CheckoutViewController(checkout: decorated, delegate: delegate, client: client)
from.present(viewController, animated: true)
return viewController
}
@MainActor
@discardableResult
package func present(checkout url: URL, from: UIViewController, entryPoint: MetaData.EntryPoint, delegate: (any CheckoutDelegate)? = nil, client: (any CheckoutCommunicationProtocol)? = nil) -> CheckoutViewController {
let decorated = CheckoutURLDecorator.decorate(url)
let viewController = CheckoutViewController(checkout: decorated, delegate: delegate, client: client, entryPoint: entryPoint)
from.present(viewController, animated: true)
return viewController
}