From 57714c767be6f332ff07d007ed741261ff8799da Mon Sep 17 00:00:00 2001 From: kean Date: Tue, 14 Jan 2025 11:53:19 -0500 Subject: [PATCH] Rework ImageSize convenience APIs --- Modules/Sources/AsyncImageKit/ImageRequest.swift | 9 +++++---- .../External/ExternalMediaPickerViewController.swift | 2 +- .../Views/NoteBlockHeaderTableViewCell.swift | 2 +- .../ViewRelated/Reader/Cards/ReaderCrossPostCell.swift | 2 +- .../ViewRelated/Reader/Cards/ReaderPostCell.swift | 2 +- 5 files changed, 9 insertions(+), 8 deletions(-) diff --git a/Modules/Sources/AsyncImageKit/ImageRequest.swift b/Modules/Sources/AsyncImageKit/ImageRequest.swift index 7f5caab7ba3c..c024e6fd848f 100644 --- a/Modules/Sources/AsyncImageKit/ImageRequest.swift +++ b/Modules/Sources/AsyncImageKit/ImageRequest.swift @@ -66,16 +66,17 @@ public struct ImageSize: Hashable, Sendable { self.height = Int(size.height) } - /// Initializes `ImageSize` with the given size in **points** scaled for the given view. + /// A convenience initializer that creates `ImageSize` with the given size + /// in **points** scaled for the given view. @MainActor public init(scaling size: CGSize, in view: UIView) { - self.init(pixels: size.scaled(by: view.traitCollection.displayScale)) + self.init(scaling: size, scale: view.traitCollection.displayScale) } /// Initializes `ImageSize` with the given size in **points** scaled for the /// current trait collection display scale. - public init(scaling size: CGSize) { - self.init(pixels: size.scaled(by: UITraitCollection.current.displayScale)) + public init(scaling size: CGSize, scale: CGFloat) { + self.init(pixels: size.scaled(by: max(1, scale))) } } diff --git a/WordPress/Classes/ViewRelated/Media/External/ExternalMediaPickerViewController.swift b/WordPress/Classes/ViewRelated/Media/External/ExternalMediaPickerViewController.swift index b0ad72b0d00a..d4def47f7823 100644 --- a/WordPress/Classes/ViewRelated/Media/External/ExternalMediaPickerViewController.swift +++ b/WordPress/Classes/ViewRelated/Media/External/ExternalMediaPickerViewController.swift @@ -236,7 +236,7 @@ final class ExternalMediaPickerViewController: UIViewController, UICollectionVie func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Self.cellReuseID, for: indexPath) as! ExternalMediaPickerCollectionCell let item = dataSource.assets[indexPath.item] - cell.configure(imageURL: item.thumbnailURL, size: ImageSize(scaling: flowLayout.itemSize)) + cell.configure(imageURL: item.thumbnailURL, size: ImageSize(scaling: flowLayout.itemSize, in: self.view)) return cell } diff --git a/WordPress/Classes/ViewRelated/Notifications/Views/NoteBlockHeaderTableViewCell.swift b/WordPress/Classes/ViewRelated/Notifications/Views/NoteBlockHeaderTableViewCell.swift index bba43f80f4f2..3668334b878f 100644 --- a/WordPress/Classes/ViewRelated/Notifications/Views/NoteBlockHeaderTableViewCell.swift +++ b/WordPress/Classes/ViewRelated/Notifications/Views/NoteBlockHeaderTableViewCell.swift @@ -71,7 +71,7 @@ class NoteBlockHeaderTableViewCell: NoteBlockTableViewCell { if let gravatar = AvatarURL(url: url) { authorAvatarImageView.downloadGravatar(gravatar, placeholder: .gravatarPlaceholderImage, animate: true) } else { - authorAvatarImageView.wp.setImage(with: url, size: ImageSize(scaling: SiteIconViewModel.Size.regular.size)) + authorAvatarImageView.wp.setImage(with: url, size: ImageSize(scaling: SiteIconViewModel.Size.regular.size, in: self)) } } diff --git a/WordPress/Classes/ViewRelated/Reader/Cards/ReaderCrossPostCell.swift b/WordPress/Classes/ViewRelated/Reader/Cards/ReaderCrossPostCell.swift index 923c06598f19..ae80009a558d 100644 --- a/WordPress/Classes/ViewRelated/Reader/Cards/ReaderCrossPostCell.swift +++ b/WordPress/Classes/ViewRelated/Reader/Cards/ReaderCrossPostCell.swift @@ -133,7 +133,7 @@ private final class ReaderCrossPostView: UIView { avatarView.setPlaceholder(UIImage(named: "post-blavatar-placeholder")) if let avatarURL = post.avatarURLForDisplay() { - let avatarSize = ImageSize(scaling: CGSize(width: avatarSize, height: avatarSize)) + let avatarSize = ImageSize(scaling: CGSize(width: avatarSize, height: avatarSize), in: self) avatarView.setImage(with: avatarURL, size: avatarSize) } } diff --git a/WordPress/Classes/ViewRelated/Reader/Cards/ReaderPostCell.swift b/WordPress/Classes/ViewRelated/Reader/Cards/ReaderPostCell.swift index 1c616f3cbb70..b727111fd7cb 100644 --- a/WordPress/Classes/ViewRelated/Reader/Cards/ReaderPostCell.swift +++ b/WordPress/Classes/ViewRelated/Reader/Cards/ReaderPostCell.swift @@ -360,7 +360,7 @@ private final class ReaderPostCellView: UIView { private func setAvatar(with viewModel: ReaderPostCellViewModel) { avatarView.setPlaceholder(UIImage(named: "post-blavatar-placeholder")) - let avatarSize = ImageSize(scaling: CGSize(width: ReaderPostCell.avatarSize, height: ReaderPostCell.avatarSize)) + let avatarSize = ImageSize(scaling: CGSize(width: ReaderPostCell.avatarSize, height: ReaderPostCell.avatarSize), in: self) if let avatarURL = viewModel.avatarURL { avatarView.setImage(with: avatarURL, size: avatarSize) } else {