Skip to content

Commit

Permalink
Make result type compatible to both Swift 4 and 5
Browse files Browse the repository at this point in the history
  • Loading branch information
onevcat committed Feb 25, 2019
1 parent 959240b commit f529036
Show file tree
Hide file tree
Showing 10 changed files with 287 additions and 200 deletions.
10 changes: 8 additions & 2 deletions Sources/Extensions/ImageView+Kingfisher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,14 @@ extension KingfisherWrapper where Base: ImageView {
CallbackQueue.mainCurrentOrAsync.execute {
maybeIndicator?.stopAnimatingView()
guard issuedIdentifier == self.taskIdentifier else {
let error = KingfisherError.imageSettingError(
reason: .notCurrentSourceTask(result: result.value, error: result.error, source: source))
let reason: KingfisherError.ImageSettingErrorReason
do {
let value = try result.get()
reason = .notCurrentSourceTask(result: value, error: nil, source: source)
} catch {
reason = .notCurrentSourceTask(result: nil, error: error, source: source)
}
let error = KingfisherError.imageSettingError(reason: reason)
completionHandler?(.failure(error))
return
}
Expand Down
20 changes: 16 additions & 4 deletions Sources/Extensions/UIButton+Kingfisher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,14 @@ extension KingfisherWrapper where Base: UIButton {
completionHandler: { result in
CallbackQueue.mainCurrentOrAsync.execute {
guard issuedTaskIdentifier == self.taskIdentifier(for: state) else {
let error = KingfisherError.imageSettingError(
reason: .notCurrentSourceTask(result: result.value, error: result.error, source: source))
let reason: KingfisherError.ImageSettingErrorReason
do {
let value = try result.get()
reason = .notCurrentSourceTask(result: value, error: nil, source: source)
} catch {
reason = .notCurrentSourceTask(result: nil, error: error, source: source)
}
let error = KingfisherError.imageSettingError(reason: reason)
completionHandler?(.failure(error))
return
}
Expand Down Expand Up @@ -205,8 +211,14 @@ extension KingfisherWrapper where Base: UIButton {
completionHandler: { result in
CallbackQueue.mainCurrentOrAsync.execute {
guard issuedTaskIdentifier == self.backgroundTaskIdentifier(for: state) else {
let error = KingfisherError.imageSettingError(
reason: .notCurrentSourceTask(result: result.value, error: result.error, source: source))
let reason: KingfisherError.ImageSettingErrorReason
do {
let value = try result.get()
reason = .notCurrentSourceTask(result: value, error: nil, source: source)
} catch {
reason = .notCurrentSourceTask(result: nil, error: error, source: source)
}
let error = KingfisherError.imageSettingError(reason: reason)
completionHandler?(.failure(error))
return
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/General/ImageSource/ImageDataProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public struct LocalFileImageDataProvider: ImageDataProvider {
public var cacheKey: String

public func data(handler: (Result<Data, Error>) -> Void) {
handler( Result { try Data(contentsOf: fileURL) } )
handler(Result(catching: { try Data(contentsOf: fileURL) }))
}
}

Expand Down
88 changes: 52 additions & 36 deletions Sources/General/KingfisherManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -323,14 +323,22 @@ public class KingfisherManager {
targetCache.retrieveImage(forKey: key, options: options) { result in
guard let completionHandler = completionHandler else { return }
options.callbackQueue.execute {
if let image = result.value?.image {
let value = result.map {
RetrieveImageResult(image: image, cacheType: $0.cacheType, source: source)
result.match(
onSuccess: { cacheResult in
let value: Result<RetrieveImageResult, KingfisherError>
if let image = cacheResult.image {
value = result.map {
RetrieveImageResult(image: image, cacheType: $0.cacheType, source: source)
}
} else {
value = .failure(KingfisherError.cacheError(reason: .imageNotExisting(key: key)))
}
completionHandler(value)
},
onFailure: { _ in
completionHandler(.failure(KingfisherError.cacheError(reason: .imageNotExisting(key: key))))
}
completionHandler(value)
} else {
completionHandler(.failure(KingfisherError.cacheError(reason: .imageNotExisting(key: key))))
}
)
}
}
return true
Expand All @@ -352,44 +360,52 @@ public class KingfisherManager {
var optionsWithoutProcessor = options
optionsWithoutProcessor.processor = DefaultImageProcessor.default
originalCache.retrieveImage(forKey: key, options: optionsWithoutProcessor) { result in
if let image = result.value?.image {
let processor = options.processor
(options.processingQueue ?? self.processingQueue).execute {
let item = ImageProcessItem.image(image)
guard let processedImage = processor.process(item: item, options: options) else {
let error = KingfisherError.processorError(
reason: .processingFailed(processor: processor, item: item))
options.callbackQueue.execute { completionHandler?(.failure(error)) }

result.match(
onSuccess: { cacheResult in
guard let image = cacheResult.image else {
return
}

var cacheOptions = options
cacheOptions.callbackQueue = .untouch
targetCache.store(
processedImage,
forKey: key,
options: cacheOptions,
toDisk: !options.cacheMemoryOnly)
{
_ in
if options.waitForCache {
let processor = options.processor
(options.processingQueue ?? self.processingQueue).execute {
let item = ImageProcessItem.image(image)
guard let processedImage = processor.process(item: item, options: options) else {
let error = KingfisherError.processorError(
reason: .processingFailed(processor: processor, item: item))
options.callbackQueue.execute { completionHandler?(.failure(error)) }
return
}

var cacheOptions = options
cacheOptions.callbackQueue = .untouch
targetCache.store(
processedImage,
forKey: key,
options: cacheOptions,
toDisk: !options.cacheMemoryOnly)
{
_ in
if options.waitForCache {
let value = RetrieveImageResult(image: processedImage, cacheType: .none, source: source)
options.callbackQueue.execute { completionHandler?(.success(value)) }
}
}

if !options.waitForCache {
let value = RetrieveImageResult(image: processedImage, cacheType: .none, source: source)
options.callbackQueue.execute { completionHandler?(.success(value)) }
}
}

if !options.waitForCache {
let value = RetrieveImageResult(image: processedImage, cacheType: .none, source: source)
options.callbackQueue.execute { completionHandler?(.success(value)) }
},
onFailure: { _ in
// This should not happen actually, since we already confirmed `originalImageCached` is `true`.
// Just in case...
options.callbackQueue.execute {
completionHandler?(.failure(KingfisherError.cacheError(reason: .imageNotExisting(key: key))))
}
}
} else {
// This should not happen actually, since we already confirmed `originalImageCached` is `true`.
// Just in case...
options.callbackQueue.execute {
completionHandler?(.failure(KingfisherError.cacheError(reason: .imageNotExisting(key: key))))
}
}
)
}
return true
}
Expand Down
29 changes: 21 additions & 8 deletions Sources/Networking/ImageDownloader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,12 @@ open class ImageDownloader {
}
sessionDelegate.onDownloadingFinished.delegate(on: self) { (self, value) in
let (url, result) = value
self.delegate?.imageDownloader(
self, didFinishDownloadingImageForURL: url, with: result.value, error: result.error)
do {
let value = try result.get()
self.delegate?.imageDownloader(self, didFinishDownloadingImageForURL: url, with: value, error: nil)
} catch {
self.delegate?.imageDownloader(self, didFinishDownloadingImageForURL: url, with: nil, error: error)
}
}
sessionDelegate.onDidDownloadData.delegate(on: self) { (self, task) in
guard let url = task.task.originalRequest?.url else {
Expand Down Expand Up @@ -244,11 +248,20 @@ open class ImageDownloader {
let (result, callbacks) = done

// Before processing the downloaded data.
self.delegate?.imageDownloader(
self,
didFinishDownloadingImageForURL: url,
with: result.value?.1,
error: result.error)
do {
let value = try result.get()
self.delegate?.imageDownloader(
self,
didFinishDownloadingImageForURL: url,
with: value.1,
error: nil)
} catch {
self.delegate?.imageDownloader(
self,
didFinishDownloadingImageForURL: url,
with: nil,
error: error)
}

switch result {
// Download finished. Now process the data to an image.
Expand All @@ -261,7 +274,7 @@ open class ImageDownloader {
// result: Result<Image>, callback: SessionDataTask.TaskCallback
let (result, callback) = result

if let image = result.value {
if let image = try? result.get() {
self.delegate?.imageDownloader(self, didDownload: image, for: url, with: response)
}

Expand Down
7 changes: 4 additions & 3 deletions Sources/Networking/ImagePrefetcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,11 @@ public class ImagePrefetcher {

let downloadTaskCompletionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void) = { result in
self.tasks.removeValue(forKey: resource.downloadURL)
if let _ = result.error {
self.failedResources.append(resource)
} else {
do {
let _ = try result.get()
self.completedResources.append(resource)
} catch {
self.failedResources.append(resource)
}

self.reportProgress()
Expand Down
Loading

0 comments on commit f529036

Please sign in to comment.