Sfoglia il codice sorgente

Merge pull request #1617 from onevcat/fix/optional-url

Optional KF init methods
Wei Wang 5 anni fa
parent
commit
24d2a0518c

+ 3 - 0
Sources/General/ImageSource/ImageDataProvider.swift

@@ -55,6 +55,9 @@ public protocol ImageDataProvider {
 
 public extension ImageDataProvider {
     var contentURL: URL? { return nil }
+    func convertToSource() -> Source {
+        .provider(self)
+    }
 }
 
 /// Represents an image data provider for loading from a local file URL on disk.

+ 3 - 3
Sources/General/ImageSource/Resource.swift

@@ -43,10 +43,10 @@ extension Resource {
     /// Converts `self` to a valid `Source` based on its `downloadURL` scheme. A `.provider` with
     /// `LocalFileImageDataProvider` associated will be returned if the URL points to a local file. Otherwise,
     /// `.network` is returned.
-    public func convertToSource() -> Source {
+    public func convertToSource(overrideCacheKey: String? = nil) -> Source {
         return downloadURL.isFileURL ?
-            .provider(LocalFileImageDataProvider(fileURL: downloadURL, cacheKey: cacheKey)) :
-            .network(self)
+            .provider(LocalFileImageDataProvider(fileURL: downloadURL, cacheKey: overrideCacheKey ?? cacheKey)) :
+            .network(ImageResource(downloadURL: downloadURL, cacheKey: overrideCacheKey ?? cacheKey))
     }
 }
 

+ 15 - 11
Sources/General/KF.swift

@@ -48,7 +48,7 @@ public enum KF {
     /// - Parameter source: The `Source` object defines data information from network or a data provider.
     /// - Returns: A `KF.Builder` for future configuration. After configuring the builder, call `set(to:)`
     ///            to start the image loading.
-    public static func source(_ source: Source) -> KF.Builder {
+    public static func source(_ source: Source?) -> KF.Builder {
         Builder(source: source)
     }
 
@@ -56,8 +56,8 @@ public enum KF {
     /// - Parameter resource: The `Resource` object defines data information like key or URL.
     /// - Returns: A `KF.Builder` for future configuration. After configuring the builder, call `set(to:)`
     ///            to start the image loading.
-    public static func resource(_ resource: Resource) -> KF.Builder {
-        Builder(source: .network(resource))
+    public static func resource(_ resource: Resource?) -> KF.Builder {
+        source(resource?.convertToSource())
     }
 
     /// Creates a builder for a given `URL` and an optional cache key.
@@ -67,16 +67,16 @@ public enum KF {
     ///               If `nil`, the `absoluteString` of `url` is used as the cache key.
     /// - Returns: A `KF.Builder` for future configuration. After configuring the builder, call `set(to:)`
     ///            to start the image loading.
-    public static func url(_ url: URL, cacheKey: String? = nil) -> KF.Builder {
-        Builder(source: .network(ImageResource(downloadURL: url, cacheKey: cacheKey)))
+    public static func url(_ url: URL?, cacheKey: String? = nil) -> KF.Builder {
+        source(url?.convertToSource(overrideCacheKey: cacheKey))
     }
 
     /// Creates a builder for a given `ImageDataProvider`.
     /// - Parameter provider: The `ImageDataProvider` object contains information about the data.
     /// - Returns: A `KF.Builder` for future configuration. After configuring the builder, call `set(to:)`
     ///            to start the image loading.
-    public static func dataProvider(_ provider: ImageDataProvider) -> KF.Builder {
-        Builder(source: .provider(provider))
+    public static func dataProvider(_ provider: ImageDataProvider?) -> KF.Builder {
+        source(provider?.convertToSource())
     }
 
     /// Creates a builder for some given raw data and a cache key.
@@ -85,8 +85,12 @@ public enum KF {
     ///   - cacheKey: The key used to store the downloaded image in cache.
     /// - Returns: A `KF.Builder` for future configuration. After configuring the builder, call `set(to:)`
     ///            to start the image loading.
-    public static func data(_ data: Data, cacheKey: String) -> KF.Builder {
-        Builder(source: .provider(RawImageDataProvider(data: data, cacheKey: cacheKey)))
+    public static func data(_ data: Data?, cacheKey: String) -> KF.Builder {
+        if let data = data {
+            return dataProvider(RawImageDataProvider(data: data, cacheKey: cacheKey))
+        } else {
+            return dataProvider(nil)
+        }
     }
 }
 
@@ -95,7 +99,7 @@ extension KF {
 
     /// A builder class to configure an image retrieving task and set it to a holder view or component.
     public class Builder {
-        private let source: Source
+        private let source: Source?
 
         #if os(watchOS)
         private var placeholder: KFCrossPlatformImage?
@@ -109,7 +113,7 @@ extension KF {
         public let onSuccessDelegate = Delegate<RetrieveImageResult, Void>()
         public let onProgressDelegate = Delegate<(Int64, Int64), Void>()
 
-        init(source: Source) {
+        init(source: Source?) {
             self.source = source
         }
 

+ 10 - 6
Sources/SwiftUI/KFImageOptions.swift

@@ -56,7 +56,7 @@ extension KFImage {
         _ resource: Resource?, isLoaded: Binding<Bool> = .constant(false)
     ) -> KFImage
     {
-        .source(resource?.convertToSource(), isLoaded: isLoaded)
+        source(resource?.convertToSource(), isLoaded: isLoaded)
     }
 
     /// Creates a `KFImage` for a given `URL`.
@@ -72,7 +72,7 @@ extension KFImage {
         _ url: URL?, cacheKey: String? = nil, isLoaded: Binding<Bool> = .constant(false)
     ) -> KFImage
     {
-        source(url?.convertToSource(), isLoaded: isLoaded)
+        source(url?.convertToSource(overrideCacheKey: cacheKey), isLoaded: isLoaded)
     }
 
     /// Creates a `KFImage` for a given `ImageDataProvider`.
@@ -83,10 +83,10 @@ extension KFImage {
     ///               wrapped value from outside.
     /// - Returns: A `KFImage` for future configuration or embedding to a `SwiftUI.View`.
     public static func dataProvider(
-        _ provider: ImageDataProvider, isLoaded: Binding<Bool> = .constant(false)
+        _ provider: ImageDataProvider?, isLoaded: Binding<Bool> = .constant(false)
     ) -> KFImage
     {
-        source(.provider(provider), isLoaded: isLoaded)
+        source(provider?.convertToSource(), isLoaded: isLoaded)
     }
 
     /// Creates a builder for some given raw data and a cache key.
@@ -98,10 +98,14 @@ extension KFImage {
     ///               wrapped value from outside.
     /// - Returns: A `KFImage` for future configuration or embedding to a `SwiftUI.View`.
     public static func data(
-        _ data: Data, cacheKey: String, isLoaded: Binding<Bool> = .constant(false)
+        _ data: Data?, cacheKey: String, isLoaded: Binding<Bool> = .constant(false)
     ) -> KFImage
     {
-        source(.provider(RawImageDataProvider(data: data, cacheKey: cacheKey)), isLoaded: isLoaded)
+        if let data = data {
+            return dataProvider(RawImageDataProvider(data: data, cacheKey: cacheKey), isLoaded: isLoaded)
+        } else {
+            return dataProvider(nil, isLoaded: isLoaded)
+        }
     }
 }