Skip to content

Commit 00127d4

Browse files
authored
Merge pull request #48 from hyperoslo/improve/code-style
Improve overall code style and move type aliases into one file
2 parents def3e62 + bba3090 commit 00127d4

21 files changed

+81
-95
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ cache:
66
- Carthage
77

88
before_install:
9-
- carthage bootstrap --platform iOS,Mac,tvOS
9+
- travis_wait 35; bin/bootstrap-if-needed
1010

1111
script:
1212
- xcodebuild clean build -project Imaginary.xcodeproj -scheme "Imaginary-Mac" -sdk macosx | xcpretty

Example/ImaginaryDemo/Podfile.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
PODS:
2-
- Cache (3.0.0):
2+
- Cache (3.1.0):
33
- SwiftHash (~> 1.4.0)
44
- Imaginary (1.0.2):
55
- Cache (~> 3.0)
@@ -13,7 +13,7 @@ EXTERNAL SOURCES:
1313
:path: "../../"
1414

1515
SPEC CHECKSUMS:
16-
Cache: 50fdd9dea71ba7d3efe39bf6bef555701509b16a
16+
Cache: 6dea29625ee40c1cafb2ff6c48f3f7341a59cfb5
1717
Imaginary: 3135892ca46c5126cf94df3340e10e267066bd12
1818
SwiftHash: acf0f29032309a5c7a7f625595eade1aca97aca2
1919

Example/ImaginaryForMac/Podfile.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
PODS:
2-
- Cache (3.0.0):
2+
- Cache (3.1.0):
33
- SwiftHash (~> 1.4.0)
44
- Imaginary (1.0.2):
55
- Cache (~> 3.0)
@@ -13,7 +13,7 @@ EXTERNAL SOURCES:
1313
:path: "../../"
1414

1515
SPEC CHECKSUMS:
16-
Cache: 50fdd9dea71ba7d3efe39bf6bef555701509b16a
16+
Cache: 6dea29625ee40c1cafb2ff6c48f3f7341a59cfb5
1717
Imaginary: 3135892ca46c5126cf94df3340e10e267066bd12
1818
SwiftHash: acf0f29032309a5c7a7f625595eade1aca97aca2
1919

Imaginary.xcodeproj/project.pbxproj

+10-16
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
BDA7AE451EEA925600E09EBC /* TypeAlias.swift in Sources */ = {isa = PBXBuildFile; fileRef = BDA7AE441EEA925600E09EBC /* TypeAlias.swift */; };
11+
BDA7AE461EEA925600E09EBC /* TypeAlias.swift in Sources */ = {isa = PBXBuildFile; fileRef = BDA7AE441EEA925600E09EBC /* TypeAlias.swift */; };
1012
BDE8FBF61D1BFC8D00C5A212 /* Cache.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = BDE8FBF51D1BFC8D00C5A212 /* Cache.framework */; };
11-
D2E649941D2108EA002F9730 /* Image.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2E649931D2108EA002F9730 /* Image.swift */; };
12-
D2E649951D2108EA002F9730 /* Image.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2E649931D2108EA002F9730 /* Image.swift */; };
13-
D2E649971D2108F6002F9730 /* ImageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2E649961D2108F6002F9730 /* ImageView.swift */; };
14-
D2E649981D2108F6002F9730 /* ImageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2E649961D2108F6002F9730 /* ImageView.swift */; };
1513
D2E6499D1D2109A2002F9730 /* Fetcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2E6499A1D2109A2002F9730 /* Fetcher.swift */; };
1614
D2E6499E1D2109A2002F9730 /* Fetcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2E6499A1D2109A2002F9730 /* Fetcher.swift */; };
1715
D2E649A01D210A74002F9730 /* Image+Cache.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2E6499F1D210A74002F9730 /* Image+Cache.swift */; };
@@ -37,11 +35,10 @@
3735
/* End PBXBuildFile section */
3836

3937
/* Begin PBXFileReference section */
38+
BDA7AE441EEA925600E09EBC /* TypeAlias.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TypeAlias.swift; sourceTree = "<group>"; };
4039
BDE8FBE91D1BFB9F00C5A212 /* Imaginary.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Imaginary.framework; sourceTree = BUILT_PRODUCTS_DIR; };
4140
BDE8FBF41D1BFC7C00C5A212 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
4241
BDE8FBF51D1BFC8D00C5A212 /* Cache.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cache.framework; path = Carthage/Build/Mac/Cache.framework; sourceTree = "<group>"; };
43-
D2E649931D2108EA002F9730 /* Image.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Image.swift; sourceTree = "<group>"; };
44-
D2E649961D2108F6002F9730 /* ImageView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ImageView.swift; sourceTree = "<group>"; };
4542
D2E6499A1D2109A2002F9730 /* Fetcher.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Fetcher.swift; sourceTree = "<group>"; };
4643
D2E6499F1D210A74002F9730 /* Image+Cache.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Image+Cache.swift"; sourceTree = "<group>"; };
4744
D2E649A21D210C1A002F9730 /* ImageView+Imaginary.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "ImageView+Imaginary.swift"; sourceTree = "<group>"; };
@@ -113,15 +110,14 @@
113110
BDE8FBD61D1BFA4F00C5A212 /* Shared */ = {
114111
isa = PBXGroup;
115112
children = (
116-
D2E6499A1D2109A2002F9730 /* Fetcher.swift */,
117113
D54225211D1D7A9300AF0046 /* Capsule.swift */,
118-
D54225221D1D7A9300AF0046 /* NSHTTPURLResponse+Imaginary.swift */,
119-
D2E649931D2108EA002F9730 /* Image.swift */,
120-
D2E649961D2108F6002F9730 /* ImageView.swift */,
121-
D2E6499F1D210A74002F9730 /* Image+Cache.swift */,
122-
D2E649A21D210C1A002F9730 /* ImageView+Imaginary.swift */,
123114
D2E649A91D210E37002F9730 /* Configuration.swift */,
115+
D2E6499A1D2109A2002F9730 /* Fetcher.swift */,
116+
D2E6499F1D210A74002F9730 /* Image+Cache.swift */,
124117
D59CC1371D79B23500D72C39 /* ImageDrawer.swift */,
118+
D2E649A21D210C1A002F9730 /* ImageView+Imaginary.swift */,
119+
D54225221D1D7A9300AF0046 /* NSHTTPURLResponse+Imaginary.swift */,
120+
BDA7AE441EEA925600E09EBC /* TypeAlias.swift */,
125121
);
126122
path = Shared;
127123
sourceTree = "<group>";
@@ -384,13 +380,12 @@
384380
D59CC1391D79B23500D72C39 /* ImageDrawer.swift in Sources */,
385381
D2E649A81D210CDE002F9730 /* Decompressor.swift in Sources */,
386382
D2E649A41D210C1A002F9730 /* ImageView+Imaginary.swift in Sources */,
387-
D2E649981D2108F6002F9730 /* ImageView.swift in Sources */,
388383
D2E649B01D211067002F9730 /* Configuration+Imaginary.swift in Sources */,
389384
D54225251D1D7A9600AF0046 /* Capsule.swift in Sources */,
390385
D2E649AB1D210E37002F9730 /* Configuration.swift in Sources */,
391-
D2E649951D2108EA002F9730 /* Image.swift in Sources */,
392386
D2E6499E1D2109A2002F9730 /* Fetcher.swift in Sources */,
393387
D2E649A11D210A74002F9730 /* Image+Cache.swift in Sources */,
388+
BDA7AE461EEA925600E09EBC /* TypeAlias.swift in Sources */,
394389
D54225371D1D7AF100AF0046 /* NSImage+CGImage.swift in Sources */,
395390
D54225261D1D7A9900AF0046 /* NSHTTPURLResponse+Imaginary.swift in Sources */,
396391
);
@@ -402,12 +397,11 @@
402397
files = (
403398
D59CC1341D79AF5B00D72C39 /* Drawers.swift in Sources */,
404399
D59CC1381D79B23500D72C39 /* ImageDrawer.swift in Sources */,
400+
BDA7AE451EEA925600E09EBC /* TypeAlias.swift in Sources */,
405401
D2E649AE1D21105F002F9730 /* Configuration+Imaginary.swift in Sources */,
406402
D2E649A31D210C1A002F9730 /* ImageView+Imaginary.swift in Sources */,
407403
D2E649A61D210CD4002F9730 /* Decompressor.swift in Sources */,
408-
D2E649941D2108EA002F9730 /* Image.swift in Sources */,
409404
D54225241D1D7A9300AF0046 /* NSHTTPURLResponse+Imaginary.swift in Sources */,
410-
D2E649971D2108F6002F9730 /* ImageView.swift in Sources */,
411405
D59CC1361D79B00E00D72C39 /* UIImage+Modify.swift in Sources */,
412406
D2E649A01D210A74002F9730 /* Image+Cache.swift in Sources */,
413407
D2E649AA1D210E37002F9730 /* Configuration.swift in Sources */,

Sources/Mac/Decompressor.swift

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
import Cocoa
22

33
struct Decompressor {
4-
54
static func decompress(_ data: Data, scale: CGFloat = 1) -> NSImage {
6-
guard let image = NSImage(data: data) else { return NSImage() }
5+
guard let image = NSImage(data: data) else {
6+
return NSImage()
7+
}
78

89
image.lockFocus()
910

1011
var imageRect: CGRect = CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)
11-
guard let imageRef = image.cgImage(forProposedRect: &imageRect, context: nil, hints: nil) else { return image }
12+
guard let imageRef = image.cgImage(forProposedRect: &imageRect, context: nil, hints: nil) else {
13+
return image
14+
}
1215

13-
if imageRef.alphaInfo != .none { return image }
16+
if imageRef.alphaInfo != .none {
17+
return image
18+
}
1419

1520
let width = imageRef.width
1621
let height = imageRef.height
@@ -26,7 +31,10 @@ struct Decompressor {
2631
bitsPerComponent: bitsPerComponent,
2732
bytesPerRow: bytesPerRow,
2833
space: colorSpaceRef,
29-
bitmapInfo: CGBitmapInfo.byteOrder32Big.rawValue) else { return image }
34+
bitmapInfo: CGBitmapInfo.byteOrder32Big.rawValue) else {
35+
return image
36+
37+
}
3038

3139
context.draw(imageRef, in: CGRect(x: 0, y: 0, width: CGFloat(width), height: CGFloat(height)))
3240

Sources/Mac/Extensions/Configuration+Imaginary.swift

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ import Cocoa
22
import Cache
33

44
public extension Configuration {
5-
6-
public static var preConfigure: ((_ imageView: NSImageView) -> Void)? = { imageView in
5+
public static var preConfigure: ((NSImageView) -> Void)? = { imageView in
76
imageView.layer?.opacity = 0.0
87
}
98

10-
public static var transitionClosure: ((_ imageView: NSImageView, _ image: NSImage) -> Void) = { imageView, newImage in
9+
public static var transitionClosure: ((NSImageView, NSImage) -> Void) = { imageView, newImage in
1110
guard let oldImage = imageView.image else {
1211
imageView.image = newImage
1312
return

Sources/Mac/Extensions/NSImage+CGImage.swift

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import Cocoa
22

33
extension NSImage {
4-
54
var cgImage: CGImage? {
65
guard let data = tiffRepresentation,
76
let source = CGImageSourceCreateWithData(data as CFData, nil),

Sources/Shared/Capsule.swift

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import Foundation
22

33
class Capsule: NSObject {
4-
54
static var ObjectKey = 0
65
let concept: Any
76

Sources/Shared/Configuration.swift

-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ import Foundation
22
import Cache
33

44
public struct Configuration {
5-
65
public static var bytesLoaded: Int = 0
7-
86
public static var imageCache: SpecializedCache<Image> = {
97
let config = Config(
108
expiry: Expiry.date(NSDate().addingTimeInterval(60 * 60 * 24 * 3) as Date),

Sources/Shared/Fetcher.swift

+15-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import Foundation
22

33
class Fetcher {
4-
54
enum Result {
65
case success(image: Image, byteCount: Int)
76
case failure(Error)
@@ -35,50 +34,53 @@ class Fetcher {
3534
active = true
3635

3736
DispatchQueue.global(qos: DispatchQoS.QoSClass.background).async { [weak self] in
38-
guard let weakSelf = self, weakSelf.active else { return }
37+
guard let `self` = self, self.active else {
38+
return
39+
}
3940

40-
weakSelf.task = weakSelf.session.dataTask(with: weakSelf.url, completionHandler: {
41-
[weak self] data, response, error -> Void in
41+
self.task = self.session.dataTask(with: self.url, completionHandler: { [weak self] data, response, error -> Void in
4242

43-
guard let weakSelf = self, weakSelf.active else { return }
43+
guard let `self` = self, self.active else {
44+
return
45+
}
4446

4547
if let error = error {
46-
weakSelf.complete { completion(.failure(error)) }
48+
self.complete { completion(.failure(error)) }
4749
return
4850
}
4951

5052
guard let httpResponse = response as? HTTPURLResponse else {
51-
weakSelf.complete { completion(.failure(Failure.invalidResponse)) }
53+
self.complete { completion(.failure(Failure.invalidResponse)) }
5254
return
5355
}
5456

5557
guard httpResponse.statusCode == 200 else {
56-
weakSelf.complete { completion(.failure(Failure.invalidStatusCode)) }
58+
self.complete { completion(.failure(Failure.invalidStatusCode)) }
5759
return
5860
}
5961

6062
guard let data = data, httpResponse.validateLength(data) else {
61-
weakSelf.complete { completion(.failure(Failure.invalidContentLength)) }
63+
self.complete { completion(.failure(Failure.invalidContentLength)) }
6264
return
6365
}
6466

6567
guard let decodedImage = Image.decode(data) else {
66-
weakSelf.complete { completion(.failure(Failure.conversionError)) }
68+
self.complete { completion(.failure(Failure.conversionError)) }
6769
return
6870
}
6971

7072
let image = preprocess(decodedImage)
7173

7274
Configuration.bytesLoaded += data.count
7375

74-
if weakSelf.active {
75-
weakSelf.complete {
76+
if self.active {
77+
self.complete {
7678
completion(Result.success(image: image, byteCount: data.count))
7779
}
7880
}
7981
})
8082

81-
weakSelf.task?.resume()
83+
self.task?.resume()
8284
}
8385
}
8486

Sources/Shared/Image+Cache.swift

-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ import Foundation
22
import Cache
33

44
extension Image {
5-
65
public typealias CacheType = Image
7-
86
public static func decode(_ data: Data) -> CacheType? {
97
return Decompressor.decompress(data as Data)
108
}

Sources/Shared/Image.swift

-9
This file was deleted.

Sources/Shared/ImageDrawer.swift

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
#if os(OSX)
2-
import Cocoa
3-
#else
4-
import UIKit
5-
#endif
1+
import CoreGraphics
62

73
public protocol ImageDrawer {
84
func draw(_ image: Image, context: CGContext, rect: CGRect)

Sources/Shared/ImageView+Imaginary.swift

+10-15
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import Foundation
22

3-
public typealias Preprocess = (Image) -> Image
4-
public typealias Completion = (Image?) -> Void
5-
63
extension ImageView {
7-
84
public func setImage(url: URL?,
95
placeholder: Image? = nil,
106
preprocess: @escaping Preprocess = { image in return image },
@@ -21,13 +17,13 @@ extension ImageView {
2117
}
2218

2319
Configuration.imageCache.async.object(forKey: url.absoluteString) { [weak self] (object: Image?) in
24-
guard let weakSelf = self else {
20+
guard let `self` = self else {
2521
return
2622
}
2723

2824
if let image = object {
2925
DispatchQueue.main.async {
30-
weakSelf.image = image
26+
self.image = image
3127
completion?(image)
3228
}
3329

@@ -36,35 +32,34 @@ extension ImageView {
3632

3733
if placeholder == nil {
3834
DispatchQueue.main.async {
39-
Configuration.preConfigure?(weakSelf)
35+
Configuration.preConfigure?(self)
4036
}
4137
}
4238

4339
DispatchQueue.main.async {
44-
weakSelf.fetchFromNetwork(url: url, preprocess: preprocess, completion: completion)
40+
self.fetchFromNetwork(url: url, preprocess: preprocess, completion: completion)
4541
}
4642
}
4743
}
4844

49-
fileprivate func fetchFromNetwork(url: URL,
50-
preprocess: @escaping Preprocess,
51-
completion: Completion? = nil) {
45+
fileprivate func fetchFromNetwork(url: URL, preprocess: @escaping Preprocess, completion: Completion? = nil) {
5246
fetcher = Fetcher(url: url)
53-
5447
fetcher?.start(preprocess) { [weak self] result in
55-
guard let weakSelf = self else { return }
48+
guard let `self` = self else {
49+
return
50+
}
5651

5752
switch result {
5853
case let .success(image, bytes):
5954
Configuration.track?(url, nil, bytes)
60-
Configuration.transitionClosure(weakSelf, image)
55+
Configuration.transitionClosure(self, image)
6156
Configuration.imageCache.async.addObject(image, forKey: url.absoluteString)
6257
completion?(image)
6358
case let .failure(error):
6459
Configuration.track?(url, error, 0)
6560
}
6661

67-
Configuration.postConfigure?(weakSelf)
62+
Configuration.postConfigure?(self)
6863
}
6964
}
7065

Sources/Shared/ImageView.swift

-9
This file was deleted.

Sources/Shared/NSHTTPURLResponse+Imaginary.swift

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import Foundation
22

33
extension HTTPURLResponse {
4-
54
func validateLength(_ data: Data) -> Bool {
65
return expectedContentLength > -1
76
? (Int64(data.count) >= expectedContentLength)

0 commit comments

Comments
 (0)