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. + + diff --git a/modules/@shopify/checkout-sheet-kit/ios/ShopifyCheckoutSheetKit.swift b/modules/@shopify/checkout-sheet-kit/ios/ShopifyCheckoutSheetKit.swift index 340f313f..d03bccac 100644 --- a/modules/@shopify/checkout-sheet-kit/ios/ShopifyCheckoutSheetKit.swift +++ b/modules/@shopify/checkout-sheet-kit/ios/ShopifyCheckoutSheetKit.swift @@ -268,11 +268,11 @@ class RCTShopifyCheckoutSheetKit: RCTEventEmitter, CheckoutDelegate { } } - private func encodeToJSON(from value: Codable) -> [String: Any] { - let encoder = JSONEncoder() + private func encodeToJSON(from value: Codable) -> [String: Any] { do { - let jsonData = try encoder.encode(value) + let jsonEncoder = JSONEncoder() + let jsonData = try jsonEncoder.encode(value) if let jsonObject = try JSONSerialization.jsonObject(with: jsonData, options: []) as? [String: Any] { return jsonObject } @@ -317,27 +317,40 @@ 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]() + private static let colorCacheQueue = DispatchQueue(label: "com.shopify.colorCacheQueue") + + convenience init(hex: String) { + 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) + 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) + } + } }