Skip to content

Commit ecbf1d4

Browse files
authored
Update Gravatar to 3.0.0 (#23701)
* Bump up to latest 3.0.0 revision * Update to latest commit * Adjust the update method * Update the commit hash * Set Gravatar to 3.0.0 * Add `squared()` for the image * Move squared() to Gravatar service * Add release notes * Add unit test for
1 parent 2d19990 commit ecbf1d4

File tree

7 files changed

+60
-8
lines changed

7 files changed

+60
-8
lines changed

Modules/Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ let package = Package(
2121
.package(url: "https://github.com/AliSoftware/OHHTTPStubs", from: "9.1.0"),
2222
.package(url: "https://github.com/Automattic/Automattic-Tracks-iOS", from: "3.4.2"),
2323
.package(url: "https://github.com/Automattic/AutomatticAbout-swift", from: "1.1.4"),
24-
.package(url: "https://github.com/Automattic/Gravatar-SDK-iOS", from: "2.1.0"),
24+
.package(url: "https://github.com/Automattic/Gravatar-SDK-iOS", from: "3.0.0"),
2525
.package(url: "https://github.com/Automattic/Gridicons-iOS", branch: "develop"),
2626
.package(url: "https://github.com/Automattic/ScreenObject", from: "0.2.3"),
2727
.package(url: "https://github.com/Automattic/XCUITestHelpers", from: "0.4.0"),

Modules/Sources/WordPressUI/Extensions/UIImage+Crop.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,20 @@ extension UIImage {
1818
}
1919
return self
2020
}
21+
22+
/// Crops the image in a perfectly square size. Longer edge is cropped out.
23+
/// If the image is already square, then just returns `self`.
24+
public func squared() -> UIImage {
25+
let currentSizeInPixels: CGSize = .init(width: size.width * scale, height: size.height * scale)
26+
guard currentSizeInPixels.width != currentSizeInPixels.height else { return self }
27+
let squareEdge = floor(min(currentSizeInPixels.width, currentSizeInPixels.height))
28+
let newSize = CGSize(width: squareEdge, height: squareEdge)
29+
let format = UIGraphicsImageRendererFormat()
30+
format.scale = 1
31+
let renderer = UIGraphicsImageRenderer(size: newSize, format: format)
32+
let squareImage = renderer.image { _ in
33+
self.draw(in: CGRect(origin: .zero, size: newSize))
34+
}
35+
return squareImage
36+
}
2137
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import XCTest
2+
import WordPressUI
3+
4+
final class UIImage_CropTests: XCTestCase {
5+
6+
func testSquared() throws {
7+
let image = try XCTUnwrap(createImage(size: .init(width: 96.0, height: 96.1)))
8+
let squareImage = image.squared()
9+
XCTAssertEqual(squareImage.size.width, squareImage.size.height)
10+
}
11+
12+
private func createImage(color: UIColor = .blue, size: CGSize) -> UIImage? {
13+
UIGraphicsBeginImageContextWithOptions(size, false, 0)
14+
color.setFill()
15+
UIRectFill(CGRectMake(0, 0, size.width, size.height))
16+
let image = UIGraphicsGetImageFromCurrentImageContext()
17+
UIGraphicsEndImageContext()
18+
return image
19+
}
20+
}

RELEASE-NOTES.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
25.6
2+
-----
3+
* [*] [internal] Update Gravatar SDK to 3.0.0 [#23701]
4+
15
25.5
26
-----
37

WordPress.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

WordPress/Classes/Services/GravatarService.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import Gravatar
88

99
public protocol GravatarImageUploader {
1010
@discardableResult
11-
func upload(_ image: UIImage, email: Email, accessToken: String) async throws -> URLResponse
11+
func upload(_ image: UIImage, selectionBehavior: AvatarSelection, accessToken: String) async throws -> AvatarType
1212
}
1313

1414
extension AvatarService: GravatarImageUploader { }
@@ -68,7 +68,10 @@ public class GravatarService {
6868

6969
Task {
7070
do {
71-
try await imageUploader.upload(image, email: Email(email), accessToken: accountToken)
71+
// The `/v3` gravatar upload endpoint expects the image to be a perfect square, otherwise fails.
72+
// Thus, we call `.squared()` (which will do nothing if the image is already square).
73+
// cropping(to: ...) sometimes generates edges a few pixels uneven. So `.squared()` will compensate.
74+
try await imageUploader.upload(image.squared(), selectionBehavior: .selectUploadedImage(for: Email(email)), accessToken: accountToken)
7275
DDLogInfo("GravatarService.uploadImage Success!")
7376
completion?(nil)
7477
} catch {

WordPress/WordPressTest/GravatarServiceTests.swift

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,23 @@ class GravatarServiceTests: CoreDataTestCase {
2020
}
2121
}
2222
}
23+
struct AvatarMock: AvatarType {
24+
var url: String
25+
var id: String
26+
}
2327

2428
class ImageServiceMock: GravatarImageUploader {
2529
var capturedAccountToken: String = ""
2630
var capturedAccountEmail: String = ""
27-
func upload(_ image: UIImage, email: Email, accessToken: String) async throws -> URLResponse {
28-
capturedAccountEmail = email.rawValue
31+
func upload(_ image: UIImage, selectionBehavior: AvatarSelection, accessToken: String) async throws -> AvatarType {
32+
switch selectionBehavior {
33+
case .selectUploadedImage(for: let email):
34+
capturedAccountEmail = email.rawValue
35+
default:
36+
break
37+
}
2938
capturedAccountToken = accessToken
30-
return URLResponse()
39+
return AvatarMock(url: "https://domain.com/avatar", id: "ID")
3140
}
3241
}
3342

0 commit comments

Comments
 (0)