From 2958d2b1acca776f64d8ccc5e36d17c077edcfdd Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sun, 22 Jan 2023 18:07:16 -0500 Subject: [PATCH] Change TrendingLinkCardCollectionViewCell to use CachedImageView --- .../TrendingLinkCardCollectionViewCell.swift | 39 ++------------- .../TrendingLinkCardCollectionViewCell.xib | 6 +-- Tusker/Views/CachedImageView.swift | 48 ++++++++++++++----- 3 files changed, 45 insertions(+), 48 deletions(-) diff --git a/Tusker/Screens/Explore/TrendingLinkCardCollectionViewCell.swift b/Tusker/Screens/Explore/TrendingLinkCardCollectionViewCell.swift index f9fdad85..55de1361 100644 --- a/Tusker/Screens/Explore/TrendingLinkCardCollectionViewCell.swift +++ b/Tusker/Screens/Explore/TrendingLinkCardCollectionViewCell.swift @@ -8,14 +8,13 @@ import UIKit import Pachyderm +import WebURLFoundationExtras class TrendingLinkCardCollectionViewCell: UICollectionViewCell { private var card: Card? - private var isGrayscale = false - private var thumbnailRequest: ImageCache.Request? - @IBOutlet weak var thumbnailView: UIImageView! + @IBOutlet weak var thumbnailView: CachedImageView! @IBOutlet weak var titleLabel: UILabel! @IBOutlet weak var descriptionLabel: UILabel! @IBOutlet weak var providerLabel: UILabel! @@ -27,14 +26,14 @@ class TrendingLinkCardCollectionViewCell: UICollectionViewCell { override func awakeFromNib() { super.awakeFromNib() + thumbnailView.cache = .attachments + layer.shadowOpacity = 0.2 layer.shadowRadius = 8 layer.shadowOffset = .zero layer.masksToBounds = false updateLayerColors() - NotificationCenter.default.addObserver(self, selector: #selector(updateUIForPreferences), name: .preferencesChanged, object: nil) - addGestureRecognizer(UIHoverGestureRecognizer(target: self, action: #selector(hoverRecognized))) } @@ -48,8 +47,7 @@ class TrendingLinkCardCollectionViewCell: UICollectionViewCell { self.card = card self.thumbnailView.image = nil - updateGrayscaleableUI(card: card) - updateUIForPreferences() + thumbnailView.update(for: card.image.flatMap { URL($0) }, blurhash: card.blurhash) let title = card.title.trimmingCharacters(in: .whitespacesAndNewlines) titleLabel.text = title @@ -77,33 +75,6 @@ class TrendingLinkCardCollectionViewCell: UICollectionViewCell { historyView.isHidden = card.history == nil || card.history!.count < 2 } - @objc private func updateUIForPreferences() { - if isGrayscale != Preferences.shared.grayscaleImages, - let card { - updateGrayscaleableUI(card: card) - } - } - - private func updateGrayscaleableUI(card: Card) { - isGrayscale = Preferences.shared.grayscaleImages - - if let imageURL = card.image, - let url = URL(imageURL) { - thumbnailRequest = ImageCache.attachments.get(url, completion: { _, image in - guard let image, - let transformedImage = ImageGrayscalifier.convertIfNecessary(url: url, image: image) else { - return - } - DispatchQueue.main.async { - self.thumbnailView.image = transformedImage - } - }) - if thumbnailRequest != nil { - loadBlurHash(card: card) - } - } - } - private func loadBlurHash(card: Card) { guard let hash = card.blurhash else { return diff --git a/Tusker/Screens/Explore/TrendingLinkCardCollectionViewCell.xib b/Tusker/Screens/Explore/TrendingLinkCardCollectionViewCell.xib index 53111e9b..6480291d 100644 --- a/Tusker/Screens/Explore/TrendingLinkCardCollectionViewCell.xib +++ b/Tusker/Screens/Explore/TrendingLinkCardCollectionViewCell.xib @@ -18,7 +18,7 @@ - + @@ -56,9 +56,9 @@ - + - +