Skip to content

Commit

Permalink
Fix: Recommendation images loading issues (#1308)
Browse files Browse the repository at this point in the history
* RemoteImageView: Make sure received image is the same we're expecting

* AdRecommendationsGridView: Load images when dequeuing cells

* Remove calls to cancelLoading() from prepareForReuse()

* Add some newlines

* StandardAdRecommendationCell: Remove unused method
  • Loading branch information
bstien authored Jan 10, 2024
1 parent e34e06e commit 3a6b73f
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ public class RemoteImageView: UIImageView {
modify: ((UIImage?) -> UIImage?)? = nil
) {
cancelLoading()

self.imagePath = imagePath
self.imageWidth = imageWidth

guard let dataSource = dataSource else {
setImage(fallbackImage, animated: false)
return
Expand All @@ -51,6 +53,10 @@ public class RemoteImageView: UIImageView {
isLoadingImage = true
backgroundColor = loadingColor
dataSource.remoteImageView(self, loadImageWithPath: imagePath, imageWidth: imageWidth, completion: { [weak self] fetchedImage in
guard imagePath == self?.imagePath else {
return
}

let image = modify?(fetchedImage) ?? fetchedImage
self?.setImage(image ?? fallbackImage, animated: false, backgroundColor: loadedColor)
self?.isLoadingImage = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,6 @@ public class ExternalAdRecommendationCell: UICollectionViewCell, AdRecommendatio

public override func prepareForReuse() {
super.prepareForReuse()
imageView.cancelLoading()
imageView.image = nil
imageView.alpha = 0.0
imageView.contentMode = .scaleAspectFill
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,6 @@ public final class StandardAdRecommendationCell: UICollectionViewCell, AdRecomme

public override func prepareForReuse() {
super.prepareForReuse()
imageView.cancelLoading()
imageView.image = nil
imageView.alpha = 0.0
imageView.contentMode = .scaleAspectFill
Expand Down Expand Up @@ -382,22 +381,6 @@ public final class StandardAdRecommendationCell: UICollectionViewCell, AdRecomme
self.imageView.alpha = 1.0
}

private func setImage(_ image: UIImage?, animated: Bool) {
imageView.image = image

let performViewChanges = { [weak self] in
self?.imageView.alpha = 1.0
self?.imageContentView.backgroundColor = .clear
}

if animated {
imageView.alpha = 0.0
UIView.animate(withDuration: 0.2, delay: 0.0, options: [.curveEaseOut], animations: performViewChanges)
} else {
performViewChanges()
}
}

@objc private func handleFavoriteButtonTap(_ button: UIButton) {
delegate?.adRecommendationCell(self, didTapFavoriteButton: button)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,14 @@ extension AdRecommendationsGridView: UICollectionViewDataSource {
preconditionFailure("Data source not configured correctly")
}

return cell
}

public func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
if let cell = cell as? ImageLoadable {
cell.loadImage()
}

return cell
}

public func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
delegate?.adRecommendationsGridView(self, willDisplayItemAtIndex: indexPath.row)
}

Expand Down

0 comments on commit 3a6b73f

Please sign in to comment.