Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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.


Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
}
}