Skip to content

Commit 6725201

Browse files
authored
Specify custom image size when downloading Site Image (#19559)
2 parents 07b1ae8 + f3f719e commit 6725201

File tree

3 files changed

+118
-49
lines changed

3 files changed

+118
-49
lines changed

WordPress/Classes/Extensions/UIImageView+SiteIcon.swift

Lines changed: 54 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,14 @@ extension UIImageView {
1111
/// Default Settings
1212
///
1313
struct SiteIconDefaults {
14-
1514
/// Default SiteIcon's Image Size, in points.
1615
///
17-
static let imageSize = 40
18-
19-
/// Default SiteIcon's Image Size, in pixels.
20-
///
21-
static var imageSizeInPixels: Int {
22-
return imageSize * Int(UIScreen.main.scale)
23-
}
16+
static let imageSize = CGSize(width: 40, height: 40)
2417
}
2518

2619

2720
/// Downloads the SiteIcon Image, hosted at the specified path. This method will attempt to optimize the URL, so that
28-
/// the download Image Size matches `SiteIconDefaults.imageSize`.
21+
/// the download Image Size matches `imageSize`.
2922
///
3023
/// TODO: This is a convenience method. Nuke me once we're all swifted.
3124
///
@@ -38,35 +31,42 @@ extension UIImageView {
3831

3932

4033
/// Downloads the SiteIcon Image, hosted at the specified path. This method will attempt to optimize the URL, so that
41-
/// the download Image Size matches `SiteIconDefaults.imageSize`.
34+
/// the download Image Size matches `imageSize`.
4235
///
4336
/// - Parameters:
4437
/// - path: SiteIcon's url (string encoded) to be downloaded.
38+
/// - imageSize: Request site icon in the specified image size.
4539
/// - placeholderImage: Yes. It's the "place holder image", Sherlock.
4640
///
4741
@objc
48-
func downloadSiteIcon(at path: String, placeholderImage: UIImage?) {
49-
guard let siteIconURL = optimizedURL(for: path) else {
42+
func downloadSiteIcon(
43+
at path: String,
44+
imageSize: CGSize = SiteIconDefaults.imageSize,
45+
placeholderImage: UIImage?
46+
) {
47+
guard let siteIconURL = optimizedURL(for: path, imageSize: imageSize) else {
5048
image = placeholderImage
5149
return
5250
}
5351

5452
logURLOptimization(from: path, to: siteIconURL)
5553

5654
let request = URLRequest(url: siteIconURL)
57-
downloadSiteIcon(with: request, placeholderImage: placeholderImage)
55+
downloadSiteIcon(with: request, imageSize: imageSize, placeholderImage: placeholderImage)
5856
}
5957

6058
/// Downloads a SiteIcon image, using a specified request.
6159
///
6260
/// - Parameters:
63-
/// - request: the request for the SiteIcon.
61+
/// - request: The request for the SiteIcon.
62+
/// - imageSize: Request site icon in the specified image size.
6463
/// - placeholderImage: Yes. It's the "place holder image".
6564
///
6665
private func downloadSiteIcon(
6766
with request: URLRequest,
68-
placeholderImage: UIImage?) {
69-
67+
imageSize expectedSize: CGSize = SiteIconDefaults.imageSize,
68+
placeholderImage: UIImage?
69+
) {
7070
af_setImage(withURLRequest: request, placeholderImage: placeholderImage, completion: { [weak self] dataResponse in
7171
switch dataResponse.result {
7272
case .success(let image):
@@ -82,8 +82,6 @@ extension UIImageView {
8282
// The following lines of code ensure that we resize the image to the default Site Icon size, to
8383
// ensure there is no UI breakage due to having larger images set here.
8484
//
85-
let expectedSize = CGSize(width: SiteIconDefaults.imageSize, height: SiteIconDefaults.imageSize)
86-
8785
if image.size != expectedSize {
8886
self.image = image.resizedImage(with: .scaleAspectFill, bounds: expectedSize, interpolationQuality: .default)
8987
} else {
@@ -103,19 +101,21 @@ extension UIImageView {
103101

104102

105103
/// Downloads the SiteIcon Image, associated to a given Blog. This method will attempt to optimize the URL, so that
106-
/// the download Image Size matches `SiteIconDefaults.imageSize`.
104+
/// the download Image Size matches `imageSize`.
107105
///
108106
/// - Parameters:
109107
/// - blog: reference to the source blog
110108
/// - placeholderImage: Yes. It's the "place holder image".
111109
///
112-
@objc
113-
func downloadSiteIcon(for blog: Blog, placeholderImage: UIImage? = .siteIconPlaceholder) {
114-
guard let siteIconPath = blog.icon, let siteIconURL = optimizedURL(for: siteIconPath) else {
110+
@objc func downloadSiteIcon(
111+
for blog: Blog,
112+
imageSize: CGSize = SiteIconDefaults.imageSize,
113+
placeholderImage: UIImage? = .siteIconPlaceholder
114+
) {
115+
guard let siteIconPath = blog.icon, let siteIconURL = optimizedURL(for: siteIconPath, imageSize: imageSize) else {
115116

116117
if blog.isWPForTeams() && placeholderImage == .siteIconPlaceholder {
117-
let standardSize = CGSize(width: SiteIconDefaults.imageSize, height: SiteIconDefaults.imageSize)
118-
image = UIImage.gridicon(.p2, size: standardSize)
118+
image = UIImage.gridicon(.p2, size: imageSize)
119119
return
120120
}
121121

@@ -135,7 +135,7 @@ extension UIImageView {
135135
for: siteIconURL,
136136
from: host,
137137
onComplete: { [weak self] request in
138-
self?.downloadSiteIcon(with: request, placeholderImage: placeholderImage)
138+
self?.downloadSiteIcon(with: request, imageSize: imageSize, placeholderImage: placeholderImage)
139139
}) { error in
140140
DDLogError(error.localizedDescription)
141141
}
@@ -148,28 +148,28 @@ extension UIImageView {
148148
extension UIImageView {
149149
/// Returns the Size Optimized URL for a given Path.
150150
///
151-
func optimizedURL(for path: String) -> URL? {
151+
func optimizedURL(for path: String, imageSize: CGSize = SiteIconDefaults.imageSize) -> URL? {
152152
if isPhotonURL(path) || isDotcomURL(path) {
153-
return optimizedDotcomURL(from: path)
153+
return optimizedDotcomURL(from: path, imageSize: imageSize)
154154
}
155155

156156
if isBlavatarURL(path) {
157-
return optimizedBlavatarURL(from: path)
157+
return optimizedBlavatarURL(from: path, imageSize: imageSize)
158158
}
159159

160-
return optimizedPhotonURL(from: path)
160+
return optimizedPhotonURL(from: path, imageSize: imageSize)
161161
}
162162

163163

164164
// MARK: - Private Helpers
165165

166-
/// Returns the download URL for a square icon with a size of `SiteIconDefaults.imageSizeInPixels`
166+
/// Returns the download URL for a square icon with a size of `imageSize` in pixels.
167167
///
168168
/// - Parameter path: SiteIcon URL (string encoded).
169169
///
170-
private func optimizedDotcomURL(from path: String) -> URL? {
171-
let size = SiteIconDefaults.imageSizeInPixels
172-
let query = String(format: "w=%d&h=%d", size, size)
170+
private func optimizedDotcomURL(from path: String, imageSize: CGSize = SiteIconDefaults.imageSize) -> URL? {
171+
let size = imageSize.toPixels()
172+
let query = String(format: "w=%d&h=%d", Int(size.width), Int(size.height))
173173

174174
return parseURL(path: path, query: query)
175175
}
@@ -179,9 +179,9 @@ extension UIImageView {
179179
///
180180
/// - Parameter path: Blavatar URL (string encoded).
181181
///
182-
private func optimizedBlavatarURL(from path: String) -> URL? {
183-
let size = SiteIconDefaults.imageSizeInPixels
184-
let query = String(format: "d=404&s=%d", size)
182+
private func optimizedBlavatarURL(from path: String, imageSize: CGSize = SiteIconDefaults.imageSize) -> URL? {
183+
let size = imageSize.toPixels()
184+
let query = String(format: "d=404&s=%d", Int(max(size.width, size.height)))
185185

186186
return parseURL(path: path, query: query)
187187
}
@@ -191,13 +191,12 @@ extension UIImageView {
191191
///
192192
/// - Parameter siteIconPath: SiteIcon URL (string encoded).
193193
///
194-
private func optimizedPhotonURL(from path: String) -> URL? {
194+
private func optimizedPhotonURL(from path: String, imageSize: CGSize = SiteIconDefaults.imageSize) -> URL? {
195195
guard let url = URL(string: path) else {
196196
return nil
197197
}
198198

199-
let size = CGSize(width: SiteIconDefaults.imageSize, height: SiteIconDefaults.imageSize)
200-
return PhotonImageURLHelper.photonURL(with: size, forImageURL: url)
199+
return PhotonImageURLHelper.photonURL(with: imageSize, forImageURL: url)
201200
}
202201

203202

@@ -267,3 +266,19 @@ private extension UIImageView {
267266
DDLogInfo("URL optimized from \(original) to \(optimized.absoluteString) for blog \(blogInfo)")
268267
}
269268
}
269+
270+
// MARK: - CGFloat Extension
271+
272+
private extension CGSize {
273+
274+
func toPixels() -> CGSize {
275+
return CGSize(width: width.toPixels(), height: height.toPixels())
276+
}
277+
}
278+
279+
private extension CGFloat {
280+
281+
func toPixels() -> CGFloat {
282+
return self * UIScreen.main.scale
283+
}
284+
}

WordPress/Jetpack/Classes/ViewRelated/WordPress-to-Jetpack Migration/Welcome/MigrationWelcomeBlogTableViewCell.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ final class MigrationWelcomeBlogTableViewCell: UITableViewCell, Reusable {
7979
self.siteNameLabel.text = displayURL
8080
self.siteAddressLabel.text = nil
8181
}
82-
self.siteImageView.downloadSiteIcon(for: blog)
82+
self.siteImageView.downloadSiteIcon(for: blog, imageSize: Constants.imageSize)
8383
}
8484

8585
// MARK: - Types

WordPress/WordPressTest/SiteIconTests.swift

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ final class SiteIconTests: XCTestCase {
1111

1212
// MARK: - Test `optimizedURL(for:)`
1313

14-
/// Tests that a dotcom image URL is valid.
15-
func testDotcomURL() {
14+
/// Tests that a dotcom image URL with default image size is valid.
15+
func testDotcomURLWithDefaultSize() {
1616
// Given
17-
let path = "https://fake.files.wordpress.com/fake.png"
17+
let path = Constants.dotcomPath
1818

1919
// When
2020
let optimizedURL = imageView.optimizedURL(for: path)
@@ -25,10 +25,10 @@ final class SiteIconTests: XCTestCase {
2525
XCTAssertEqual(optimizedURL, expectedURL)
2626
}
2727

28-
/// Tests that a gravatar image URL is valid.
29-
func testBlavatarURL() {
28+
/// Tests that a gravatar image URL with default image size is valid.
29+
func testBlavatarURLWithDefaultSize() {
3030
// Given
31-
let path = "https://secure.gravatar.com/blavatar/123"
31+
let path = Constants.gravatarPath
3232

3333
// When
3434
let optimizedURL = imageView.optimizedURL(for: path)
@@ -39,10 +39,10 @@ final class SiteIconTests: XCTestCase {
3939
XCTAssertEqual(optimizedURL, expectedURL)
4040
}
4141

42-
/// Tests that a photon image URL is valid.
43-
func testPhotonURL() {
42+
/// Tests that a photon image URL with default image size is valid.
43+
func testPhotonURLWithDefaultSize() {
4444
// Given
45-
let path = "https://fake.wp.com/fake.png"
45+
let path = Constants.photonPath
4646

4747
// When
4848
let optimizedURL = imageView.optimizedURL(for: path)
@@ -52,4 +52,58 @@ final class SiteIconTests: XCTestCase {
5252
let expectedURL = URL(string: "\(path)?w=\(size)&h=\(size)")
5353
XCTAssertEqual(optimizedURL, expectedURL)
5454
}
55+
56+
/// Tests that a dotcom image URL with custom image size is valid.
57+
func testDotcomURLWithCustomSize() {
58+
// Given
59+
let sizeInPoints = Constants.customImageSize
60+
let path = Constants.dotcomPath
61+
62+
// When
63+
let optimizedURL = imageView.optimizedURL(for: path, imageSize: sizeInPoints)
64+
65+
// Then
66+
let size = Int(sizeInPoints.width) * Int(UIScreen.main.scale)
67+
let expectedURL = URL(string: "\(path)?w=\(size)&h=\(size)")
68+
XCTAssertEqual(optimizedURL, expectedURL)
69+
}
70+
71+
/// Tests that a gravatar image URL with custom image size is valid.
72+
func testBlavatarURLWithCustomSize() {
73+
// Given
74+
let sizeInPoints = Constants.customImageSize
75+
let path = Constants.gravatarPath
76+
77+
// When
78+
let optimizedURL = imageView.optimizedURL(for: path, imageSize: sizeInPoints)
79+
80+
// Then
81+
let size = Int(sizeInPoints.width) * Int(UIScreen.main.scale)
82+
let expectedURL = URL(string: "\(path)?d=404&s=\(size)")
83+
XCTAssertEqual(optimizedURL, expectedURL)
84+
}
85+
86+
/// Tests that a photon image URL with custom image size is valid.
87+
func testPhotonURLWithCustomSize() {
88+
// Given
89+
let sizeInPoints = Constants.customImageSize
90+
let path = Constants.photonPath
91+
92+
// When
93+
let optimizedURL = imageView.optimizedURL(for: path, imageSize: sizeInPoints)
94+
95+
// Then
96+
let size = Int(sizeInPoints.width) * Int(UIScreen.main.scale)
97+
let expectedURL = URL(string: "\(path)?w=\(size)&h=\(size)")
98+
XCTAssertEqual(optimizedURL, expectedURL)
99+
}
100+
101+
// MARK: - Constants
102+
103+
private struct Constants {
104+
static let customImageSize = CGSize(width: 60, height: 60)
105+
static let dotcomPath = "https://fake.files.wordpress.com/fake.png"
106+
static let gravatarPath = "https://secure.gravatar.com/blavatar/123"
107+
static let photonPath = "https://fake.wp.com/fake.png"
108+
}
55109
}

0 commit comments

Comments
 (0)