From 6f6ff9f3ecb863e2d6dc5547ca539828314038ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=BE=D0=BB=D0=B0=D0=B8=CC=86=20=D0=90?= =?UTF-8?q?=D1=88=D0=B0=D0=BD=D0=B8=D0=BD?= Date: Thu, 29 Sep 2016 17:51:52 +0300 Subject: [PATCH] review notes fixed --- LeadKit/.DS_Store | Bin 6148 -> 6148 bytes LeadKit/LeadKit.xcodeproj/project.pbxproj | 4 +++ .../String/String+Localization.swift | 22 +++++++++++++++ .../Extensions/UIImage/UIImage+Alpha.swift | 2 +- .../Extensions/UIImage/UIImage+Cropping.swift | 10 +++---- .../UIImage/UIImage+Gradients.swift | 9 +++--- .../Extensions/UIImage/UIImage+Loading.swift | 26 ++++++------------ .../Extensions/UIImage/UIImage+Resize.swift | 4 +-- .../UIImage/UIImage+Transformations.swift | 14 +++++----- .../UIImageView+LoadingImage.swift | 5 ++-- 10 files changed, 57 insertions(+), 39 deletions(-) create mode 100644 LeadKit/LeadKit/Extensions/String/String+Localization.swift diff --git a/LeadKit/.DS_Store b/LeadKit/.DS_Store index 36540d453b4d9c36e0702f3ad79d5fc0e927cb19..4048ccc14543e5957360688839c9f0c6ddf2bee4 100644 GIT binary patch delta 38 ucmZoMXffE}z{n(VeDXR*waFQbJWQw5Ca+=Cm^_b>n_1|T+U7Nk5n=%GkPH<7 delta 38 ucmZoMXffE}z{tcaJ$W6Y+T;vI9;SWVlh-h6OrFQc%`Dhzzj+N~gctzWs|yMM diff --git a/LeadKit/LeadKit.xcodeproj/project.pbxproj b/LeadKit/LeadKit.xcodeproj/project.pbxproj index b1f336a7..fc5b240d 100644 --- a/LeadKit/LeadKit.xcodeproj/project.pbxproj +++ b/LeadKit/LeadKit.xcodeproj/project.pbxproj @@ -57,6 +57,7 @@ 95B39A7E1D9C069B0057BD54 /* UIImage+Text.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95B39A7D1D9C069B0057BD54 /* UIImage+Text.swift */; }; 95B39A801D9C09440057BD54 /* UIImage+Cropping.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95B39A7F1D9C09440057BD54 /* UIImage+Cropping.swift */; }; 95B39A841D9C0C3E0057BD54 /* UIImage+Alpha.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95B39A831D9C0C3E0057BD54 /* UIImage+Alpha.swift */; }; + 95B39A861D9D51250057BD54 /* String+Localization.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95B39A851D9D51250057BD54 /* String+Localization.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -123,6 +124,7 @@ 95B39A7D1D9C069B0057BD54 /* UIImage+Text.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIImage+Text.swift"; sourceTree = ""; }; 95B39A7F1D9C09440057BD54 /* UIImage+Cropping.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIImage+Cropping.swift"; sourceTree = ""; }; 95B39A831D9C0C3E0057BD54 /* UIImage+Alpha.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIImage+Alpha.swift"; sourceTree = ""; }; + 95B39A851D9D51250057BD54 /* String+Localization.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "String+Localization.swift"; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -216,6 +218,7 @@ isa = PBXGroup; children = ( 787783661CA04D4A001CDC9B /* String+SizeCalculation.swift */, + 95B39A851D9D51250057BD54 /* String+Localization.swift */, ); path = String; sourceTree = ""; @@ -575,6 +578,7 @@ 78C36F771D80117D00E7EBEA /* UIImage+Transformations.swift in Sources */, 786D78EA1D53C43E006B2CEA /* ApiError.swift in Sources */, 787A071A1D085750009EC97F /* CellsControllerProtocol.swift in Sources */, + 95B39A861D9D51250057BD54 /* String+Localization.swift in Sources */, 78C36F7E1D801E3E00E7EBEA /* Double+Rounding.swift in Sources */, 78CFEE551C5C45E500F50370 /* NibNameProtocol.swift in Sources */, 78CFEE561C5C45E500F50370 /* ReuseIdentifierProtocol.swift in Sources */, diff --git a/LeadKit/LeadKit/Extensions/String/String+Localization.swift b/LeadKit/LeadKit/Extensions/String/String+Localization.swift new file mode 100644 index 00000000..98c45e4d --- /dev/null +++ b/LeadKit/LeadKit/Extensions/String/String+Localization.swift @@ -0,0 +1,22 @@ +// +// String+Localization.swift +// LeadKit +// +// Created by Николай Ашанин on 29.09.16. +// Copyright © 2016 Touch Instinct. All rights reserved. +// + +import UIKit + +public extension String { + + /** + method returns localized string with default comment and self name + + - returns: localized string + */ + public func localized() -> String { + return NSLocalizedString(self, comment: "") + } + +} diff --git a/LeadKit/LeadKit/Extensions/UIImage/UIImage+Alpha.swift b/LeadKit/LeadKit/Extensions/UIImage/UIImage+Alpha.swift index bb23247e..8f19bff8 100644 --- a/LeadKit/LeadKit/Extensions/UIImage/UIImage+Alpha.swift +++ b/LeadKit/LeadKit/Extensions/UIImage/UIImage+Alpha.swift @@ -27,7 +27,7 @@ public extension UIImage { - returns: a copy of the given image, adding an alpha channel if it doesn't already have one. */ public func applyAlpha() -> UIImage? { - if hasAlpha() { + guard !hasAlpha() else { return self } diff --git a/LeadKit/LeadKit/Extensions/UIImage/UIImage+Cropping.swift b/LeadKit/LeadKit/Extensions/UIImage/UIImage+Cropping.swift index 7b3ed8e5..e0d5a149 100644 --- a/LeadKit/LeadKit/Extensions/UIImage/UIImage+Cropping.swift +++ b/LeadKit/LeadKit/Extensions/UIImage/UIImage+Cropping.swift @@ -32,11 +32,11 @@ public extension UIImage { - returns: cropped image */ public func cropToSquare() -> UIImage? { - let newSize = CGSize(width: size.width * scale, height: size.height * scale) - let shortest = min(newSize.width, newSize.height) - let left: CGFloat = newSize.width > shortest ? (newSize.width-shortest)/2 : 0 - let top: CGFloat = newSize.height > shortest ? (newSize.height-shortest)/2 : 0 - let rect = CGRect(x: 0, y: 0, width: size.width, height: newSize.height) + let scaledSize = CGSize(width: size.width * scale, height: size.height * scale) + let shortest = min(scaledSize.width, scaledSize.height) + let left: CGFloat = scaledSize.width > shortest ? (scaledSize.width-shortest)/2 : 0 + let top: CGFloat = scaledSize.height > shortest ? (scaledSize.height-shortest)/2 : 0 + let rect = CGRect(x: 0, y: 0, width: scaledSize.width, height: scaledSize.height) let insetRect = CGRectInset(rect, left, top) return crop(insetRect) } diff --git a/LeadKit/LeadKit/Extensions/UIImage/UIImage+Gradients.swift b/LeadKit/LeadKit/Extensions/UIImage/UIImage+Gradients.swift index 09f15ec4..78c12d0e 100644 --- a/LeadKit/LeadKit/Extensions/UIImage/UIImage+Gradients.swift +++ b/LeadKit/LeadKit/Extensions/UIImage/UIImage+Gradients.swift @@ -23,7 +23,8 @@ public extension UIImage { let colorSpace = CGColorSpaceCreateDeviceRGB() let colors = gradientColors.map {(color: UIColor) -> AnyObject? in return color.CGColor as AnyObject? } as NSArray let gradient = CGGradientCreateWithColors(colorSpace, colors, nil) - CGContextDrawLinearGradient(context, gradient, + CGContextDrawLinearGradient(context, + gradient, CGPoint(x: 0, y: 0), CGPoint(x: 0, y: size.height), CGGradientDrawingOptions(rawValue: 0)) @@ -79,12 +80,12 @@ public extension UIImage { public convenience init?(startColor: UIColor, endColor: UIColor, radialGradientCenter: CGPoint = CGPoint(x: 0.5, y: 0.5), - radius: Float = 0.5, + radius: CGFloat = 0.5, size: CGSize = CGSize(width: 100, height: 100)) { UIGraphicsBeginImageContextWithOptions(size, true, 0) let numLocations: Int = 2 - let locations: [CGFloat] = [0.0, 1.0] as [CGFloat] + let locations: [CGFloat] = [0.0, 1.0] let startComponents = CGColorGetComponents(startColor.CGColor) let endComponents = CGColorGetComponents(endColor.CGColor) @@ -104,7 +105,7 @@ public extension UIImage { // Normalize the 0-1 ranged inputs to the width of the image let aCenter = CGPoint(x: radialGradientCenter.x * size.width, y: radialGradientCenter.y * size.height) - let aRadius = CGFloat(min(size.width, size.height)) * CGFloat(radius) + let aRadius = (min(size.width, size.height)) * (radius) // Draw it CGContextDrawRadialGradient(UIGraphicsGetCurrentContext(), diff --git a/LeadKit/LeadKit/Extensions/UIImage/UIImage+Loading.swift b/LeadKit/LeadKit/Extensions/UIImage/UIImage+Loading.swift index ef329849..6e54eb49 100644 --- a/LeadKit/LeadKit/Extensions/UIImage/UIImage+Loading.swift +++ b/LeadKit/LeadKit/Extensions/UIImage/UIImage+Loading.swift @@ -15,23 +15,13 @@ public enum UIImageContentMode { case ScaleToFill, ScaleAspectFit, ScaleAspectFill } -struct StaticSharedCache { - static var sharedCache: NSCache? = nil - static var onceToken: dispatch_once_t = 0 -} - public extension UIImage { /** a singleton shared NSURL cache used for images from URL - - - returns: shared image cache */ - private class func sharedCache() -> NSCache { - dispatch_once(&StaticSharedCache.onceToken) { - StaticSharedCache.sharedCache = NSCache() - } - return StaticSharedCache.sharedCache + static var sharedCache: NSCache = { + return NSCache() } // MARK: Image From URL @@ -39,23 +29,23 @@ public extension UIImage { /** creates a new image from a URL with optional caching. if cached, the cached image is returned. - otherwise, a place holder is used until the image from web is returned by the closure. + otherwise, a place holder is used until the image from web is returned by the fetchComplete. - parameter url: The image URL. - parameter placeholder: The placeholder image. - parameter shouldCacheImage: Weather or not we should cache the NSURL response (default: true) - - parameter closure: Returns the image from the web the first time is fetched. + - parameter fetchComplete: Returns the image from the web the first time is fetched. - returns: A new image */ public class func imageFromURL(url: String, placeholder: UIImage, shouldCacheImage: Bool = true, - closure: (image: UIImage?) -> ()) -> UIImage? { + fetchComplete: (image: UIImage?) -> ()) -> UIImage? { // From Cache if shouldCacheImage { if let image = UIImage.sharedCache().objectForKey(url) as? UIImage { - closure(image: nil) + fetchComplete(image: nil) return image } } @@ -65,7 +55,7 @@ public extension UIImage { session.dataTaskWithURL(nsURL, completionHandler: { (data, response, error) -> Void in if error != nil { dispatch_async(dispatch_get_main_queue()) { - closure(image: nil) + fetchComplete(image: nil) } } if let data = data, image = UIImage(data: data) { @@ -73,7 +63,7 @@ public extension UIImage { UIImage.sharedCache().setObject(image, forKey: url) } dispatch_async(dispatch_get_main_queue()) { - closure(image: image) + fetchComplete(image: image) } } session.finishTasksAndInvalidate() diff --git a/LeadKit/LeadKit/Extensions/UIImage/UIImage+Resize.swift b/LeadKit/LeadKit/Extensions/UIImage/UIImage+Resize.swift index 1e9a2213..46fedd49 100644 --- a/LeadKit/LeadKit/Extensions/UIImage/UIImage+Resize.swift +++ b/LeadKit/LeadKit/Extensions/UIImage/UIImage+Resize.swift @@ -38,8 +38,8 @@ public extension UIImage { */ public func resize(size: CGSize, contentMode: UIImageContentMode = .ScaleToFill) -> UIImage? { - let horizontalRatio = size.width/size.width - let verticalRatio = size.height/size.height + let horizontalRatio = size.width / size.width + let verticalRatio = size.height / size.height var ratio: CGFloat = 1 switch contentMode { diff --git a/LeadKit/LeadKit/Extensions/UIImage/UIImage+Transformations.swift b/LeadKit/LeadKit/Extensions/UIImage/UIImage+Transformations.swift index e45964b6..4fa8864b 100644 --- a/LeadKit/LeadKit/Extensions/UIImage/UIImage+Transformations.swift +++ b/LeadKit/LeadKit/Extensions/UIImage/UIImage+Transformations.swift @@ -58,13 +58,13 @@ public extension UIImage { CGContextSaveGState(context) CGContextTranslateCTM(context, rect.minX, rect.minY) CGContextScaleCTM(context, cornerRadius, cornerRadius) - let fw = rect.size.width / cornerRadius - let fh = rect.size.height / cornerRadius - CGContextMoveToPoint(context, fw, fh/2) - CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1) - CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1) - CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1) - CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1) + let roundedWidth = rect.size.width / cornerRadius + let roundedHeight = rect.size.height / cornerRadius + CGContextMoveToPoint(context, roundedWidth, roundedHeight/2) + CGContextAddArcToPoint(context, roundedWidth, roundedHeight, roundedWidth/2, roundedHeight, 1) + CGContextAddArcToPoint(context, 0, roundedHeight, 0, roundedHeight/2, 1) + CGContextAddArcToPoint(context, 0, 0, roundedWidth/2, 0, 1) + CGContextAddArcToPoint(context, roundedWidth, 0, roundedWidth, roundedHeight/2, 1) CGContextRestoreGState(context) } CGContextClosePath(context) diff --git a/LeadKit/LeadKit/Extensions/UIImageView/UIImageView+LoadingImage.swift b/LeadKit/LeadKit/Extensions/UIImageView/UIImageView+LoadingImage.swift index ab4d9dbb..4e1db836 100644 --- a/LeadKit/LeadKit/Extensions/UIImageView/UIImageView+LoadingImage.swift +++ b/LeadKit/LeadKit/Extensions/UIImageView/UIImageView+LoadingImage.swift @@ -31,12 +31,13 @@ public extension UIImageView { image = UIImage.imageFromURL(url, placeholder: placeholder, - shouldCacheImage: shouldCacheImage) { (uploadedImage: UIImage?) in + shouldCacheImage: shouldCacheImage) { [weak self] + (uploadedImage: UIImage?) in guard let image = uploadedImage else { return nil } - self.image = image + self?.image = image if fadeIn { let transition = CATransition() transition.duration = 0.5