Browse Source

Clean code

onevcat 4 years ago
parent
commit
48d08b227e

+ 4 - 0
Kingfisher.xcodeproj/project.pbxproj

@@ -16,6 +16,7 @@
 		4B8351CC217084660081EED8 /* Runtime.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B8351CB217084660081EED8 /* Runtime.swift */; };
 		4B88CEB02646C056009EBB41 /* KFImageProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B88CEAF2646C056009EBB41 /* KFImageProtocol.swift */; };
 		4B88CEB22646C653009EBB41 /* KFImageRenderer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B88CEB12646C653009EBB41 /* KFImageRenderer.swift */; };
+		4B88CEB42646D0BF009EBB41 /* ImageContext.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B88CEB32646D0BF009EBB41 /* ImageContext.swift */; };
 		4B8E2917216F3F7F0095FAD1 /* ImageDownloaderDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B8E2916216F3F7F0095FAD1 /* ImageDownloaderDelegate.swift */; };
 		4B8E291C216F40AA0095FAD1 /* AuthenticationChallengeResponsable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B8E291B216F40AA0095FAD1 /* AuthenticationChallengeResponsable.swift */; };
 		4BA3BF1E228BCDD100909201 /* DataReceivingSideEffectTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4BA3BF1D228BCDD100909201 /* DataReceivingSideEffectTests.swift */; };
@@ -142,6 +143,7 @@
 		4B8351CB217084660081EED8 /* Runtime.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Runtime.swift; sourceTree = "<group>"; };
 		4B88CEAF2646C056009EBB41 /* KFImageProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KFImageProtocol.swift; sourceTree = "<group>"; };
 		4B88CEB12646C653009EBB41 /* KFImageRenderer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KFImageRenderer.swift; sourceTree = "<group>"; };
+		4B88CEB32646D0BF009EBB41 /* ImageContext.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageContext.swift; sourceTree = "<group>"; };
 		4B8E2916216F3F7F0095FAD1 /* ImageDownloaderDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageDownloaderDelegate.swift; sourceTree = "<group>"; };
 		4B8E291B216F40AA0095FAD1 /* AuthenticationChallengeResponsable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthenticationChallengeResponsable.swift; sourceTree = "<group>"; };
 		4BA3BF1D228BCDD100909201 /* DataReceivingSideEffectTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataReceivingSideEffectTests.swift; sourceTree = "<group>"; };
@@ -658,6 +660,7 @@
 			isa = PBXGroup;
 			children = (
 				D1F7607523097532000C5269 /* ImageBinder.swift */,
+				4B88CEB32646D0BF009EBB41 /* ImageContext.swift */,
 				D1F7607623097532000C5269 /* KFImage.swift */,
 				07292244263B02F00089E810 /* KFAnimatedImage.swift */,
 				4B88CEB12646C653009EBB41 /* KFImageRenderer.swift */,
@@ -793,6 +796,7 @@
 				D12AB6E4215D2BB50013BA68 /* Placeholder.swift in Sources */,
 				4B46CC6921744AC500D90C4A /* DiskStorage.swift in Sources */,
 				4B46CC5F217449C600D90C4A /* MemoryStorage.swift in Sources */,
+				4B88CEB42646D0BF009EBB41 /* ImageContext.swift in Sources */,
 				D16CC3D624E02E9500F1A515 /* AVAssetImageDataProvider.swift in Sources */,
 				D1839845216E333E003927D3 /* Delegate.swift in Sources */,
 				D12AB6D8215D2BB50013BA68 /* ImageTransition.swift in Sources */,

+ 50 - 0
Sources/SwiftUI/ImageContext.swift

@@ -0,0 +1,50 @@
+//
+//  ImageContext.swift
+//  Kingfisher
+//
+//  Created by JP20028 on 2021/05/08.
+//
+//  Copyright (c) 2021 Wei Wang <onevcat@gmail.com>
+//
+//  Permission is hereby granted, free of charge, to any person obtaining a copy
+//  of this software and associated documentation files (the "Software"), to deal
+//  in the Software without restriction, including without limitation the rights
+//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+//  copies of the Software, and to permit persons to whom the Software is
+//  furnished to do so, subject to the following conditions:
+//
+//  The above copyright notice and this permission notice shall be included in
+//  all copies or substantial portions of the Software.
+//
+//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+//  THE SOFTWARE.
+
+#if canImport(SwiftUI) && canImport(Combine)
+import SwiftUI
+
+@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
+extension KFImage {
+    public struct Context<HoldingView: KFImageHoldingView> {
+        var binder: ImageBinder
+        var configurations: [(HoldingView) -> HoldingView] = []
+        var cancelOnDisappear: Bool = false
+        var placeholder: AnyView? = nil
+
+        init(binder: ImageBinder) {
+            self.binder = binder
+        }
+    }
+}
+
+@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
+extension KFAnimatedImage {
+    public typealias Context = KFImage.Context
+    typealias ImageBinder = KFImage.ImageBinder
+}
+
+#endif

+ 0 - 60
Sources/SwiftUI/KFAnimatedImage.swift

@@ -29,72 +29,13 @@ import SwiftUI
 
 @available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
 public struct KFAnimatedImage: KFImageProtocol {
-    
-    public typealias Context = KFImage.Context
-    typealias ImageBinder = KFImage.ImageBinder
-    
     public typealias HoldingView = KFAnimatedImageViewRepresenter
-    
     public var context: Context<HoldingView>
-
     public init(context: KFImage.Context<HoldingView>) {
         self.context = context
     }
 }
 
-/// A Kingfisher compatible SwiftUI `View` to load an image from a `Source`.
-/// Declaring a `KFAnimatedImage` in a `View`'s body to trigger loading from the given `Source`.
-@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
-struct KFAnimatedImageRender: View {
-    /// An image binder that manages loading and cancelling image related task.
-    @ObservedObject var binder: KFAnimatedImage.ImageBinder
-
-    // Acts as a placeholder when loading an image.
-    var placeholder: AnyView?
-
-    // Whether the download task should be cancelled when the view disappears.
-    let cancelOnDisappear: Bool
-
-    init(_ context: KFAnimatedImage.Context<Image>) {
-        self.binder = context.binder
-        self.placeholder = context.placeholder
-        self.cancelOnDisappear = context.cancelOnDisappear
-    }
-    
-    /// Declares the content and behavior of this view.
-    @ViewBuilder
-    var body: some View {
-        if let image = binder.loadedImage {
-            KFAnimatedImageViewRepresenter(image: image)
-                .opacity(binder.loaded ? 1.0 : 0.0)
-        } else {
-            Group {
-                if placeholder != nil {
-                    placeholder
-                } else {
-                    Color.clear
-                }
-            }
-            .onAppear { [weak binder = self.binder] in
-                guard let binder = binder else {
-                    return
-                }
-                if !binder.loadingOrSucceeded {
-                    binder.start()
-                }
-            }
-            .onDisappear { [weak binder = self.binder] in
-                guard let binder = binder else {
-                    return
-                }
-                if self.cancelOnDisappear {
-                    binder.cancel()
-                }
-            }
-        }
-    }
-}
-
 /// A wrapped `UIViewRepresentable` of `AnimatedImageView`
 @available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
 public struct KFAnimatedImageViewRepresenter: UIViewRepresentable, KFImageHoldingView {
@@ -130,5 +71,4 @@ struct KFAnimatedImage_Previews : PreviewProvider {
     }
 }
 #endif
-
 #endif

+ 0 - 17
Sources/SwiftUI/KFImage.swift

@@ -30,30 +30,13 @@ import SwiftUI
 
 @available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
 public struct KFImage: KFImageProtocol {
-    
     public typealias HoldingView = Image
-
     public var context: Context<HoldingView>
-    
     public init(context: Context<Image>) {
         self.context = context
     }
 }
 
-@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
-extension KFImage {
-    public struct Context<HoldingView: KFImageHoldingView> {
-        var binder: ImageBinder
-        var configurations: [(HoldingView) -> HoldingView] = []
-        var cancelOnDisappear: Bool = false
-        var placeholder: AnyView? = nil
-
-        init(binder: ImageBinder) {
-            self.binder = binder
-        }
-    }
-}
-
 // MARK: - Image compatibility.
 @available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
 extension KFImage {

+ 1 - 1
Sources/SwiftUI/KFImageOptions.swift

@@ -24,7 +24,7 @@
 //  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 //  THE SOFTWARE.
 
-#if canImport(SwiftUI) && canImport(Combine)
+#if canImport(SwiftUI)
 import SwiftUI
 
 // MARK: - KFImage creating.

+ 7 - 3
Sources/SwiftUI/KFImageProtocol.swift

@@ -24,14 +24,17 @@
 //  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 //  THE SOFTWARE.
 
+#if canImport(SwiftUI)
 import SwiftUI
 
 @available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
 public protocol KFImageProtocol: View {
-    associatedtype HoldingView: KFImageHoldingView
-    var context: KFImage.Context<HoldingView> { get set }
     
-    init(context: KFImage.Context<HoldingView>)
+    typealias Context = KFImage.Context<HoldingView>
+    
+    associatedtype HoldingView: KFImageHoldingView
+    var context: Context { get set }
+    init(context: Context)
 }
 
 @available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
@@ -88,3 +91,4 @@ public protocol KFImageHoldingView: View {
     static func created(from image: KFCrossPlatformImage) -> Self
 }
 
+#endif

+ 2 - 0
Sources/SwiftUI/KFImageRenderer.swift

@@ -24,6 +24,7 @@
 //  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 //  THE SOFTWARE.
 
+#if canImport(SwiftUI)
 import SwiftUI
 
 /// A Kingfisher compatible SwiftUI `View` to load an image from a `Source`.
@@ -144,3 +145,4 @@ extension UIImage.Orientation {
     }
 }
 #endif
+#endif