Skip to content

Commit

Permalink
Merge pull request #83 from Automattic/etoledom/rating-enum-pure-swift
Browse files Browse the repository at this point in the history
Make ImageRating enum pure swift
  • Loading branch information
etoledom authored Mar 1, 2024
2 parents 5c7a283 + 95f75ff commit a60da28
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 48 deletions.
11 changes: 5 additions & 6 deletions Demo/Demo/Gravatar-Demo/DemoAvatarDownloadViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class DemoAvatarDownloadViewController: UIViewController {
private lazy var gravatarRatingInputField: UITextField = {
let textField = UITextField()
textField.translatesAutoresizingMaskIntoConstraints = false
textField.placeholder = "Gravatar rating (optional). [0|1|2|3]"
textField.placeholder = "Gravatar rating (optional). [g|pg|r|x]"
textField.keyboardType = .numberPad
return textField
}()
Expand Down Expand Up @@ -117,12 +117,11 @@ class DemoAvatarDownloadViewController: UIViewController {
return Self.imageViewSize
}

private var preferredRating: GravatarRating? {
private var preferredRating: ImageRating? {
if let ratingStr = gravatarRatingInputField.text,
!ratingStr.isEmpty,
let ratingNo = Int(ratingStr)
!ratingStr.isEmpty
{
return GravatarRating(rawValue: ratingNo)
return ImageRating(rawValue: ratingStr)
}
return nil
}
Expand All @@ -148,7 +147,7 @@ class DemoAvatarDownloadViewController: UIViewController {

let options: GravatarImageDownloadOptions = .init(
preferredSize: .points(preferredSize),
gravatarRating: preferredRating,
rating: preferredRating,
forceRefresh: igonreCacheSwitchWithLabel.isOn,
forceDefaultImage: forceDefaultImageSwitchWithLabel.isOn,
defaultImage: preferredDefaultImage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ extension GravatarWrapper where Component: UIImageView {
public func setImage(
email: String,
placeholder: UIImage? = nil,
rating: GravatarRating? = nil,
rating: ImageRating? = nil,
preferredSize: CGSize? = nil,
options: [GravatarImageSettingOption]? = nil,
completionHandler: GravatarImageSetCompletion? = nil
Expand Down
10 changes: 5 additions & 5 deletions Sources/Gravatar/GravatarURL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ extension URL {
private enum ImageDownloadOptionQueryName: String, CaseIterable {
case defaultImage = "d"
case preferredPixelSize = "s"
case gravatarRating = "r"
case rating = "r"
case forceDefaultImage = "f"
}

Expand All @@ -138,8 +138,8 @@ extension GravatarImageDownloadOptions {
self.defaultImage.queryValue()
case .forceDefaultImage:
self.forceDefaultImage.queryValue()
case .gravatarRating:
self.gravatarRating.queryValue()
case .rating:
self.rating.queryValue()
case .preferredPixelSize:
self.preferredPixelSize.queryValue()
}
Expand All @@ -156,11 +156,11 @@ extension DefaultImageOption? {
}
}

extension GravatarRating? {
extension ImageRating? {
fileprivate func queryValue() -> String? {
guard let self else { return nil }

return self.stringValue()
return self.rawValue
}
}

Expand Down
18 changes: 9 additions & 9 deletions Sources/Gravatar/Options/GravatarOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ public struct GravatarImageSettingOptions {
}
}

func deriveDownloadOptions(garavatarRating rating: GravatarRating? = nil, preferredSize size: ImageSize? = nil) -> GravatarImageDownloadOptions {
func deriveDownloadOptions(garavatarRating rating: ImageRating? = nil, preferredSize size: ImageSize? = nil) -> GravatarImageDownloadOptions {
GravatarImageDownloadOptions(
preferredSize: size,
gravatarRating: rating,
rating: rating,
forceRefresh: forceRefresh,
processingMethod: processingMethod
)
Expand All @@ -67,7 +67,7 @@ public struct GravatarImageSettingOptions {

/// Download options to use outside of `GravatarCompatible` UI components. Refer to `GravatarImageSettingOption`.
public struct GravatarImageDownloadOptions {
let gravatarRating: GravatarRating?
let rating: ImageRating?
let forceRefresh: Bool
let forceDefaultImage: Bool?
let defaultImage: DefaultImageOption?
Expand All @@ -87,7 +87,7 @@ public struct GravatarImageDownloadOptions {
/// - processor: processor for handling the downloaded `Data`
public init(
preferredSize: ImageSize? = nil,
gravatarRating: GravatarRating? = nil,
rating: ImageRating? = nil,
forceRefresh: Bool = false,
forceDefaultImage: Bool? = nil,
defaultImage: DefaultImageOption? = nil,
Expand All @@ -96,7 +96,7 @@ public struct GravatarImageDownloadOptions {
self.init(
scaleFactor: UIScreen.main.scale,
preferredSize: preferredSize,
gravatarRating: gravatarRating,
rating: rating,
forceRefresh: forceRefresh,
forceDefaultImage: forceDefaultImage,
defaultImage: defaultImage,
Expand All @@ -107,13 +107,13 @@ public struct GravatarImageDownloadOptions {
private init(
scaleFactor: CGFloat,
preferredSize: ImageSize? = nil,
gravatarRating: GravatarRating? = nil,
rating: ImageRating? = nil,
forceRefresh: Bool = false,
forceDefaultImage: Bool? = nil,
defaultImage: DefaultImageOption? = nil,
processingMethod: ImageProcessingMethod
) {
self.gravatarRating = gravatarRating
self.rating = rating
self.forceRefresh = forceRefresh
self.forceDefaultImage = forceDefaultImage
self.processingMethod = processingMethod
Expand All @@ -134,7 +134,7 @@ public struct GravatarImageDownloadOptions {
func updating(
scaleFactor: CGFloat? = nil,
preferredSize: ImageSize? = nil,
gravatarRating: GravatarRating? = nil,
rating: ImageRating? = nil,
forceRefresh: Bool? = nil,
forceDefaultImage: Bool? = nil,
defaultImage: DefaultImageOption? = nil,
Expand All @@ -143,7 +143,7 @@ public struct GravatarImageDownloadOptions {
GravatarImageDownloadOptions(
scaleFactor: scaleFactor ?? self.scaleFactor,
preferredSize: preferredSize ?? self.preferredSize,
gravatarRating: gravatarRating ?? self.gravatarRating,
rating: rating ?? self.rating,
forceRefresh: forceRefresh ?? self.forceRefresh,
forceDefaultImage: forceDefaultImage ?? self.forceDefaultImage,
defaultImage: defaultImage ?? self.defaultImage,
Expand Down
25 changes: 0 additions & 25 deletions Sources/Gravatar/Options/GravatarRating.swift

This file was deleted.

11 changes: 11 additions & 0 deletions Sources/Gravatar/Options/ImageRating.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Foundation

/// Helper Enum that specifies all of the available Gravatar Image Ratings
///

public enum ImageRating: String {
case g
case pg
case r
case x
}
4 changes: 2 additions & 2 deletions Tests/GravatarTests/GravatarURLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ final class GravatarURLTests: XCTestCase {
"https://gravatar.com/avatar/676212ff796c79a3c06261eb10e3f455aa93998ee6e45263da13679c74b1e674?s=24"
)

let urlAddingRating = GravatarURL.gravatarUrl(with: exampleEmail, options: options.updating(gravatarRating: .pg))
let urlAddingRating = GravatarURL.gravatarUrl(with: exampleEmail, options: options.updating(rating: .pg))
XCTAssertEqual(
urlAddingRating?.absoluteString,
"https://gravatar.com/avatar/676212ff796c79a3c06261eb10e3f455aa93998ee6e45263da13679c74b1e674?r=pg"
Expand All @@ -99,7 +99,7 @@ final class GravatarURLTests: XCTestCase {

let allOptions = GravatarImageDownloadOptions(
preferredSize: .pixels(200),
gravatarRating: .g,
rating: .g,
forceDefaultImage: true,
defaultImage: .monsterId
)
Expand Down

0 comments on commit a60da28

Please sign in to comment.