Last active
November 30, 2021 12:28
-
-
Save fruitcoder/4ccd2d2f52d12e2199228516e62cc693 to your computer and use it in GitHub Desktop.
Convenience initializer for CPListItem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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