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

Gravatar SDK integration #156

Merged
merged 6 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,8 @@ _None._
### Internal Changes

- Add this changelog file [#119]

## 1.16.0

- Deprecates some types in favor of the new [Gravatar iOS SDK](https://github.com/Automattic/Gravatar-SDK-iOS)

7 changes: 7 additions & 0 deletions Sources/WordPressUI/Extensions/Gravatar/Gravatar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Foundation
/// Helper Enum that specifies all of the available Gravatar Image Ratings
/// TODO: Convert into a pure Swift String Enum. It's done this way to maintain ObjC Compatibility
///
@available(*, deprecated, message: "Use `Rating` from the Gravatar iOS SDK. See: https://github.com/Automattic/Gravatar-SDK-iOS.")
@objc
public enum GravatarRatings: Int {
case g
Expand Down Expand Up @@ -30,12 +31,14 @@ public enum GravatarRatings: Int {
/// Helper Enum that specifies some of the options for default images
/// To see all available options, visit : https://en.gravatar.com/site/implement/images/
///
@available(*, deprecated, message: "Use `DefaultAvatarOption` from the Gravatar iOS SDK. See: https://github.com/Automattic/Gravatar-SDK-iOS.")
public enum GravatarDefaultImage: String {
case fileNotFound = "404"
case mp
case identicon
}

@available(*, deprecated, message: "Use `AvatarURL` from the Gravatar iOS SDK. See: https://github.com/Automattic/Gravatar-SDK-iOS")
public struct Gravatar {
fileprivate struct Defaults {
static let scheme = "https"
Expand Down Expand Up @@ -107,13 +110,17 @@ public struct Gravatar {
}
}

@available(*, deprecated, message: "Usage of the deprecated type: Gravatar.")
extension Gravatar: Equatable {}

@available(*, deprecated, message: "Usage of the deprecated type: Gravatar.")
public func ==(lhs: Gravatar, rhs: Gravatar) -> Bool {
return lhs.canonicalURL == rhs.canonicalURL
}

@available(*, deprecated, message: "Usage of the deprecated type: Gravatar.")
public extension Gravatar {
@available(*, deprecated, message: "Usage of the deprecated type: Gravatar.")
init?(_ url: URL) {
guard Gravatar.isGravatarURL(url) else {
return nil
Expand Down
10 changes: 8 additions & 2 deletions Sources/WordPressUI/Extensions/UIImageView+Gravatar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ extension UIImageView {
/// - 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)
Expand All @@ -40,7 +42,8 @@ extension UIImageView {
/// - 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)
Expand All @@ -50,7 +53,7 @@ extension UIImageView {
}

/// Configures the UIImageView to listen for changes to the gravatar it is displaying
private func listenForGravatarChanges(forEmail trackedEmail: String) {
public func listenForGravatarChanges(forEmail trackedEmail: String) {
if let currentObersver = gravatarWrapper?.observer {
NotificationCenter.default.removeObserver(currentObersver)
gravatarWrapper = nil
Expand Down Expand Up @@ -88,6 +91,8 @@ extension UIImageView {
/// - 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
Expand Down Expand Up @@ -138,6 +143,7 @@ extension UIImageView {
/// 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
Expand Down
8 changes: 8 additions & 0 deletions Tests/WordPressUITests/Extensions/GravatarTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,42 @@ import XCTest
@testable import WordPressUI

class GravatarTest: XCTestCase {
@available(*, deprecated, message: "Deprecated because of Gravatar usage")
func testUnknownGravatarUrlMatchesURLWithSubdomainAndQueryParameters() {
let url = URL(string: "https://0.gravatar.com/avatar/ad516503a11cd5ca435acc9bb6523536?s=256&r=G")!
let gravatar = Gravatar(url)
XCTAssertNil(gravatar)
}

@available(*, deprecated, message: "Deprecated because of Gravatar usage")
func testUnknownGravatarUrlMatchesURLWithoutSubdomains() {
let url = URL(string: "https://0.gravatar.com/avatar/ad516503a11cd5ca435acc9bb6523536")!
let gravatar = Gravatar(url)
XCTAssertNil(gravatar)
}

@available(*, deprecated, message: "Deprecated because of Gravatar usage")
func testIsUnknownGravatarUrlMatchesURLWithHttpSchema() {
let url = URL(string: "http://0.gravatar.com/avatar/ad516503a11cd5ca435acc9bb6523536")!
let gravatar = Gravatar(url)
XCTAssertNil(gravatar)
}

@available(*, deprecated, message: "Deprecated because of Gravatar usage")
func testGravatarRejectsIncorrectPath() {
let url = URL(string: "http://0.gravatar.com/5b415e3c9c245e557af9f580eeb8760a")!
let gravatar = Gravatar(url)
XCTAssertNil(gravatar)
}

@available(*, deprecated, message: "Deprecated because of Gravatar usage")
func testGravatarRejectsIncorrectHost() {
let url = URL(string: "http://0.argvatar.com/avatar/5b415e3c9c245e557af9f580eeb8760a")!
let gravatar = Gravatar(url)
XCTAssertNil(gravatar)
}

@available(*, deprecated, message: "Deprecated because of Gravatar usage")
func testGravatarRemovesQueryParameters() {
let url = URL(string: "https://secure.gravatar.com/avatar/5b415e3c9c245e557af9f580eeb8760a?d=http://0.gravatar.com/5b415e3c9c245e557af9f580eeb8760a")!
let expected = URL(string: "https://secure.gravatar.com/avatar/5b415e3c9c245e557af9f580eeb8760a")!
Expand All @@ -40,6 +46,7 @@ class GravatarTest: XCTestCase {
XCTAssertEqual(gravatar!.canonicalURL, expected)
}

@available(*, deprecated, message: "Deprecated because of Gravatar usage")
func testGravatarForcesHTTPS() {
let url = URL(string: "http://0.gravatar.com/avatar/5b415e3c9c245e557af9f580eeb8760a")!
let expected = URL(string: "https://secure.gravatar.com/avatar/5b415e3c9c245e557af9f580eeb8760a")!
Expand All @@ -48,6 +55,7 @@ class GravatarTest: XCTestCase {
XCTAssertEqual(gravatar!.canonicalURL, expected)
}

@available(*, deprecated, message: "Deprecated because of Gravatar usage")
func testGravatarAppendsSizeQuery() {
let url = URL(string: "http://0.gravatar.com/avatar/5b415e3c9c245e557af9f580eeb8760a")!
let expected = URL(string: "https://secure.gravatar.com/avatar/5b415e3c9c245e557af9f580eeb8760a?s=128&d=404")!
Expand Down
2 changes: 1 addition & 1 deletion WordPressUI.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Pod::Spec.new do |s|
s.name = 'WordPressUI'
s.version = '1.15.1'
s.version = '1.16.0'

s.summary = 'Home of reusable WordPress UI components.'
s.description = <<-DESC
Expand Down