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

Feature source image prefetcher #1142

Merged
merged 3 commits into from
Mar 24, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 16 additions & 0 deletions Sources/General/ImageSource/Source.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,19 @@ public enum Source {
}
}
}

extension Source {
var asResource: Resource? {
guard case .network(let resource) = self else {
return nil
}
return resource
}

var asProvider: ImageDataProvider? {
guard case .provider(let provider) = self else {
return nil
}
return provider
}
}
22 changes: 16 additions & 6 deletions Sources/General/KingfisherManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,10 @@ public class KingfisherManager {
{
if options.forceRefresh {
return loadAndCacheImage(
source: source, options: options, progressBlock: progressBlock, completionHandler: completionHandler)
source: source,
options: options,
progressBlock: progressBlock,
completionHandler: completionHandler)?.value
} else {
let loadedFromCache = retrieveImageFromCache(
source: source,
Expand All @@ -184,7 +187,10 @@ public class KingfisherManager {
}

return loadAndCacheImage(
source: source, options: options, progressBlock: progressBlock, completionHandler: completionHandler)
source: source,
options: options,
progressBlock: progressBlock,
completionHandler: completionHandler)?.value
}
}

Expand Down Expand Up @@ -230,7 +236,7 @@ public class KingfisherManager {
source: Source,
options: KingfisherParsedOptionsInfo,
progressBlock: DownloadProgressBlock? = nil,
completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)?) -> DownloadTask?
completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)?) -> DownloadTask.WrappedTask?
{
func cacheImage(_ result: Result<ImageLoadingResult, KingfisherError>)
{
Expand Down Expand Up @@ -276,14 +282,18 @@ public class KingfisherManager {
switch source {
case .network(let resource):
let downloader = options.downloader ?? self.downloader
return downloader.downloadImage(
guard let task = downloader.downloadImage(
with: resource.downloadURL,
options: options,
progressBlock: progressBlock,
completionHandler: cacheImage)
completionHandler: cacheImage) else
{
return nil
}
return .download(task)
case .provider(let provider):
provideImage(provider: provider, options: options, completionHandler: cacheImage)
return nil
return .dataProviding
}
}

Expand Down
21 changes: 21 additions & 0 deletions Sources/Networking/ImageDownloader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,27 @@ public struct DownloadTask {
}
}

extension DownloadTask {
enum WrappedTask {
case download(DownloadTask)
case dataProviding

func cancel() {
switch self {
case .download(let task): task.cancel()
case .dataProviding: break
}
}

var value: DownloadTask? {
switch self {
case .download(let task): return task
case .dataProviding: return nil
}
}
}
}

/// Represents a downloading manager for requesting the image with a URL from server.
open class ImageDownloader {

Expand Down
Loading