@@ -27,77 +27,114 @@ import UIKit
2727
2828@objc ( RCTShopifyCheckoutKit)
2929class RCTShopifyCheckoutKit : UIViewController , CheckoutDelegate {
30- func checkoutDidComplete( ) { }
31-
32- func checkoutDidFail( error _: ShopifyCheckoutKit . CheckoutError ) { }
33-
34- func checkoutDidCancel( ) {
35- DispatchQueue . main. async {
36- if let rootViewController = UIApplication . shared. delegate? . window?? . rootViewController {
37- rootViewController. dismiss ( animated: true )
38- }
39- }
40- }
41-
42- @objc func constantsToExport( ) -> [ String : String ] ! {
43- return [
44- " version " : ShopifyCheckoutKit . version
45- ]
46- }
47-
48- @objc func present( _ checkoutURL: String ) {
49- DispatchQueue . main. async {
50- let sharedDelegate = UIApplication . shared. delegate
51-
52- if let url = URL ( string: checkoutURL) , let rootViewController = sharedDelegate? . window?? . rootViewController {
53- ShopifyCheckoutKit . present ( checkout: url, from: rootViewController, delegate: self )
54- }
55- }
56- }
57-
58- @objc func preload( _ checkoutURL: String ) {
59- DispatchQueue . main. async {
60- if let url = URL ( string: checkoutURL) {
61- ShopifyCheckoutKit . preload ( checkout: url)
62- }
63- }
64- }
65-
66- private func getColorScheme( _ colorScheme: String ) -> ShopifyCheckoutKit . Configuration . ColorScheme {
67- switch colorScheme {
68- case " web_default " :
69- return ShopifyCheckoutKit . Configuration. ColorScheme. web
70- case " automatic " :
71- return ShopifyCheckoutKit . Configuration. ColorScheme. automatic
72- case " light " :
73- return ShopifyCheckoutKit . Configuration. ColorScheme. light
74- case " dark " :
75- return ShopifyCheckoutKit . Configuration. ColorScheme. dark
76- default :
77- return ShopifyCheckoutKit . Configuration. ColorScheme. automatic
78- }
79- }
80-
81- @objc func configure( _ configuration: [ AnyHashable : Any ] ) {
82- if let preloading = configuration [ " preloading " ] as? Bool {
83- ShopifyCheckoutKit . configuration. preloading. enabled = preloading
84- }
85-
86- if let colorScheme = configuration [ " colorScheme " ] as? String {
87- ShopifyCheckoutKit . configuration. colorScheme = getColorScheme ( colorScheme)
88- }
89- }
90-
91- @objc func getConfig( _ resolve: @escaping RCTPromiseResolveBlock , reject _: @escaping RCTPromiseRejectBlock ) {
92- let config : [ String : Any ] = [
93- " preloading " : ShopifyCheckoutKit . configuration. preloading. enabled,
94- " colorScheme " : ShopifyCheckoutKit . configuration. colorScheme. rawValue
95- ]
96-
97- resolve ( config)
98- }
99-
100- @objc static func requiresMainQueueSetup( ) -> Bool {
101- return true
102- }
30+ func checkoutDidComplete( ) { }
31+
32+ func checkoutDidFail( error _: ShopifyCheckoutKit . CheckoutError ) { }
33+
34+ func checkoutDidCancel( ) {
35+ DispatchQueue . main. async {
36+ if let rootViewController = UIApplication . shared. delegate? . window?? . rootViewController {
37+ rootViewController. dismiss ( animated: true )
38+ }
39+ }
40+ }
41+
42+ @objc func constantsToExport( ) -> [ String : String ] ! {
43+ return [
44+ " version " : ShopifyCheckoutKit . version
45+ ]
46+ }
47+
48+ @objc func present( _ checkoutURL: String ) {
49+ DispatchQueue . main. async {
50+ let sharedDelegate = UIApplication . shared. delegate
51+
52+ if let url = URL ( string: checkoutURL) , let rootViewController = sharedDelegate? . window?? . rootViewController {
53+ ShopifyCheckoutKit . present ( checkout: url, from: rootViewController, delegate: self )
54+ }
55+ }
56+ }
57+
58+ @objc func preload( _ checkoutURL: String ) {
59+ DispatchQueue . main. async {
60+ if let url = URL ( string: checkoutURL) {
61+ ShopifyCheckoutKit . preload ( checkout: url)
62+ }
63+ }
64+ }
65+
66+ private func getColorScheme( _ colorScheme: String ) -> ShopifyCheckoutKit . Configuration . ColorScheme {
67+ switch colorScheme {
68+ case " web_default " :
69+ return ShopifyCheckoutKit . Configuration. ColorScheme. web
70+ case " automatic " :
71+ return ShopifyCheckoutKit . Configuration. ColorScheme. automatic
72+ case " light " :
73+ return ShopifyCheckoutKit . Configuration. ColorScheme. light
74+ case " dark " :
75+ return ShopifyCheckoutKit . Configuration. ColorScheme. dark
76+ default :
77+ return ShopifyCheckoutKit . Configuration. ColorScheme. automatic
78+ }
79+ }
80+
81+ @objc func configure( _ configuration: [ AnyHashable : Any ] ) {
82+ let colorConfig = configuration [ " colors " ] as? [ AnyHashable : Any ]
83+ let iosConfig = colorConfig ? [ " ios " ] as? [ String : String ]
84+
85+ if let preloading = configuration [ " preloading " ] as? Bool {
86+ ShopifyCheckoutKit . configuration. preloading. enabled = preloading
87+ }
88+
89+ if let colorScheme = configuration [ " colorScheme " ] as? String {
90+ ShopifyCheckoutKit . configuration. colorScheme = getColorScheme ( colorScheme)
91+ }
92+
93+ if let spinnerColorHex = iosConfig ? [ " spinnerColor " ] as? String {
94+ ShopifyCheckoutKit . configuration. spinnerColor = UIColor ( hex: spinnerColorHex)
95+ }
96+
97+ if let backgroundColorHex = iosConfig ? [ " backgroundColor " ] as? String {
98+ ShopifyCheckoutKit . configuration. backgroundColor = UIColor ( hex: backgroundColorHex)
99+ }
100+ }
101+
102+ @objc func getConfig( _ resolve: @escaping RCTPromiseResolveBlock , reject _: @escaping RCTPromiseRejectBlock ) {
103+ let config : [ String : Any ] = [
104+ " preloading " : ShopifyCheckoutKit . configuration. preloading. enabled,
105+ " colorScheme " : ShopifyCheckoutKit . configuration. colorScheme. rawValue
106+ ]
107+
108+ resolve ( config)
109+ }
110+
111+ @objc static func requiresMainQueueSetup( ) -> Bool {
112+ return true
113+ }
114+ }
115+
116+ extension UIColor {
117+ convenience init ( hex: String ) {
118+ let hexString : String = hex. trimmingCharacters ( in: CharacterSet . whitespacesAndNewlines)
119+ let start = hexString. index ( hexString. startIndex, offsetBy: hexString. hasPrefix ( " # " ) ? 1 : 0 )
120+ let hexColor = String ( hexString [ start... ] )
121+
122+ let scanner = Scanner ( string: hexColor)
123+ var hexNumber : UInt64 = 0
124+
125+ if scanner. scanHexInt64 ( & hexNumber) {
126+ let red = ( hexNumber & 0xff0000 ) >> 16
127+ let green = ( hexNumber & 0x00ff00 ) >> 8
128+ let blue = hexNumber & 0x0000ff
129+
130+ self . init (
131+ red: CGFloat ( red) / 0xff ,
132+ green: CGFloat ( green) / 0xff ,
133+ blue: CGFloat ( blue) / 0xff ,
134+ alpha: 1
135+ )
136+ } else {
137+ self . init ( red: 0 , green: 0 , blue: 0 , alpha: 1 )
138+ }
139+ }
103140}
0 commit comments