-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCheckoutViewController.swift
More file actions
122 lines (102 loc) · 4.26 KB
/
Copy pathCheckoutViewController.swift
File metadata and controls
122 lines (102 loc) · 4.26 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#if !COCOAPODS
import EmbeddedCheckoutProtocol
#endif
import SwiftUI
import UIKit
@MainActor
public class CheckoutViewController: UINavigationController {
public init(checkout url: URL, delegate: (any CheckoutDelegate)? = nil, client: (any CheckoutCommunicationProtocol)? = nil) {
let rootViewController = CheckoutWebViewController(checkoutURL: url, delegate: delegate, client: client, entryPoint: nil)
super.init(rootViewController: rootViewController)
presentationController?.delegate = rootViewController
}
package init(checkout url: URL, delegate: (any CheckoutDelegate)? = nil, client: (any CheckoutCommunicationProtocol)? = nil, entryPoint: MetaData.EntryPoint? = nil) {
let rootViewController = CheckoutWebViewController(checkoutURL: url, delegate: delegate, client: client, entryPoint: entryPoint)
super.init(rootViewController: rootViewController)
presentationController?.delegate = rootViewController
}
@available(*, unavailable)
required init?(coder _: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
public struct ShopifyCheckout: UIViewControllerRepresentable, CheckoutConfigurable {
public typealias UIViewControllerType = CheckoutViewController
var checkoutURL: URL
var client: (any CheckoutCommunicationProtocol)?
var onCancelAction: (() -> Void)?
var onFailAction: ((CheckoutError) -> Void)?
public init(checkout url: URL) {
checkoutURL = url
}
var decoratedCheckoutURL: URL {
CheckoutURLDecorator.decorate(checkoutURL)
}
public func makeUIViewController(context _: Self.Context) -> CheckoutViewController {
let viewController = CheckoutViewController(checkout: decoratedCheckoutURL, client: client)
configureWebViewController(viewController)
return viewController
}
public func updateUIViewController(_ uiViewController: CheckoutViewController, context _: Self.Context) {
configureWebViewController(uiViewController)
}
private func configureWebViewController(_ navigationController: CheckoutViewController) {
guard
let webViewController = navigationController
.viewControllers
.compactMap({ $0 as? CheckoutWebViewController })
.first
else {
return
}
webViewController.client = client
webViewController.checkoutView?.client = client
webViewController.onCancel = onCancelAction
webViewController.onFail = onFailAction
}
@discardableResult public func connect(_ handler: any CheckoutCommunicationProtocol) -> Self {
var copy = self
copy.client = handler
return copy
}
@discardableResult public func onCancel(_ action: @escaping () -> Void) -> Self {
var copy = self
copy.onCancelAction = action
return copy
}
@discardableResult public func onFail(_ action: @escaping (CheckoutError) -> Void) -> Self {
var copy = self
copy.onFailAction = action
return copy
}
}
@MainActor
public protocol CheckoutConfigurable {
func backgroundColor(_ color: UIColor) -> Self
func colorScheme(_ colorScheme: ShopifyCheckoutKit.Configuration.ColorScheme) -> Self
func tintColor(_ color: UIColor) -> Self
func title(_ title: String) -> Self
func closeButtonTintColor(_ color: UIColor?) -> Self
}
extension CheckoutConfigurable {
@discardableResult public func backgroundColor(_ color: UIColor) -> Self {
ShopifyCheckoutKit.configuration.backgroundColor = color
return self
}
@discardableResult public func colorScheme(_ colorScheme: ShopifyCheckoutKit.Configuration.ColorScheme) -> Self {
ShopifyCheckoutKit.configuration.colorScheme = colorScheme
return self
}
@discardableResult public func tintColor(_ color: UIColor) -> Self {
ShopifyCheckoutKit.configuration.tintColor = color
return self
}
@discardableResult public func title(_ title: String) -> Self {
ShopifyCheckoutKit.configuration.title = title
return self
}
@discardableResult public func closeButtonTintColor(_ color: UIColor?) -> Self {
ShopifyCheckoutKit.configuration.closeButtonTintColor = color
return self
}
}