From c68f8fe5aaa682fc8c17d4302eb04ab6fb61f519 Mon Sep 17 00:00:00 2001 From: kean Date: Tue, 14 Jan 2025 11:38:40 -0500 Subject: [PATCH 1/6] Update ImageSize to use Int --- Modules/Sources/AsyncImageKit/ImageRequest.swift | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Modules/Sources/AsyncImageKit/ImageRequest.swift b/Modules/Sources/AsyncImageKit/ImageRequest.swift index 5a4ada4df736..d4b019a73267 100644 --- a/Modules/Sources/AsyncImageKit/ImageRequest.swift +++ b/Modules/Sources/AsyncImageKit/ImageRequest.swift @@ -51,17 +51,17 @@ public struct ImageRequestOptions: Hashable, Sendable { /// Image size in **pixels**. public struct ImageSize: Hashable, Sendable { - public let width: CGFloat - public let height: CGFloat + public let width: Int + public let height: Int - public init(width: CGFloat, height: CGFloat) { - self.width = width - self.height = height + public init(width: Int, height: Int) { + self.width = Int(width) + self.height = Int(height) } public init(_ size: CGSize) { - self.width = size.width - self.height = size.height + self.width = Int(size.width) + self.height = Int(size.height) } /// Initializes `ImageSize` with the given size scaled for the given view. From a0abf4e0c7cda49ac04ad5756b4ab72ace4ce7c3 Mon Sep 17 00:00:00 2001 From: kean Date: Tue, 14 Jan 2025 11:41:27 -0500 Subject: [PATCH 2/6] Add ImageSize(pixels:) and document APIs --- Modules/Sources/AsyncImageKit/ImageRequest.swift | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Modules/Sources/AsyncImageKit/ImageRequest.swift b/Modules/Sources/AsyncImageKit/ImageRequest.swift index d4b019a73267..7f5caab7ba3c 100644 --- a/Modules/Sources/AsyncImageKit/ImageRequest.swift +++ b/Modules/Sources/AsyncImageKit/ImageRequest.swift @@ -54,26 +54,28 @@ public struct ImageSize: Hashable, Sendable { public let width: Int public let height: Int + /// Initializes the struct with given size in **pixels**. public init(width: Int, height: Int) { self.width = Int(width) self.height = Int(height) } - public init(_ size: CGSize) { + /// Initializes the struct with given size in **pixels**. + public init(pixels size: CGSize) { self.width = Int(size.width) self.height = Int(size.height) } - /// Initializes `ImageSize` with the given size scaled for the given view. + /// Initializes `ImageSize` with the given size in **points** scaled for the given view. @MainActor public init(scaling size: CGSize, in view: UIView) { - self.init(size.scaled(by: view.traitCollection.displayScale)) + self.init(pixels: size.scaled(by: view.traitCollection.displayScale)) } - /// Initializes `ImageSize` with the given size scaled for the current trait - /// collection display scale. + /// Initializes `ImageSize` with the given size in **points** scaled for the + /// current trait collection display scale. public init(scaling size: CGSize) { - self.init(size.scaled(by: UITraitCollection.current.displayScale)) + self.init(pixels: size.scaled(by: UITraitCollection.current.displayScale)) } } From 57714c767be6f332ff07d007ed741261ff8799da Mon Sep 17 00:00:00 2001 From: kean Date: Tue, 14 Jan 2025 11:53:19 -0500 Subject: [PATCH 3/6] 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 { From 6fdb460ad3c453249da8f6c2d97c40a3c0038751 Mon Sep 17 00:00:00 2001 From: kean Date: Tue, 14 Jan 2025 11:53:44 -0500 Subject: [PATCH 4/6] Make ImageSize properties writable --- Modules/Sources/AsyncImageKit/ImageRequest.swift | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Modules/Sources/AsyncImageKit/ImageRequest.swift b/Modules/Sources/AsyncImageKit/ImageRequest.swift index c024e6fd848f..76c64e6b02ea 100644 --- a/Modules/Sources/AsyncImageKit/ImageRequest.swift +++ b/Modules/Sources/AsyncImageKit/ImageRequest.swift @@ -51,8 +51,10 @@ public struct ImageRequestOptions: Hashable, Sendable { /// Image size in **pixels**. public struct ImageSize: Hashable, Sendable { - public let width: Int - public let height: Int + /// Width in **pixels**. + public var width: Int + /// Height in **pixels**. + public var height: Int /// Initializes the struct with given size in **pixels**. public init(width: Int, height: Int) { From 92837fba337368870309fb40e323e6cdbd3591c4 Mon Sep 17 00:00:00 2001 From: kean Date: Tue, 14 Jan 2025 12:01:10 -0500 Subject: [PATCH 5/6] Add note about deprecating passTouchesToSuperview --- Modules/Sources/AsyncImageKit/Views/AsyncImageView.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Modules/Sources/AsyncImageKit/Views/AsyncImageView.swift b/Modules/Sources/AsyncImageKit/Views/AsyncImageView.swift index 464490a0bfb2..df8c49bd1fcb 100644 --- a/Modules/Sources/AsyncImageKit/Views/AsyncImageView.swift +++ b/Modules/Sources/AsyncImageKit/Views/AsyncImageView.swift @@ -30,6 +30,7 @@ public final class AsyncImageView: UIView { /// By default, `background`. public var loadingStyle = LoadingStyle.background + // TODO: remove when WPRichTextImage is removed public var passTouchesToSuperview = false public init() {} From a0feb32acd3927f93d5cb69baed85d0c7ca724c1 Mon Sep 17 00:00:00 2001 From: kean Date: Tue, 14 Jan 2025 12:15:05 -0500 Subject: [PATCH 6/6] Fix stopAll --- Modules/Sources/AsyncImageKit/ImagePrefetcher.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Modules/Sources/AsyncImageKit/ImagePrefetcher.swift b/Modules/Sources/AsyncImageKit/ImagePrefetcher.swift index 623d60ac2aa2..7b807ec0dd99 100644 --- a/Modules/Sources/AsyncImageKit/ImagePrefetcher.swift +++ b/Modules/Sources/AsyncImageKit/ImagePrefetcher.swift @@ -89,6 +89,7 @@ public final class ImagePrefetcher { value.task?.cancel() } queue.removeAll() + numberOfActiveTasks = 0 } }