From 64d8e6bc667b1fdcac3d738c006114fbd33a6436 Mon Sep 17 00:00:00 2001 From: Andrew Montgomery Date: Fri, 1 Mar 2024 15:23:21 -0600 Subject: [PATCH] Move pixels conversion into ImageSize extension Refactor ImageQueryOptions to use new .`pixels(:)` function --- Sources/Gravatar/Options/ImageQueryOptions.swift | 10 +--------- Sources/Gravatar/Options/ImageSize.swift | 11 +++++++++++ 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/Sources/Gravatar/Options/ImageQueryOptions.swift b/Sources/Gravatar/Options/ImageQueryOptions.swift index 4d21d6da..a5292d4a 100644 --- a/Sources/Gravatar/Options/ImageQueryOptions.swift +++ b/Sources/Gravatar/Options/ImageQueryOptions.swift @@ -45,15 +45,7 @@ public struct ImageQueryOptions { self.rating = rating self.forceDefaultImage = forceDefaultImage self.defaultImage = defaultImage - - switch preferredSize { - case .pixels(let pixels): - preferredPixelSize = pixels - case .points(let points): - preferredPixelSize = Int(points * scaleFactor) - case .none: - preferredPixelSize = nil - } + self.preferredPixelSize = preferredSize?.pixels(scaleFactor: scaleFactor) } } diff --git a/Sources/Gravatar/Options/ImageSize.swift b/Sources/Gravatar/Options/ImageSize.swift index 3799160b..de7cc6fb 100644 --- a/Sources/Gravatar/Options/ImageSize.swift +++ b/Sources/Gravatar/Options/ImageSize.swift @@ -13,3 +13,14 @@ public enum ImageSize { /// The returned image's size in pixels will be of the exact value passed here. case pixels(Int) } + +extension ImageSize { + func pixels(scaleFactor: CGFloat) -> Int { + switch self { + case .pixels(let pixels): + pixels + case .points(let points): + Int(points * scaleFactor) + } + } +}