Skip to content

Commit

Permalink
Merge branch 'trunk' into etoledom/docs-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
etoledom committed Feb 21, 2024
2 parents 710c1db + 91cd113 commit 087374c
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Sources/Gravatar/Cache/GravatarImageCaching.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public protocol GravatarImageCaching {
func getImage(forKey key: String) -> UIImage?
}

public class GravatarImageCache: GravatarImageCaching {
class GravatarImageCache: GravatarImageCaching {
private let cache = NSCache<NSString, UIImage>()

/// The default cache used by the image dowloader.
Expand Down
2 changes: 1 addition & 1 deletion Sources/Gravatar/Network/ProfileRemote.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ struct ProfileRemote: Decodable {
let name: ProfileName?
}

public struct ProfileName: Decodable {
struct ProfileName: Decodable {
let givenName: String
let familyName: String
let formatted: String
Expand Down
10 changes: 5 additions & 5 deletions Sources/Gravatar/Network/Services/ImageService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ public struct ImageService: ImageServing {
private let client: HTTPClient
private let imageCache: GravatarImageCaching

public init(client: HTTPClient? = nil, cache: GravatarImageCaching = GravatarImageCache()) {
public init(client: HTTPClient? = nil, cache: GravatarImageCaching? = nil) {
self.client = client ?? URLSessionHTTPClient()
self.imageCache = cache
self.imageCache = cache ?? GravatarImageCache()
}

@discardableResult
Expand All @@ -31,7 +31,7 @@ public struct ImageService: ImageServing {
public func fetchImage(
with url: URL,
forceRefresh: Bool = false,
processor: GravatarImageProcessor = DefaultImageProcessor.common,
processor: ImageProcessor = DefaultImageProcessor.common,
completionHandler: ImageDownloadCompletion?
) -> CancellableDataTask? {
Task {
Expand Down Expand Up @@ -64,15 +64,15 @@ public struct ImageService: ImageServing {
public func fetchImage(
with url: URL,
forceRefresh: Bool = false,
processor: GravatarImageProcessor = DefaultImageProcessor.common
processor: ImageProcessor = DefaultImageProcessor.common
) async throws -> GravatarImageDownloadResult {
if !forceRefresh, let result = cachedImageResult(for: url) {
return result
}
return try await fetchImage(from: url, imageProcressor: processor)
}

private func fetchImage(from url: URL, imageProcressor: GravatarImageProcessor) async throws -> GravatarImageDownloadResult {
private func fetchImage(from url: URL, imageProcressor: ImageProcessor) async throws -> GravatarImageDownloadResult {
let request = URLRequest.imageRequest(url: url)
let (data, response) = try await client.fetchData(with: request)

Expand Down
4 changes: 2 additions & 2 deletions Sources/Gravatar/Network/Services/ImageServing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public protocol ImageServing {
func fetchImage(
with url: URL,
forceRefresh: Bool,
processor: GravatarImageProcessor,
processor: ImageProcessor,
completionHandler: ImageDownloadCompletion?
) -> CancellableDataTask?

Expand All @@ -24,6 +24,6 @@ public protocol ImageServing {
func fetchImage(
with url: URL,
forceRefresh: Bool,
processor: GravatarImageProcessor
processor: ImageProcessor
) async throws -> GravatarImageDownloadResult
}
39 changes: 21 additions & 18 deletions Sources/Gravatar/Options/GravatarOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,35 @@ import UIKit

/// Options to use when fetching a Gravatar profile image and setting it to a `GravatarCompatible` UI component.
public enum GravatarImageSettingOption {
// Transition style to use when setting the new image downloaded. Default: .none
/// Transition style to use when setting the new image downloaded. Default: .none
case transition(GravatarImageTransition)

// By setting this option, the current image will be removed and the placeholder will be shown while downloading the new image. Default: false
/// By setting this option, the current image will be removed and the placeholder will be shown while downloading the new image. Default: false
case removeCurrentImageWhileLoading

// Ignore the cached value and re-download the image. Default: false
/// Ignore the cached value and re-download the image. Default: `false`
case forceRefresh

// Processor to run on the the downloaded data while converting it into an image.
// If not set `DefaultImageProcessor.common` will be used.
case processor(GravatarImageProcessor)

// By setting this you can pass a cache of your preference to save the downloaded image. Default: GravatarImageCache.shared

/// Processor to run on the the downloaded data while converting it into an image.
///
/// If not set `DefaultImageProcessor.common` will be used.
case processor(ImageProcessor)

/// By setting this you can pass a cache of your preference to save the downloaded image.
///
///If not set, an internal cache will be used. To force images to not be cached, set the `forceRefresh` flag.
case imageCache(GravatarImageCaching)
// A custom image downloader. Defaults to `GravatarimageDownloader` if not set.

/// A custom image downloader. Defaults to `GravatarimageDownloader` if not set.
case imageDownloader(ImageServing)
}

// Parsed options derived from [GravatarImageSettingOption]
/// Parsed options derived from [GravatarImageSettingOption]
public struct GravatarImageSettingOptions {
var transition: GravatarImageTransition = .none
var removeCurrentImageWhileLoading = false
var forceRefresh = false
var processor: GravatarImageProcessor = DefaultImageProcessor.common
var processor: ImageProcessor = DefaultImageProcessor.common
var imageCache: GravatarImageCaching = GravatarImageCache.shared
var imageDownloader: ImageServing? = nil

Expand Down Expand Up @@ -62,13 +65,13 @@ public struct GravatarImageSettingOptions {
}
}

// Download options to use outside of `GravatarCompatible` UI components. Refer to `GravatarImageSettingOption`.
/// Download options to use outside of `GravatarCompatible` UI components. Refer to `GravatarImageSettingOption`.
public struct GravatarImageDownloadOptions {
let gravatarRating: GravatarRating?
let forceRefresh: Bool
let forceDefaultImage: Bool
let defaultImage: DefaultImageOption?
let processor: GravatarImageProcessor
let processor: ImageProcessor
let preferredPixelSize: Int?

private let preferredSize: ImageSize?
Expand All @@ -80,7 +83,7 @@ public struct GravatarImageDownloadOptions {
forceRefresh: Bool = false,
forceDefaultImage: Bool = false,
defaultImage: DefaultImageOption? = nil,
processor: GravatarImageProcessor = DefaultImageProcessor.common
processor: ImageProcessor = DefaultImageProcessor.common
) {
self.init(
scaleFactor: UIScreen.main.scale,
Expand All @@ -100,7 +103,7 @@ public struct GravatarImageDownloadOptions {
forceRefresh: Bool = false,
forceDefaultImage: Bool = false,
defaultImage: DefaultImageOption? = nil,
processor: GravatarImageProcessor = DefaultImageProcessor.common
processor: ImageProcessor = DefaultImageProcessor.common
) {
self.gravatarRating = gravatarRating
self.forceRefresh = forceRefresh
Expand All @@ -127,7 +130,7 @@ public struct GravatarImageDownloadOptions {
forceRefresh: Bool? = nil,
forceDefaultImage: Bool? = nil,
defaultImage: DefaultImageOption? = nil,
processor: GravatarImageProcessor? = nil
processor: ImageProcessor? = nil
) -> Self {
GravatarImageDownloadOptions(
scaleFactor: scaleFactor ?? self.scaleFactor,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import UIKit

/// The default processor. It applies the scale factor on the given image data and converts it into an image.
public struct DefaultImageProcessor: GravatarImageProcessor {
public struct DefaultImageProcessor: ImageProcessor {

public static let common = DefaultImageProcessor(scaleFactor: UIScreen.main.scale)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import Foundation
import UIKit

/// Processor to apply to the downloaded image data.
public protocol GravatarImageProcessor {
public protocol ImageProcessor {
func process(_ data: Data) -> UIImage?
}
2 changes: 1 addition & 1 deletion Tests/GravatarTests/GravatarImageCacheTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//

import XCTest
import Gravatar
@testable import Gravatar

final class GravatarImageCacheTests: XCTestCase {

Expand Down
2 changes: 1 addition & 1 deletion Tests/GravatarTests/GravatarOptionsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ final class GravatarOptionsTests: XCTestCase {
}
}

class TestImageProcessor: GravatarImageProcessor {
class TestImageProcessor: ImageProcessor {
var processedData = false
func process(_ data: Data) -> UIImage? {
processedData = true
Expand Down
4 changes: 2 additions & 2 deletions Tests/GravatarTests/TestImageRetriever.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class TestImageRetriever: ImageServing {
self.result = result
}

func fetchImage(with url: URL, forceRefresh: Bool, processor: Gravatar.GravatarImageProcessor, completionHandler: Gravatar.ImageDownloadCompletion?) -> Gravatar.CancellableDataTask? {
func fetchImage(with url: URL, forceRefresh: Bool, processor: Gravatar.ImageProcessor, completionHandler: Gravatar.ImageDownloadCompletion?) -> Gravatar.CancellableDataTask? {
completionQueue.append((url.absoluteString, completionHandler))
taskIdentifier += 1
return TestDataTask(taskIdentifier: taskIdentifier)
Expand All @@ -28,7 +28,7 @@ class TestImageRetriever: ImageServing {
return TestDataTask(taskIdentifier: taskIdentifier)
}

func fetchImage(with url: URL, forceRefresh: Bool, processor: Gravatar.GravatarImageProcessor) async throws -> Gravatar.GravatarImageDownloadResult {
func fetchImage(with url: URL, forceRefresh: Bool, processor: Gravatar.ImageProcessor) async throws -> Gravatar.GravatarImageDownloadResult {
fatalError("Not Implemented")
}

Expand Down

0 comments on commit 087374c

Please sign in to comment.