Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve overall code style and move type aliases into one file #48

Merged
merged 3 commits into from
Jun 9, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Example/ImaginaryDemo/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PODS:
- Cache (3.0.0):
- Cache (3.1.0):
- SwiftHash (~> 1.4.0)
- Imaginary (1.0.2):
- Cache (~> 3.0)
Expand All @@ -13,7 +13,7 @@ EXTERNAL SOURCES:
:path: "../../"

SPEC CHECKSUMS:
Cache: 50fdd9dea71ba7d3efe39bf6bef555701509b16a
Cache: 6dea29625ee40c1cafb2ff6c48f3f7341a59cfb5
Imaginary: 3135892ca46c5126cf94df3340e10e267066bd12
SwiftHash: acf0f29032309a5c7a7f625595eade1aca97aca2

Expand Down
4 changes: 2 additions & 2 deletions Example/ImaginaryForMac/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PODS:
- Cache (3.0.0):
- Cache (3.1.0):
- SwiftHash (~> 1.4.0)
- Imaginary (1.0.2):
- Cache (~> 3.0)
Expand All @@ -13,7 +13,7 @@ EXTERNAL SOURCES:
:path: "../../"

SPEC CHECKSUMS:
Cache: 50fdd9dea71ba7d3efe39bf6bef555701509b16a
Cache: 6dea29625ee40c1cafb2ff6c48f3f7341a59cfb5
Imaginary: 3135892ca46c5126cf94df3340e10e267066bd12
SwiftHash: acf0f29032309a5c7a7f625595eade1aca97aca2

Expand Down
26 changes: 10 additions & 16 deletions Imaginary.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
objects = {

/* Begin PBXBuildFile section */
BDA7AE451EEA925600E09EBC /* TypeAlias.swift in Sources */ = {isa = PBXBuildFile; fileRef = BDA7AE441EEA925600E09EBC /* TypeAlias.swift */; };
BDA7AE461EEA925600E09EBC /* TypeAlias.swift in Sources */ = {isa = PBXBuildFile; fileRef = BDA7AE441EEA925600E09EBC /* TypeAlias.swift */; };
BDE8FBF61D1BFC8D00C5A212 /* Cache.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = BDE8FBF51D1BFC8D00C5A212 /* Cache.framework */; };
D2E649941D2108EA002F9730 /* Image.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2E649931D2108EA002F9730 /* Image.swift */; };
D2E649951D2108EA002F9730 /* Image.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2E649931D2108EA002F9730 /* Image.swift */; };
D2E649971D2108F6002F9730 /* ImageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2E649961D2108F6002F9730 /* ImageView.swift */; };
D2E649981D2108F6002F9730 /* ImageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2E649961D2108F6002F9730 /* ImageView.swift */; };
D2E6499D1D2109A2002F9730 /* Fetcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2E6499A1D2109A2002F9730 /* Fetcher.swift */; };
D2E6499E1D2109A2002F9730 /* Fetcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2E6499A1D2109A2002F9730 /* Fetcher.swift */; };
D2E649A01D210A74002F9730 /* Image+Cache.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2E6499F1D210A74002F9730 /* Image+Cache.swift */; };
Expand All @@ -37,11 +35,10 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
BDA7AE441EEA925600E09EBC /* TypeAlias.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TypeAlias.swift; sourceTree = "<group>"; };
BDE8FBE91D1BFB9F00C5A212 /* Imaginary.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Imaginary.framework; sourceTree = BUILT_PRODUCTS_DIR; };
BDE8FBF41D1BFC7C00C5A212 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
BDE8FBF51D1BFC8D00C5A212 /* Cache.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cache.framework; path = Carthage/Build/Mac/Cache.framework; sourceTree = "<group>"; };
D2E649931D2108EA002F9730 /* Image.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Image.swift; sourceTree = "<group>"; };
D2E649961D2108F6002F9730 /* ImageView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ImageView.swift; sourceTree = "<group>"; };
D2E6499A1D2109A2002F9730 /* Fetcher.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Fetcher.swift; sourceTree = "<group>"; };
D2E6499F1D210A74002F9730 /* Image+Cache.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Image+Cache.swift"; sourceTree = "<group>"; };
D2E649A21D210C1A002F9730 /* ImageView+Imaginary.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "ImageView+Imaginary.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -113,15 +110,14 @@
BDE8FBD61D1BFA4F00C5A212 /* Shared */ = {
isa = PBXGroup;
children = (
D2E6499A1D2109A2002F9730 /* Fetcher.swift */,
D54225211D1D7A9300AF0046 /* Capsule.swift */,
D54225221D1D7A9300AF0046 /* NSHTTPURLResponse+Imaginary.swift */,
D2E649931D2108EA002F9730 /* Image.swift */,
D2E649961D2108F6002F9730 /* ImageView.swift */,
D2E6499F1D210A74002F9730 /* Image+Cache.swift */,
D2E649A21D210C1A002F9730 /* ImageView+Imaginary.swift */,
D2E649A91D210E37002F9730 /* Configuration.swift */,
D2E6499A1D2109A2002F9730 /* Fetcher.swift */,
D2E6499F1D210A74002F9730 /* Image+Cache.swift */,
D59CC1371D79B23500D72C39 /* ImageDrawer.swift */,
D2E649A21D210C1A002F9730 /* ImageView+Imaginary.swift */,
D54225221D1D7A9300AF0046 /* NSHTTPURLResponse+Imaginary.swift */,
BDA7AE441EEA925600E09EBC /* TypeAlias.swift */,
);
path = Shared;
sourceTree = "<group>";
Expand Down Expand Up @@ -384,13 +380,12 @@
D59CC1391D79B23500D72C39 /* ImageDrawer.swift in Sources */,
D2E649A81D210CDE002F9730 /* Decompressor.swift in Sources */,
D2E649A41D210C1A002F9730 /* ImageView+Imaginary.swift in Sources */,
D2E649981D2108F6002F9730 /* ImageView.swift in Sources */,
D2E649B01D211067002F9730 /* Configuration+Imaginary.swift in Sources */,
D54225251D1D7A9600AF0046 /* Capsule.swift in Sources */,
D2E649AB1D210E37002F9730 /* Configuration.swift in Sources */,
D2E649951D2108EA002F9730 /* Image.swift in Sources */,
D2E6499E1D2109A2002F9730 /* Fetcher.swift in Sources */,
D2E649A11D210A74002F9730 /* Image+Cache.swift in Sources */,
BDA7AE461EEA925600E09EBC /* TypeAlias.swift in Sources */,
D54225371D1D7AF100AF0046 /* NSImage+CGImage.swift in Sources */,
D54225261D1D7A9900AF0046 /* NSHTTPURLResponse+Imaginary.swift in Sources */,
);
Expand All @@ -402,12 +397,11 @@
files = (
D59CC1341D79AF5B00D72C39 /* Drawers.swift in Sources */,
D59CC1381D79B23500D72C39 /* ImageDrawer.swift in Sources */,
BDA7AE451EEA925600E09EBC /* TypeAlias.swift in Sources */,
D2E649AE1D21105F002F9730 /* Configuration+Imaginary.swift in Sources */,
D2E649A31D210C1A002F9730 /* ImageView+Imaginary.swift in Sources */,
D2E649A61D210CD4002F9730 /* Decompressor.swift in Sources */,
D2E649941D2108EA002F9730 /* Image.swift in Sources */,
D54225241D1D7A9300AF0046 /* NSHTTPURLResponse+Imaginary.swift in Sources */,
D2E649971D2108F6002F9730 /* ImageView.swift in Sources */,
D59CC1361D79B00E00D72C39 /* UIImage+Modify.swift in Sources */,
D2E649A01D210A74002F9730 /* Image+Cache.swift in Sources */,
D2E649AA1D210E37002F9730 /* Configuration.swift in Sources */,
Expand Down
18 changes: 13 additions & 5 deletions Sources/Mac/Decompressor.swift
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import Cocoa

struct Decompressor {

static func decompress(_ data: Data, scale: CGFloat = 1) -> NSImage {
guard let image = NSImage(data: data) else { return NSImage() }
guard let image = NSImage(data: data) else {
return NSImage()
}

image.lockFocus()

var imageRect: CGRect = CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)
guard let imageRef = image.cgImage(forProposedRect: &imageRect, context: nil, hints: nil) else { return image }
guard let imageRef = image.cgImage(forProposedRect: &imageRect, context: nil, hints: nil) else {
return image
}

if imageRef.alphaInfo != .none { return image }
if imageRef.alphaInfo != .none {
return image
}

let width = imageRef.width
let height = imageRef.height
Expand All @@ -26,7 +31,10 @@ struct Decompressor {
bitsPerComponent: bitsPerComponent,
bytesPerRow: bytesPerRow,
space: colorSpaceRef,
bitmapInfo: CGBitmapInfo.byteOrder32Big.rawValue) else { return image }
bitmapInfo: CGBitmapInfo.byteOrder32Big.rawValue) else {
return image

}

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

Expand Down
5 changes: 2 additions & 3 deletions Sources/Mac/Extensions/Configuration+Imaginary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import Cocoa
import Cache

public extension Configuration {

public static var preConfigure: ((_ imageView: NSImageView) -> Void)? = { imageView in
public static var preConfigure: ((NSImageView) -> Void)? = { imageView in
imageView.layer?.opacity = 0.0
}

public static var transitionClosure: ((_ imageView: NSImageView, _ image: NSImage) -> Void) = { imageView, newImage in
public static var transitionClosure: ((NSImageView, NSImage) -> Void) = { imageView, newImage in
guard let oldImage = imageView.image else {
imageView.image = newImage
return
Expand Down
1 change: 0 additions & 1 deletion Sources/Mac/Extensions/NSImage+CGImage.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Cocoa

extension NSImage {

var cgImage: CGImage? {
guard let data = tiffRepresentation,
let source = CGImageSourceCreateWithData(data as CFData, nil),
Expand Down
1 change: 0 additions & 1 deletion Sources/Shared/Capsule.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Foundation

class Capsule: NSObject {

static var ObjectKey = 0
let concept: Any

Expand Down
2 changes: 0 additions & 2 deletions Sources/Shared/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import Foundation
import Cache

public struct Configuration {

public static var bytesLoaded: Int = 0

public static var imageCache: SpecializedCache<Image> = {
let config = Config(
expiry: Expiry.date(NSDate().addingTimeInterval(60 * 60 * 24 * 3) as Date),
Expand Down
28 changes: 15 additions & 13 deletions Sources/Shared/Fetcher.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Foundation

class Fetcher {

enum Result {
case success(image: Image, byteCount: Int)
case failure(Error)
Expand Down Expand Up @@ -35,50 +34,53 @@ class Fetcher {
active = true

DispatchQueue.global(qos: DispatchQoS.QoSClass.background).async { [weak self] in
guard let weakSelf = self, weakSelf.active else { return }
guard let `self` = self, self.active else {
return
}

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

guard let weakSelf = self, weakSelf.active else { return }
guard let `self` = self, self.active else {
return
}

if let error = error {
weakSelf.complete { completion(.failure(error)) }
self.complete { completion(.failure(error)) }
return
}

guard let httpResponse = response as? HTTPURLResponse else {
weakSelf.complete { completion(.failure(Failure.invalidResponse)) }
self.complete { completion(.failure(Failure.invalidResponse)) }
return
}

guard httpResponse.statusCode == 200 else {
weakSelf.complete { completion(.failure(Failure.invalidStatusCode)) }
self.complete { completion(.failure(Failure.invalidStatusCode)) }
return
}

guard let data = data, httpResponse.validateLength(data) else {
weakSelf.complete { completion(.failure(Failure.invalidContentLength)) }
self.complete { completion(.failure(Failure.invalidContentLength)) }
return
}

guard let decodedImage = Image.decode(data) else {
weakSelf.complete { completion(.failure(Failure.conversionError)) }
self.complete { completion(.failure(Failure.conversionError)) }
return
}

let image = preprocess(decodedImage)

Configuration.bytesLoaded += data.count

if weakSelf.active {
weakSelf.complete {
if self.active {
self.complete {
completion(Result.success(image: image, byteCount: data.count))
}
}
})

weakSelf.task?.resume()
self.task?.resume()
}
}

Expand Down
2 changes: 0 additions & 2 deletions Sources/Shared/Image+Cache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import Foundation
import Cache

extension Image {

public typealias CacheType = Image

public static func decode(_ data: Data) -> CacheType? {
return Decompressor.decompress(data as Data)
}
Expand Down
9 changes: 0 additions & 9 deletions Sources/Shared/Image.swift

This file was deleted.

6 changes: 1 addition & 5 deletions Sources/Shared/ImageDrawer.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
#if os(OSX)
import Cocoa
#else
import UIKit
#endif
import CoreGraphics

public protocol ImageDrawer {
func draw(_ image: Image, context: CGContext, rect: CGRect)
Expand Down
25 changes: 10 additions & 15 deletions Sources/Shared/ImageView+Imaginary.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import Foundation

public typealias Preprocess = (Image) -> Image
public typealias Completion = (Image?) -> Void

extension ImageView {

public func setImage(url: URL?,
placeholder: Image? = nil,
preprocess: @escaping Preprocess = { image in return image },
Expand All @@ -21,13 +17,13 @@ extension ImageView {
}

Configuration.imageCache.async.object(forKey: url.absoluteString) { [weak self] (object: Image?) in
guard let weakSelf = self else {
guard let `self` = self else {
return
}

if let image = object {
DispatchQueue.main.async {
weakSelf.image = image
self.image = image
completion?(image)
}

Expand All @@ -36,35 +32,34 @@ extension ImageView {

if placeholder == nil {
DispatchQueue.main.async {
Configuration.preConfigure?(weakSelf)
Configuration.preConfigure?(self)
}
}

DispatchQueue.main.async {
weakSelf.fetchFromNetwork(url: url, preprocess: preprocess, completion: completion)
self.fetchFromNetwork(url: url, preprocess: preprocess, completion: completion)
}
}
}

fileprivate func fetchFromNetwork(url: URL,
preprocess: @escaping Preprocess,
completion: Completion? = nil) {
fileprivate func fetchFromNetwork(url: URL, preprocess: @escaping Preprocess, completion: Completion? = nil) {
fetcher = Fetcher(url: url)

fetcher?.start(preprocess) { [weak self] result in
guard let weakSelf = self else { return }
guard let `self` = self else {
return
}

switch result {
case let .success(image, bytes):
Configuration.track?(url, nil, bytes)
Configuration.transitionClosure(weakSelf, image)
Configuration.transitionClosure(self, image)
Configuration.imageCache.async.addObject(image, forKey: url.absoluteString)
completion?(image)
case let .failure(error):
Configuration.track?(url, error, 0)
}

Configuration.postConfigure?(weakSelf)
Configuration.postConfigure?(self)
}
}

Expand Down
9 changes: 0 additions & 9 deletions Sources/Shared/ImageView.swift

This file was deleted.

1 change: 0 additions & 1 deletion Sources/Shared/NSHTTPURLResponse+Imaginary.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Foundation

extension HTTPURLResponse {

func validateLength(_ data: Data) -> Bool {
return expectedContentLength > -1
? (Int64(data.count) >= expectedContentLength)
Expand Down
17 changes: 17 additions & 0 deletions Sources/Shared/TypeAlias.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// This file contains a collection of type aliases to make
// the implementation more unified. Instead of referencing
// platform specific UI objects, we make type aliases to point
// to different implementations depending on the platform.

#if os(OSX)
import Cocoa
public typealias Image = NSImage
public typealias ImageView = NSImageView
#else
import UIKit
public typealias Image = UIImage
public typealias ImageView = UIImageView
#endif

public typealias Preprocess = (Image) -> Image
public typealias Completion = (Image?) -> Void
Loading