Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Gravatar related deprecated types and replace with Gravatar SDK #23429

Merged
merged 8 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 0 additions & 148 deletions Modules/Sources/WordPressUI/Extensions/Gravatar/Gravatar.swift

This file was deleted.

101 changes: 0 additions & 101 deletions Modules/Sources/WordPressUI/Extensions/UIImageView+Gravatar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,6 @@ private class GravatarNotificationWrapper {
/// UIImageView Helper Methods that allow us to download a Gravatar, given the User's Email
///
extension UIImageView {

/// Downloads and sets the User's Gravatar, given his email.
/// TODO: This is a convenience method. Please, remove once all of the code has been migrated over to Swift.
///
/// - Parameters:
/// - email: the user's email
/// - rating: expected image rating
///
/// This method uses deprecated types. Please check the deprecation warning in `GravatarRatings`. Also check out the UIImageView extension from the Gravatar iOS SDK as an alternative to download images. See: https://github.com/Automattic/Gravatar-SDK-iOS.
@available(*, deprecated, message: "Usage of the deprecated type: GravatarRatings.")
@objc
public func downloadGravatarWithEmail(_ email: String, rating: GravatarRatings) {
downloadGravatarWithEmail(email, rating: rating, placeholderImage: .gravatarPlaceholderImage)
}

/// Downloads and sets the User's Gravatar, given his email.
///
/// - Parameters:
/// - email: the user's email
/// - rating: expected image rating
/// - placeholderImage: Image to be used as Placeholder
/// This method uses deprecated types. Please check the deprecation warning in `GravatarRatings`. Also check out the UIImageView extension from the Gravatar iOS SDK as an alternative to download images. See: https://github.com/Automattic/Gravatar-SDK-iOS.
@available(*, deprecated, message: "Usage of the deprecated type: GravatarRatings.")
@objc
public func downloadGravatarWithEmail(_ email: String, rating: GravatarRatings = .default, placeholderImage: UIImage = .gravatarPlaceholderImage) {
let gravatarURL = Gravatar.gravatarUrl(for: email, size: gravatarDefaultSize(), rating: rating)

listenForGravatarChanges(forEmail: email)
downloadImage(from: gravatarURL, placeholderImage: placeholderImage)
}

/// Configures the UIImageView to listen for changes to the gravatar it is displaying
public func listenForGravatarChanges(forEmail trackedEmail: String) {
if let currentObersver = gravatarWrapper?.observer {
Expand Down Expand Up @@ -83,76 +52,6 @@ extension UIImageView {
}
}

/// Downloads the provided Gravatar.
///
/// - Parameters:
/// - gravatar: the user's Gravatar
/// - placeholder: Image to be used as Placeholder
/// - animate: enable/disable fade in animation
/// - failure: Callback block to be invoked when an error occurs while fetching the Gravatar image
///
/// This method uses deprecated types. Please check the deprecation warning in `GravatarRatings`. Also check out the UIImageView extension from the Gravatar iOS SDK as an alternative to download images. See: https://github.com/Automattic/Gravatar-SDK-iOS.
@available(*, deprecated, message: "Usage of the deprecated type: Gravatar.")
public func downloadGravatar(_ gravatar: Gravatar?, placeholder: UIImage, animate: Bool, failure: ((Error?) -> Void)? = nil) {
guard let gravatar = gravatar else {
self.image = placeholder
return
}

// Starting with iOS 10, it seems `initWithCoder` uses a default size
// of 1000x1000, which was messing with our size calculations for gravatars
// on newly created table cells.
// Calling `layoutIfNeeded()` forces UIKit to calculate the actual size.
layoutIfNeeded()

let size = Int(ceil(frame.width * UIScreen.main.scale))
let url = gravatar.urlWithSize(size)

self.downloadImage(from: url,
placeholderImage: placeholder,
success: { image in
guard image != self.image else {
return
}

self.image = image
if animate {
self.fadeInAnimation()
}
}, failure: { error in
failure?(error)
})
}

/// Sets an Image Override in both, AFNetworking's Private Cache + NSURLCache
///
/// - Parameters:
/// - image: new UIImage
/// - rating: rating for the new image.
/// - email: associated email of the new gravatar
/// - Note: You may want to use `updateGravatar(image:, email:)` instead
///
/// *WHY* is this required?. *WHY* life has to be so complicated?, is the universe against us?
/// This has been implemented as a workaround. During Upload, we want any async calls made to the
/// `downloadGravatar` API to return the "Fresh" image.
///
/// Note II:
/// We cannot just clear NSURLCache, since the helper that's supposed to do that, is broken since iOS 8.
/// Ref: Ref: http://blog.airsource.co.uk/2014/10/11/nsurlcache-ios8-broken/
///
/// P.s.:
/// Hope buddah, and the code reviewer, can forgive me for this hack.
///
@available(*, deprecated, message: "Usage of the deprecated type: GravatarRatings.")
@objc public func overrideGravatarImageCache(_ image: UIImage, rating: GravatarRatings, email: String) {
guard let gravatarURL = Gravatar.gravatarUrl(for: email, size: gravatarDefaultSize(), rating: rating) else {
return
}

listenForGravatarChanges(forEmail: email)
overrideImageCache(for: gravatarURL, with: image)
}

/// Updates the gravatar image for the given email, and notifies all gravatar image views
///
/// - Parameters:
Expand Down
4 changes: 4 additions & 0 deletions WordPress/WordPress.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -2608,6 +2608,7 @@
912347762216E27200BD9F97 /* GutenbergViewController+Localization.swift in Sources */ = {isa = PBXBuildFile; fileRef = 912347752216E27200BD9F97 /* GutenbergViewController+Localization.swift */; };
91B0535D2B726F810073455C /* GravatarInfoRow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 91B0535C2B726F810073455C /* GravatarInfoRow.swift */; };
91B0535E2B726F810073455C /* GravatarInfoRow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 91B0535C2B726F810073455C /* GravatarInfoRow.swift */; };
91BE834E2C48FF0F00BB5B3B /* UIImageView+Additions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 91BE834D2C48FF0F00BB5B3B /* UIImageView+Additions.swift */; };
91D8364121946EFB008340B2 /* GutenbergMediaPickerHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 91D8364021946EFB008340B2 /* GutenbergMediaPickerHelper.swift */; };
91DCE84621A6A7F50062F134 /* PostEditor+MoreOptions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 91DCE84521A6A7F50062F134 /* PostEditor+MoreOptions.swift */; };
91DCE84821A6C58C0062F134 /* PostEditor+Publish.swift in Sources */ = {isa = PBXBuildFile; fileRef = 91DCE84721A6C58C0062F134 /* PostEditor+Publish.swift */; };
Expand Down Expand Up @@ -8268,6 +8269,7 @@
912347752216E27200BD9F97 /* GutenbergViewController+Localization.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "GutenbergViewController+Localization.swift"; sourceTree = "<group>"; };
9149D34BF5182F360C84EDB9 /* Pods-JetpackDraftActionExtension.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-JetpackDraftActionExtension.debug.xcconfig"; path = "../Pods/Target Support Files/Pods-JetpackDraftActionExtension/Pods-JetpackDraftActionExtension.debug.xcconfig"; sourceTree = "<group>"; };
91B0535C2B726F810073455C /* GravatarInfoRow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GravatarInfoRow.swift; sourceTree = "<group>"; };
91BE834D2C48FF0F00BB5B3B /* UIImageView+Additions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIImageView+Additions.swift"; sourceTree = "<group>"; };
91D8364021946EFB008340B2 /* GutenbergMediaPickerHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GutenbergMediaPickerHelper.swift; sourceTree = "<group>"; };
91DCE84521A6A7F50062F134 /* PostEditor+MoreOptions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "PostEditor+MoreOptions.swift"; sourceTree = "<group>"; };
91DCE84721A6C58C0062F134 /* PostEditor+Publish.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "PostEditor+Publish.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -13014,6 +13016,7 @@
4AD954542C2145CB00D0EEFA /* Login2FAViewController.swift */,
4AD954552C2145CB00D0EEFA /* LoginEmailViewController.swift */,
4AD954562C2145CB00D0EEFA /* LoginLinkRequestViewController.swift */,
91BE834D2C48FF0F00BB5B3B /* UIImageView+Additions.swift */,
4AD954572C2145CB00D0EEFA /* LoginNavigationController.swift */,
4AD954582C2145CB00D0EEFA /* LoginPrologueLoginMethodViewController.swift */,
4AD954592C2145CB00D0EEFA /* LoginProloguePageViewController.swift */,
Expand Down Expand Up @@ -23708,6 +23711,7 @@
4AD955172C2145CB00D0EEFA /* LoginLinkRequestViewController.swift in Sources */,
4AD954CC2C2145CB00D0EEFA /* IDToken.swift in Sources */,
4AD9550C2C2145CB00D0EEFA /* WordPressComAccountService.swift in Sources */,
91BE834E2C48FF0F00BB5B3B /* UIImageView+Additions.swift in Sources */,
4AD9550E2C2145CB00D0EEFA /* WordPressComOAuthClientFacade.swift in Sources */,
4AD954CD2C2145CB00D0EEFA /* JSONWebToken.swift in Sources */,
4AD954EB2C2145CB00D0EEFA /* NUXKeyboardResponder.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import UIKit
import WordPressShared
import WordPressUI
import GravatarUI

/// Step one in the auth link flow. This VC displays a form to request a "magic"
/// authentication link be emailed to the user. Allows the user to signin via
Expand Down Expand Up @@ -36,7 +37,9 @@ class LoginLinkRequestViewController: LoginViewController {

let email = loginFields.username
if email.isValidEmail() {
gravatarView?.downloadGravatarWithEmail(email, rating: .x)
Task {
try await gravatarView?.setGravatarImage(with: email, placeholder: .gravatarPlaceholderImage, rating: .x)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't able to make this code run even though I followed the email link flow. Maybe I am missing something.

But this is the same method we call from GravatarEmailTableViewCell.swift. And I was able to test that.

}
} else {
gravatarView?.isHidden = true
}
Expand Down
21 changes: 21 additions & 0 deletions WordPressAuthenticator/Sources/Signin/UIImageView+Additions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import UIKit
import WordPressUI
import GravatarUI

extension UIImageView {
func setGravatarImage(with email: String, placeholder: UIImage? = nil, rating: Rating = .general, preferredSize: CGSize? = nil) async throws {
var options: [ImageSettingOption]?
if let cache = WordPressUI.ImageCache.shared as? Gravatar.ImageCaching {
options = [.imageCache(cache)]
}
else {
assertionFailure("WordPressUI.ImageCache.shared should conform to Gravatar.ImageCaching")
}
try await gravatar.setImage(avatarID: .email(email),
placeholder: placeholder,
rating: .x,
preferredSize: preferredSize,
defaultAvatarOption: .status404,
options: options)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ class GravatarEmailTableViewCell: UITableViewCell {
return
}

gravatarImageView?.downloadGravatarWithEmail(email, placeholderImage: placeholderImage ?? gridicon)
Task {
try await gravatarImageView?.setGravatarImage(with: email, placeholder: placeholderImage ?? gridicon, preferredSize: gridicon.size)
}

gravatarImageViewSizeConstraints.forEach { constraint in
constraint.constant = gridicon.size.width
Expand Down