From eed42002f30a973b0b79c10c4bd1e2925c630767 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Angel=20V=C3=A1zquez?= Date: Mon, 26 Sep 2016 23:04:26 -0400 Subject: [PATCH 1/4] Restrict icon sizes. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Restricted icon sizes to: - 18×18 - 24x24 - 36×36 - 48×48 If the user requests another size, an icon with the default size is returned. --- Gridicons/Gridicons/Gridicons.swift | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/Gridicons/Gridicons/Gridicons.swift b/Gridicons/Gridicons/Gridicons.swift index ca09ff6..41fe41e 100644 --- a/Gridicons/Gridicons/Gridicons.swift +++ b/Gridicons/Gridicons/Gridicons.swift @@ -156,6 +156,8 @@ public enum GridiconType: Int { public final class Gridicon: NSObject { public static let defaultSize = CGSize(width: 24.0, height: 24.0) + private static let availableSizes = [CGSize(width: 18.0, height: 18.0), defaultSize, CGSize(width: 36.0, height: 36.0), CGSize(width: 48.0, height: 48.0)] + private static let resizingBehavior = GridiconsGenerated.ResizingBehavior.AspectFit private static let cache = NSCache() @@ -170,18 +172,34 @@ public final class Gridicon: NSObject { // These are two separate methods (rather than one method with a default argument) because Obj-C - /// - returns: A template image of the specified Gridicon type, at the specified size. + /// - returns: A template image of the specified Gridicon type, at the default size. If the size + /// -----------specified by the user is not 18x18, 24x24, 36x36 or 48x48. public static func iconOfType(type: GridiconType, withSize size: CGSize) -> UIImage { if let icon = cachedIconOfType(type, withSize: size) { return icon } - let icon = generateIconOfType(type, withSize: size).imageWithRenderingMode(.AlwaysTemplate) - cache.setObject(icon, forKey: "\(type.rawValue)-\(size.width)-\(size.height)") + var icon = generateIconOfType(type, withSize: correctIconSize(size)) + + cache.setObject(icon, forKey: "\(type.rawValue)-\(icon.size.width)-\(icon.size.height)") return icon } + private static func correctIconSize(size: CGSize) -> CGSize{ + var correctSize = defaultSize + + if(size == availableSizes[0]) { + correctSize = size + } else if(size == availableSizes[2]) { + correctSize = size + } else if(size == availableSizes[3]) { + correctSize = size + } + + return correctSize + } + private static func cachedIconOfType(type: GridiconType, withSize size: CGSize) -> UIImage? { return cache.objectForKey("\(type.rawValue)-\(size.width)-\(size.height)") as? UIImage } @@ -488,4 +506,4 @@ public final class Gridicon: NSObject { return GridiconsGenerated.imageOfGridiconsaddimage(size: size, resizing: resizingBehavior) } } -} \ No newline at end of file +} From 606d3fb9f80ce9ecd10a41885cd85cf317cca15d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Angel=20V=C3=A1zquez?= Date: Mon, 26 Sep 2016 23:06:17 -0400 Subject: [PATCH 2/4] Restrict stepper. Stepper increments by available values. --- GridiconsDemo/GridiconsDemo/ViewController.swift | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/GridiconsDemo/GridiconsDemo/ViewController.swift b/GridiconsDemo/GridiconsDemo/ViewController.swift index b6ea5c7..1eaaff8 100644 --- a/GridiconsDemo/GridiconsDemo/ViewController.swift +++ b/GridiconsDemo/GridiconsDemo/ViewController.swift @@ -50,6 +50,12 @@ class ViewController: UIViewController { let value = Int(stepper.value) + if(value == 18) { + stepper.stepValue = 6 + } else { + stepper.stepValue = 12 + } + sizeLabel.text = "\(value)px" iconSize = CGSize(width: CGFloat(value), height: CGFloat(value)) @@ -73,4 +79,4 @@ extension ViewController: UICollectionViewDataSource { return cell } -} \ No newline at end of file +} From f6482611007c701390bbc9a0a1c4e45ed25ab7b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Angel=20V=C3=A1zquez?= Date: Tue, 27 Sep 2016 11:19:52 -0400 Subject: [PATCH 3/4] FORMAT. Removed warnings and added a blank line at the and (per guidelines). --- Gridicons/Gridicons/Gridicons.swift | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Gridicons/Gridicons/Gridicons.swift b/Gridicons/Gridicons/Gridicons.swift index 41fe41e..8c26c12 100644 --- a/Gridicons/Gridicons/Gridicons.swift +++ b/Gridicons/Gridicons/Gridicons.swift @@ -179,8 +179,7 @@ public final class Gridicon: NSObject { return icon } - var icon = generateIconOfType(type, withSize: correctIconSize(size)) - + let icon = generateIconOfType(type, withSize: correctIconSize(size)) cache.setObject(icon, forKey: "\(type.rawValue)-\(icon.size.width)-\(icon.size.height)") return icon From a61eaafbc557b4d9ede8151cbf2536cd67f394d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Angel=20V=C3=A1zquez?= Date: Tue, 27 Sep 2016 13:16:38 -0400 Subject: [PATCH 4/4] Changed testIconsAreTheCorrectSize(). If the user request a size other than 18x18, 24x24, 36x36 or 48x48 an icon with the default size will be returned. --- Gridicons/GridiconsTests/GridiconsTests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gridicons/GridiconsTests/GridiconsTests.swift b/Gridicons/GridiconsTests/GridiconsTests.swift index 48a4b85..c54be19 100644 --- a/Gridicons/GridiconsTests/GridiconsTests.swift +++ b/Gridicons/GridiconsTests/GridiconsTests.swift @@ -34,7 +34,7 @@ class GridiconsTests: XCTestCase { let size = CGSize(width: 250, height: 250) let icon2 = Gridicon.iconOfType(.UserCircle, withSize: size) - XCTAssertEqual(icon2.size, size) + XCTAssertEqual(icon2.size, Gridicon.defaultSize) } func testSingleIconGenerationPerformance() {