From 9e0117dcc0628f6082fd5ef814525389f5c5550c Mon Sep 17 00:00:00 2001 From: admirsaheta Date: Fri, 18 Apr 2025 16:39:45 +0200 Subject: [PATCH 1/5] feat:improve-efficiency-overhead --- .../ios/ShopifyCheckoutSheetKit.swift | 62 +++++++++++-------- 1 file changed, 36 insertions(+), 26 deletions(-) diff --git a/modules/@shopify/checkout-sheet-kit/ios/ShopifyCheckoutSheetKit.swift b/modules/@shopify/checkout-sheet-kit/ios/ShopifyCheckoutSheetKit.swift index 340f313f..2b303aa3 100644 --- a/modules/@shopify/checkout-sheet-kit/ios/ShopifyCheckoutSheetKit.swift +++ b/modules/@shopify/checkout-sheet-kit/ios/ShopifyCheckoutSheetKit.swift @@ -268,15 +268,16 @@ class RCTShopifyCheckoutSheetKit: RCTEventEmitter, CheckoutDelegate { } } - private func encodeToJSON(from value: Codable) -> [String: Any] { - let encoder = JSONEncoder() + private let jsonEncoder = JSONEncoder() + private func encodeToJSON(from value: Codable) -> [String: Any] { do { - let jsonData = try encoder.encode(value) + let jsonData = try jsonEncoder.encode(value) if let jsonObject = try JSONSerialization.jsonObject(with: jsonData, options: []) as? [String: Any] { return jsonObject } } catch { + // Consider using a logging framework here print("Error encoding to JSON object: \(error)") } return [:] @@ -317,27 +318,36 @@ class RCTShopifyCheckoutSheetKit: RCTEventEmitter, CheckoutDelegate { } extension UIColor { - convenience init(hex: String) { - let hexString: String = hex.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) - let start = hexString.index(hexString.startIndex, offsetBy: hexString.hasPrefix("#") ? 1 : 0) - let hexColor = String(hexString[start...]) - - let scanner = Scanner(string: hexColor) - var hexNumber: UInt64 = 0 - - if scanner.scanHexInt64(&hexNumber) { - let red = (hexNumber & 0xff0000) >> 16 - let green = (hexNumber & 0x00ff00) >> 8 - let blue = hexNumber & 0x0000ff - - self.init( - red: CGFloat(red) / 0xff, - green: CGFloat(green) / 0xff, - blue: CGFloat(blue) / 0xff, - alpha: 1 - ) - } else { - self.init(red: 0, green: 0, blue: 0, alpha: 1) - } - } + private static var colorCache = [String: UIColor]() + + convenience init(hex: String) { + if let cachedColor = UIColor.colorCache[hex] { + self.init(cgColor: cachedColor.cgColor) + return + } + + let hexString: String = hex.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) + let start = hexString.index(hexString.startIndex, offsetBy: hexString.hasPrefix("#") ? 1 : 0) + let hexColor = String(hexString[start...]) + + let scanner = Scanner(string: hexColor) + var hexNumber: UInt64 = 0 + + if scanner.scanHexInt64(&hexNumber) { + let red = (hexNumber & 0xff0000) >> 16 + let green = (hexNumber & 0x00ff00) >> 8 + let blue = hexNumber & 0x0000ff + + let color = UIColor( + red: CGFloat(red) / 0xff, + green: CGFloat(green) / 0xff, + blue: CGFloat(blue) / 0xff, + alpha: 1 + ) + UIColor.colorCache[hex] = color + self.init(cgColor: color.cgColor) + } else { + self.init(red: 0, green: 0, blue: 0, alpha: 1) + } + } } From c78e23d8363072bdb0bf6cf555b860ec43d0ef11 Mon Sep 17 00:00:00 2001 From: admirsaheta Date: Fri, 18 Apr 2025 16:44:50 +0200 Subject: [PATCH 2/5] add:changelog-entry --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 51318ce2..f4bf353a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## 3.2.0 - April 18, 2025 + +- Reusing JSONEncoder from CheckoutSheetKitSwift to reduce function overhead +- The UIColor extension now caches color instances to avoid redundant parsing of hex strings and creation of new UIColor objects + ## 3.2.0 - December 18, 2024 - Handle geolocation requests for Android devices @@ -171,3 +176,5 @@ Updates the README on the NPM regsitry entry page. Initial publication of the `@shopify/checkout-sheet-kit` package. Please refer to the [Readme](./README.md) for setup intstructions and usage. + + From 2cb5a5c372c73f8d795f70cdfe4a01acca41d265 Mon Sep 17 00:00:00 2001 From: admirsaheta Date: Fri, 18 Apr 2025 16:46:13 +0200 Subject: [PATCH 3/5] format:document --- .../checkout-sheet-kit/ios/ShopifyCheckoutSheetKit.swift | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/@shopify/checkout-sheet-kit/ios/ShopifyCheckoutSheetKit.swift b/modules/@shopify/checkout-sheet-kit/ios/ShopifyCheckoutSheetKit.swift index 2b303aa3..196e9e28 100644 --- a/modules/@shopify/checkout-sheet-kit/ios/ShopifyCheckoutSheetKit.swift +++ b/modules/@shopify/checkout-sheet-kit/ios/ShopifyCheckoutSheetKit.swift @@ -277,7 +277,6 @@ class RCTShopifyCheckoutSheetKit: RCTEventEmitter, CheckoutDelegate { return jsonObject } } catch { - // Consider using a logging framework here print("Error encoding to JSON object: \(error)") } return [:] From 3f02df0c31a8b1abceea322f299bf8902850ca72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Admir=20=C5=A0aheta?= <81534875+admirsaheta@users.noreply.github.com> Date: Tue, 22 Apr 2025 16:44:20 +0200 Subject: [PATCH 4/5] Update modules/@shopify/checkout-sheet-kit/ios/ShopifyCheckoutSheetKit.swift Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- .../checkout-sheet-kit/ios/ShopifyCheckoutSheetKit.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/@shopify/checkout-sheet-kit/ios/ShopifyCheckoutSheetKit.swift b/modules/@shopify/checkout-sheet-kit/ios/ShopifyCheckoutSheetKit.swift index 196e9e28..9a4e63d0 100644 --- a/modules/@shopify/checkout-sheet-kit/ios/ShopifyCheckoutSheetKit.swift +++ b/modules/@shopify/checkout-sheet-kit/ios/ShopifyCheckoutSheetKit.swift @@ -268,10 +268,10 @@ class RCTShopifyCheckoutSheetKit: RCTEventEmitter, CheckoutDelegate { } } - private let jsonEncoder = JSONEncoder() private func encodeToJSON(from value: Codable) -> [String: Any] { do { + let jsonEncoder = JSONEncoder() let jsonData = try jsonEncoder.encode(value) if let jsonObject = try JSONSerialization.jsonObject(with: jsonData, options: []) as? [String: Any] { return jsonObject From e42a69490883fb72adc55dbebc92c69bcd8572cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Admir=20=C5=A0aheta?= <81534875+admirsaheta@users.noreply.github.com> Date: Tue, 22 Apr 2025 16:44:26 +0200 Subject: [PATCH 5/5] Update modules/@shopify/checkout-sheet-kit/ios/ShopifyCheckoutSheetKit.swift Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- .../checkout-sheet-kit/ios/ShopifyCheckoutSheetKit.swift | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/modules/@shopify/checkout-sheet-kit/ios/ShopifyCheckoutSheetKit.swift b/modules/@shopify/checkout-sheet-kit/ios/ShopifyCheckoutSheetKit.swift index 9a4e63d0..d03bccac 100644 --- a/modules/@shopify/checkout-sheet-kit/ios/ShopifyCheckoutSheetKit.swift +++ b/modules/@shopify/checkout-sheet-kit/ios/ShopifyCheckoutSheetKit.swift @@ -318,12 +318,16 @@ class RCTShopifyCheckoutSheetKit: RCTEventEmitter, CheckoutDelegate { extension UIColor { private static var colorCache = [String: UIColor]() + private static let colorCacheQueue = DispatchQueue(label: "com.shopify.colorCacheQueue") convenience init(hex: String) { - if let cachedColor = UIColor.colorCache[hex] { + var cachedColor: UIColor? + UIColor.colorCacheQueue.sync { + cachedColor = UIColor.colorCache[hex] + } + if let cachedColor = cachedColor { self.init(cgColor: cachedColor.cgColor) return - } let hexString: String = hex.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) let start = hexString.index(hexString.startIndex, offsetBy: hexString.hasPrefix("#") ? 1 : 0)