Skip to content

Instantly share code, notes, and snippets.

@fruitcoder
Last active November 30, 2021 12:28
Show Gist options
  • Save fruitcoder/4ccd2d2f52d12e2199228516e62cc693 to your computer and use it in GitHub Desktop.
Save fruitcoder/4ccd2d2f52d12e2199228516e62cc693 to your computer and use it in GitHub Desktop.
Convenience initializer for CPListItem
@available(iOS 14.0, *)
extension CPListItem {
convenience init(
id: String,
text: String?,
detailText: String?,
remoteImageUrl: URL?,
placeholder: UIImage?
) {
self.init(text: text, detailText: detailText, image: placeholder)
userInfo = [Self.identifierUserInfoKey: id]
if let imageUrl = remoteImageUrl {
Current.imageCache.downloadImage(url: imageUrl, progressHandler: nil) { image, _, _, _ in
guard let image = image else { return }
// don't call this on a background thread
DispatchQueue.main.async { [weak self] in
self?.setImage(image.carPlayImage ?? image) // UPDATE: `carPlayImage` was added on 21-11-30
}
}
}
var identifier: String? {
(userInfo as? [String: Any])?[Self.identifierUserInfoKey] as? String
}
private static let identifierUserInfoKey = "CPListItem.Identifier"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment