Skip to content

Commit

Permalink
Rename defaultImage as defaultImageOption (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
pinarol authored Mar 7, 2024
1 parent 173ae97 commit cb83f82
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class DemoAvatarDownloadViewController: UIViewController {
private lazy var imageDefaultButton: UIButton = {
let button = UIButton(type: .system)
button.translatesAutoresizingMaskIntoConstraints = false
button.setTitle("Default Image: (Backend driven)", for: .normal)
button.setTitle("Default Image Option: (Backend driven)", for: .normal)
button.addTarget(self, action: #selector(selectImageDefault), for: .touchUpInside)
return button
}()
Expand Down Expand Up @@ -129,12 +129,12 @@ class DemoAvatarDownloadViewController: UIViewController {
private var preferredDefaultImage: DefaultImageOption? = nil

@objc private func selectImageDefault() {
let controller = UIAlertController(title: "Default Image", message: nil, preferredStyle: .actionSheet)
let controller = UIAlertController(title: "Default Image Option", message: nil, preferredStyle: .actionSheet)

DefaultImageOption.allCases.forEach { option in
controller.addAction(UIAlertAction(title: "\(option)", style: .default) { [weak self] action in
self?.preferredDefaultImage = option
self?.imageDefaultButton.setTitle("Default Image: \(option)", for: .normal)
self?.imageDefaultButton.setTitle("Default Image Option: \(option)", for: .normal)
})
}

Expand All @@ -150,7 +150,7 @@ class DemoAvatarDownloadViewController: UIViewController {
rating: preferredRating,
forceRefresh: igonreCacheSwitchWithLabel.isOn,
forceDefaultImage: forceDefaultImageSwitchWithLabel.isOn,
defaultImage: preferredDefaultImage
defaultImageOption: preferredDefaultImage
)

avatarImageView.image = nil // Setting to nil to make the effect of `forceRefresh more visible
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class DemoUIImageViewExtensionViewController: UIViewController {
let placeholderImage: UIImage? = showPlaceholderSwitchWithLabel.isOn ? UIImage(named: "placeholder") : nil
avatarImageView.gravatar.setImage(email: emailInputField.text ?? "",
placeholder: placeholderImage,
defaultImage: preferredDefaultImage,
defaultImageOption: preferredDefaultImage,
options: options) { result in
switch result {
case .success(let result):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,15 @@ extension GravatarWrapper where Component: UIImageView {
placeholder: UIImage? = nil,
rating: ImageRating? = nil,
preferredSize: CGSize? = nil,
defaultImage: DefaultImageOption? = nil,
defaultImageOption: DefaultImageOption? = nil,
options: [GravatarImageSettingOption]? = nil,
completionHandler: GravatarImageSetCompletion? = nil
) -> CancellableDataTask? {
let pointsSize = pointImageSize(from: preferredSize)
let downloadOptions = GravatarImageSettingOptions(options: options).deriveDownloadOptions(
garavatarRating: rating,
preferredSize: pointsSize,
defaultImage: defaultImage
defaultImageOption: defaultImageOption
)

let gravatarURL = GravatarURL.gravatarUrl(with: email, options: downloadOptions.imageQueryOptions)
Expand Down
10 changes: 5 additions & 5 deletions Sources/Gravatar/Options/GravatarOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ public struct GravatarImageSettingOptions {
func deriveDownloadOptions(
garavatarRating rating: ImageRating? = nil,
preferredSize size: ImageSize? = nil,
defaultImage: DefaultImageOption? = nil
defaultImageOption: DefaultImageOption? = nil
) -> GravatarImageDownloadOptions {
GravatarImageDownloadOptions(
preferredSize: size,
rating: rating,
forceRefresh: forceRefresh,
defaultImage: defaultImage,
defaultImageOption: defaultImageOption,
processingMethod: processingMethod
)
}
Expand All @@ -84,14 +84,14 @@ public struct GravatarImageDownloadOptions {
/// - gravatarRating: maximum rating for image (set to `nil` for default rating)
/// - forceRefresh: force the image to be downloaded, ignoring the cache
/// - forceDefaultImage: force the default image to be used (set to `nil` for default value)
/// - defaultImage: configure the default image (set to `nil` to use the default default image)
/// - defaultImageOption: configure the default image (set to `nil` to use the default default image)
/// - processor: processor for handling the downloaded `Data`
public init(
preferredSize: ImageSize? = nil,
rating: ImageRating? = nil,
forceRefresh: Bool = false,
forceDefaultImage: Bool? = nil,
defaultImage: DefaultImageOption? = nil,
defaultImageOption: DefaultImageOption? = nil,
processingMethod: ImageProcessingMethod = .common
) {
self.forceRefresh = forceRefresh
Expand All @@ -101,7 +101,7 @@ public struct GravatarImageDownloadOptions {
self.imageQueryOptions = ImageQueryOptions(
preferredSize: preferredSize,
rating: rating,
defaultImage: defaultImage,
defaultImageOption: defaultImageOption,
forceDefaultImage: forceDefaultImage
)
}
Expand Down
18 changes: 9 additions & 9 deletions Sources/Gravatar/Options/ImageQueryOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import UIKit
public struct ImageQueryOptions {
let rating: ImageRating?
let forceDefaultImage: Bool?
let defaultImage: DefaultImageOption?
let defaultImageOption: DefaultImageOption?
let preferredPixelSize: Int?

/// Creating an instance of `ImageQueryOptions`.
Expand All @@ -17,20 +17,20 @@ public struct ImageQueryOptions {
/// - preferredSize: The preferred image size. Note that many users have lower resolution images, so requesting larger sizes may result in
/// pixelation/low-quality images.
/// - gravatarRating: The lowest rating allowed to be displayed. If the requested email hash does not have an image meeting the requested rating level,
/// - defaultImage: Choose what will happen if no Gravatar image is found. See ``DefaultImageOption`` for more info.
/// - defaultImageOption: Choose what will happen if no Gravatar image is found. See ``DefaultImageOption`` for more info.
/// then the default image is returned.
/// - forceDefaultImage: If set to `true`, the requested image will always be the default.
public init(
preferredSize: ImageSize? = nil,
rating: ImageRating? = nil,
defaultImage: DefaultImageOption? = nil,
defaultImageOption: DefaultImageOption? = nil,
forceDefaultImage: Bool? = nil
) {
self.init(
scaleFactor: UIScreen.main.scale,
rating: rating,
forceDefaultImage: forceDefaultImage,
defaultImage: defaultImage,
defaultImageOption: defaultImageOption,
preferredSize: preferredSize
)
}
Expand All @@ -39,12 +39,12 @@ public struct ImageQueryOptions {
scaleFactor: CGFloat,
rating: ImageRating? = nil,
forceDefaultImage: Bool? = nil,
defaultImage: DefaultImageOption? = nil,
defaultImageOption: DefaultImageOption? = nil,
preferredSize: ImageSize? = nil
) {
self.rating = rating
self.forceDefaultImage = forceDefaultImage
self.defaultImage = defaultImage
self.defaultImageOption = defaultImageOption
self.preferredPixelSize = preferredSize?.pixels(scaleFactor: scaleFactor)
}
}
Expand All @@ -53,7 +53,7 @@ public struct ImageQueryOptions {

extension ImageQueryOptions {
private enum QueryName: String, CaseIterable {
case defaultImage = "d"
case defaultImageOption = "d"
case preferredPixelSize = "s"
case rating = "r"
case forceDefaultImage = "f"
Expand All @@ -65,8 +65,8 @@ extension ImageQueryOptions {

private func queryItem(for queryName: QueryName) -> URLQueryItem? {
let value: String? = switch queryName {
case .defaultImage:
self.defaultImage.queryValue
case .defaultImageOption:
self.defaultImageOption.queryValue
case .forceDefaultImage:
self.forceDefaultImage.queryValue
case .rating:
Expand Down
18 changes: 9 additions & 9 deletions Tests/GravatarTests/GravatarURLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ final class GravatarURLTests: XCTestCase {
func testUrlWithDefaultImage() throws {
let url = GravatarURL(verifiedGravatarURL)
XCTAssertNotNil(url)
XCTAssertEqual(url?.url(with: ImageQueryOptions(defaultImage: .fileNotFound)).query, "d=404")
XCTAssertEqual(url?.url(with: ImageQueryOptions(defaultImage: .misteryPerson)).query, "d=mp")
XCTAssertEqual(url?.url(with: ImageQueryOptions(defaultImage: .monsterId)).query, "d=monsterid")
XCTAssertEqual(url?.url(with: ImageQueryOptions(defaultImage: .retro)).query, "d=retro")
XCTAssertEqual(url?.url(with: ImageQueryOptions(defaultImage: .roboHash)).query, "d=robohash")
XCTAssertEqual(url?.url(with: ImageQueryOptions(defaultImage: .transparentPNG)).query, "d=blank")
XCTAssertEqual(url?.url(with: ImageQueryOptions(defaultImage: .wavatar)).query, "d=wavatar")
XCTAssertEqual(url?.url(with: ImageQueryOptions(defaultImageOption: .fileNotFound)).query, "d=404")
XCTAssertEqual(url?.url(with: ImageQueryOptions(defaultImageOption: .misteryPerson)).query, "d=mp")
XCTAssertEqual(url?.url(with: ImageQueryOptions(defaultImageOption: .monsterId)).query, "d=monsterid")
XCTAssertEqual(url?.url(with: ImageQueryOptions(defaultImageOption: .retro)).query, "d=retro")
XCTAssertEqual(url?.url(with: ImageQueryOptions(defaultImageOption: .roboHash)).query, "d=robohash")
XCTAssertEqual(url?.url(with: ImageQueryOptions(defaultImageOption: .transparentPNG)).query, "d=blank")
XCTAssertEqual(url?.url(with: ImageQueryOptions(defaultImageOption: .wavatar)).query, "d=wavatar")
}

func testUrlWithForcedImageDefault() throws {
Expand All @@ -68,7 +68,7 @@ final class GravatarURLTests: XCTestCase {
"https://gravatar.com/avatar/676212ff796c79a3c06261eb10e3f455aa93998ee6e45263da13679c74b1e674"
)

let urlAddingDefaultImage = GravatarURL.gravatarUrl(with: exampleEmail, options: ImageQueryOptions(defaultImage: .identicon))
let urlAddingDefaultImage = GravatarURL.gravatarUrl(with: exampleEmail, options: ImageQueryOptions(defaultImageOption: .identicon))
XCTAssertEqual(
urlAddingDefaultImage?.absoluteString,
"https://gravatar.com/avatar/676212ff796c79a3c06261eb10e3f455aa93998ee6e45263da13679c74b1e674?d=identicon"
Expand All @@ -95,7 +95,7 @@ final class GravatarURLTests: XCTestCase {
let allOptions = ImageQueryOptions(
preferredSize: .pixels(200),
rating: .g,
defaultImage: .monsterId,
defaultImageOption: .monsterId,
forceDefaultImage: true
)
let urlAddingAllOptions = GravatarURL.gravatarUrl(with: exampleEmail, options: allOptions)
Expand Down
2 changes: 1 addition & 1 deletion Tests/GravatarTests/GravatarWrapper+UIImageViewTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ final class GravatarWrapper_UIImageViewTests: XCTestCase {

imageView.gravatar.setImage(
email: "[email protected]",
defaultImage: .roboHash,
defaultImageOption: .roboHash,
options: [.imageDownloader(imageDownloader)]
)

Expand Down
2 changes: 1 addition & 1 deletion Tests/GravatarTests/ImageServiceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ final class ImageServiceTests: XCTestCase {
let response = HTTPURLResponse.successResponse(with: URL(string: urlWithQuery)!)
let sessionMock = URLSessionMock(returnData: ImageHelper.testImageData, response: response)
let service = imageService(with: sessionMock)
let options = GravatarImageDownloadOptions(defaultImage: .misteryPerson)
let options = GravatarImageDownloadOptions(defaultImageOption: .misteryPerson)

let imageResponse = try await service.fetchImage(with: TestData.email, options: options)

Expand Down

0 comments on commit cb83f82

Please sign in to comment.