Skip to content
This repository has been archived by the owner on May 28, 2024. It is now read-only.

Commit

Permalink
Drop Swift 4.0 - Swift 4.1 support
Browse files Browse the repository at this point in the history
  • Loading branch information
kean committed Apr 6, 2019
1 parent 80eb40b commit 42cc946
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 110 deletions.
20 changes: 0 additions & 20 deletions Sources/DataCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ public final class DataCache: DataCaching {

private let _filenameGenerator: FilenameGenerator

#if swift(>=4.2)
/// Creates a cache instance with a given `name`. The cache creates a directory
/// with the given `name` in a `.cachesDirectory` in `.userDomainMask`.
/// - parameter filenameGenerator: Generates a filename for the given URL.
Expand All @@ -124,25 +123,6 @@ public final class DataCache: DataCaching {
public static func filename(for key: String) -> String? {
return key.sha1
}
#else
/// Creates a cache instance with a given `name`. The cache creates a directory
/// with the given `name` in a `.cachesDirectory` in `.userDomainMask`.
/// - parameter filenameGenerator: Generates a filename for the given URL.
public convenience init(name: String, filenameGenerator: @escaping (String) -> String?) throws {
guard let root = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first else {
throw NSError(domain: NSCocoaErrorDomain, code: NSFileNoSuchFileError, userInfo: nil)
}
try self.init(path: root.appendingPathComponent(name, isDirectory: true), filenameGenerator: filenameGenerator)
}

/// Creates a cache instance with a given path.
/// - parameter filenameGenerator: Generates a filename for the given URL.
public init(path: URL, filenameGenerator: @escaping (String) -> String?) throws {
self.path = path
self._filenameGenerator = filenameGenerator
try self._didInit()
}
#endif

private func _didInit() throws {
try FileManager.default.createDirectory(at: path, withIntermediateDirectories: true, attributes: nil)
Expand Down
5 changes: 0 additions & 5 deletions Sources/ImageCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,8 @@ internal final class _Cache<Key: Hashable, Value> {
self.costLimit = costLimit
self.countLimit = countLimit
#if os(iOS) || os(tvOS)
#if swift(>=4.2)
NotificationCenter.default.addObserver(self, selector: #selector(removeAll), name: UIApplication.didReceiveMemoryWarningNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(didEnterBackground), name: UIApplication.didEnterBackgroundNotification, object: nil)
#else
NotificationCenter.default.addObserver(self, selector: #selector(removeAll), name: .UIApplicationDidReceiveMemoryWarning, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(didEnterBackground), name: .UIApplicationDidEnterBackground, object: nil)
#endif
#endif
}

Expand Down
11 changes: 0 additions & 11 deletions Sources/ImageDecoding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -203,17 +203,6 @@ enum ImageFormat: Equatable {
data[index] != number
}
}

#if !swift(>=4.1)
static func == (lhs: ImageFormat, rhs: ImageFormat) -> Bool {
switch (lhs, rhs) {
case let (.jpeg(lhs), .jpeg(rhs)): return lhs == rhs
case (.png, .png): return true
case (.gif, .gif): return true
default: return false
}
}
#endif
}

// MARK: - Animated Images
Expand Down
10 changes: 0 additions & 10 deletions Sources/ImagePreheater.swift
Original file line number Diff line number Diff line change
Expand Up @@ -168,15 +168,5 @@ public final class ImagePreheater {
self.cacheKey = ImageRequest.CacheKey(request: request)
self.loadKey = ImageRequest.LoadKey(request: request)
}

#if !swift(>=4.1)
var hashValue: Int {
return cacheKey.hashValue
}

static func == (lhs: PreheatKey, rhs: PreheatKey) -> Bool {
return lhs.cacheKey == rhs.cacheKey && lhs.loadKey == rhs.loadKey
}
#endif
}
}
75 changes: 37 additions & 38 deletions Sources/ImageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -362,26 +362,7 @@ private final class ImageViewController {
_display(image, options.transition, options.alwaysTransition, false, options.contentModes?.success)
}

#else

private func handle(response: ImageResponse?, error: Error?, fromMemCache: Bool, options: ImageLoadingOptions) {
// NSImageView doesn't support content mode, unfortunately.
if let image = response?.image {
_display(image, options.transition, options.alwaysTransition, fromMemCache, nil)
} else if let failureImage = options.failureImage {
_display(failureImage, options.failureImageTransition, options.alwaysTransition, fromMemCache, nil)
}
self.task = nil
}

private func handle(partialImage response: ImageResponse?, options: ImageLoadingOptions) {
guard let image = response?.image else { return }
_display(image, options.transition, options.alwaysTransition, false, nil)
}

#endif

private func _display(_ image: Image, _ transition: ImageLoadingOptions.Transition?, _ alwaysTransition: Bool, _ fromMemCache: Bool, _ newContentMode: _ContentMode?) {
private func _display(_ image: Image, _ transition: ImageLoadingOptions.Transition?, _ alwaysTransition: Bool, _ fromMemCache: Bool, _ newContentMode: UIView.ContentMode?) {
guard let imageView = imageView else { return }

if !fromMemCache || alwaysTransition, let transition = transition {
Expand All @@ -396,28 +377,16 @@ private final class ImageViewController {
} else {
imageView.display(image: image)
}
#if !os(macOS)
if let newContentMode = newContentMode {
imageView.contentMode = newContentMode
}
#endif
}

// MARK: - Animations

#if !os(macOS)

// Image view used for cross-fade transition between images with different
// content modes.
private lazy var transitionImageView = UIImageView()

#if swift(>=4.2)
private typealias _ContentMode = UIView.ContentMode
#else
private typealias _ContentMode = UIViewContentMode
#endif

private func _runFadeInTransition(image: Image, params: ImageLoadingOptions.Transition.Parameters, contentMode: _ContentMode?) {
private func _runFadeInTransition(image: Image, params: ImageLoadingOptions.Transition.Parameters, contentMode: UIView.ContentMode?) {
guard let imageView = imageView else { return }

// Special case where we animate between content modes, only works
Expand All @@ -438,7 +407,7 @@ private final class ImageViewController {
options: params.options.union(.transitionCrossDissolve),
animations: {
imageView.display(image: image)
},
},
completion: nil
)
}
Expand Down Expand Up @@ -468,20 +437,50 @@ private final class ImageViewController {
animations: {
transitionView.alpha = 0
imageView.alpha = 1
},
},
completion: { isCompleted in
if isCompleted {
transitionView.removeFromSuperview()
}
}
}
)
}

#else

private typealias _ContentMode = Void // There is no content mode on macOS
private func handle(response: ImageResponse?, error: Error?, fromMemCache: Bool, options: ImageLoadingOptions) {
// NSImageView doesn't support content mode, unfortunately.
if let image = response?.image {
_display(image, options.transition, options.alwaysTransition, fromMemCache)
} else if let failureImage = options.failureImage {
_display(failureImage, options.failureImageTransition, options.alwaysTransition, fromMemCache)
}
self.task = nil
}

private func handle(partialImage response: ImageResponse?, options: ImageLoadingOptions) {
guard let image = response?.image else { return }
_display(image, options.transition, options.alwaysTransition, false)
}

private func _display(_ image: Image, _ transition: ImageLoadingOptions.Transition?, _ alwaysTransition: Bool, _ fromMemCache: Bool) {
guard let imageView = imageView else { return }

if !fromMemCache || alwaysTransition, let transition = transition {
switch transition.style {
case let .fadeIn(params):
_runFadeInTransition(image: image, params: params)
case let .custom(closure):
// The user is reponsible for both displaying an image and performing
// animations.
closure(imageView, image)
}
} else {
imageView.display(image: image)
}
}

private func _runFadeInTransition(image: Image, params: ImageLoadingOptions.Transition.Parameters, contentMode: _ContentMode?) {
private func _runFadeInTransition(image: Image, params: ImageLoadingOptions.Transition.Parameters) {
let animation = CABasicAnimation(keyPath: "opacity")
animation.duration = params.duration
animation.fromValue = 0
Expand Down
10 changes: 0 additions & 10 deletions Sources/Internal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -539,15 +539,6 @@ final class Property<T> {

// MARK: - Misc

#if !swift(>=4.1)
extension Sequence {
public func compactMap<ElementOfResult>(_ transform: (Element) throws -> ElementOfResult?) rethrows -> [ElementOfResult] {
return try flatMap(transform)
}
}
#endif

#if swift(>=4.2)
import CommonCrypto

extension String {
Expand Down Expand Up @@ -576,4 +567,3 @@ extension String {
return hash.map({ String(format: "%02x", $0) }).joined()
}
}
#endif
2 changes: 0 additions & 2 deletions Tests/DataCacheTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ class DataCacheTests: XCTestCase {

// MARK: Default Key Encoder

#if swift(>=4.2)
func testDefaultKeyEncoder() {
let cache = try! DataCache(name: UUID().uuidString)
let filename = cache.filename(for: "http://test.com")
Expand All @@ -64,7 +63,6 @@ class DataCacheTests: XCTestCase {
func testSHA1() {
XCTAssertEqual("http://test.com".sha1, "50334ee0b51600df6397ce93ceed4728c37fee4e")
}
#endif

// MARK: Add

Expand Down
12 changes: 0 additions & 12 deletions Tests/ImageCacheTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,7 @@ class ImageCacheTests: XCTestCase {
cache[Test.request] = Image()

// When
#if swift(>=4.2)
NotificationCenter.default.post(name: UIApplication.didReceiveMemoryWarningNotification, object: nil)
#else
NotificationCenter.default.post(name: NSNotification.Name.UIApplicationDidReceiveMemoryWarning, object: nil)
#endif

// Then
XCTAssertNil(cache[Test.request])
Expand All @@ -290,11 +286,7 @@ class ImageCacheTests: XCTestCase {
XCTAssertEqual(cache.totalCount, 10)

// When
#if swift(>=4.2)
NotificationCenter.default.post(name: UIApplication.didEnterBackgroundNotification, object: nil)
#else
NotificationCenter.default.post(name: NSNotification.Name.UIApplicationDidEnterBackground, object: nil)
#endif

// Then
XCTAssertEqual(cache.totalCount, 1)
Expand All @@ -313,11 +305,7 @@ class ImageCacheTests: XCTestCase {
XCTAssertEqual(cache.totalCount, 10)

// When
#if swift(>=4.2)
NotificationCenter.default.post(name: UIApplication.didEnterBackgroundNotification, object: nil)
#else
NotificationCenter.default.post(name: NSNotification.Name.UIApplicationDidEnterBackground, object: nil)
#endif

// Then
XCTAssertEqual(cache.totalCount, 1)
Expand Down
2 changes: 1 addition & 1 deletion Tests/ImagePipelineTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ class ImagePipelineErrorHandlingTests: XCTestCase {
// Given
let pipeline = ImagePipeline {
$0.dataLoader = MockDataLoader()
return // !swift(>=4.1)
return
}

let request = Test.request.processed(with: MockFailingProcessor())
Expand Down
2 changes: 1 addition & 1 deletion Tests/ImageProcessingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ImageProcessingTests: XCTestCase {
mockDataLoader = MockDataLoader()
pipeline = ImagePipeline {
$0.dataLoader = mockDataLoader
return // !swift(>=4.1)
return
}
}

Expand Down

0 comments on commit 42cc946

Please sign in to comment.