Browse Source

Add all Image extension to KFImage

onevcat 6 years ago
parent
commit
9f3b348900
3 changed files with 109 additions and 37 deletions
  1. 10 0
      Kingfisher.xcodeproj/project.pbxproj
  2. 59 0
      Sources/SwiftUI/ImageBinder.swift
  3. 40 37
      Sources/SwiftUI/KFImage.swift

+ 10 - 0
Kingfisher.xcodeproj/project.pbxproj

@@ -65,6 +65,10 @@
 		C9286408228584EB00257182 /* ImageProgressive.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9286406228584EB00257182 /* ImageProgressive.swift */; };
 		C9286409228584EB00257182 /* ImageProgressive.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9286406228584EB00257182 /* ImageProgressive.swift */; };
 		C928640A228584EB00257182 /* ImageProgressive.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9286406228584EB00257182 /* ImageProgressive.swift */; };
+		D10CEBD622C4E6AC0065C727 /* ImageBinder.swift in Sources */ = {isa = PBXBuildFile; fileRef = D10CEBD522C4E6AC0065C727 /* ImageBinder.swift */; };
+		D10CEBD722C4E6AC0065C727 /* ImageBinder.swift in Sources */ = {isa = PBXBuildFile; fileRef = D10CEBD522C4E6AC0065C727 /* ImageBinder.swift */; };
+		D10CEBD822C4E6AC0065C727 /* ImageBinder.swift in Sources */ = {isa = PBXBuildFile; fileRef = D10CEBD522C4E6AC0065C727 /* ImageBinder.swift */; };
+		D10CEBD922C4E6AC0065C727 /* ImageBinder.swift in Sources */ = {isa = PBXBuildFile; fileRef = D10CEBD522C4E6AC0065C727 /* ImageBinder.swift */; };
 		D10EC2361C3D632300A4211C /* Kingfisher.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4B2944481C3D01B20088C3E7 /* Kingfisher.framework */; };
 		D114F36E215D2D0B00A01349 /* String+MD5.swift in Sources */ = {isa = PBXBuildFile; fileRef = D12AB6BC215D2BB50013BA68 /* String+MD5.swift */; };
 		D12AB6C0215D2BB50013BA68 /* RequestModifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = D12AB69D215D2BB50013BA68 /* RequestModifier.swift */; };
@@ -312,6 +316,7 @@
 		C9286406228584EB00257182 /* ImageProgressive.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageProgressive.swift; sourceTree = "<group>"; };
 		C959EEE7228940FE00467A10 /* QuartzCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuartzCore.framework; path = System/Library/Frameworks/QuartzCore.framework; sourceTree = SDKROOT; };
 		CCDD057F8DA8D24EE701CF98 /* libPods-KingfisherTests.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-KingfisherTests.a"; sourceTree = BUILT_PRODUCTS_DIR; };
+		D10CEBD522C4E6AC0065C727 /* ImageBinder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageBinder.swift; sourceTree = "<group>"; };
 		D10EC2311C3D632300A4211C /* KingfisherTests-macOS.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "KingfisherTests-macOS.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
 		D12AB69D215D2BB50013BA68 /* RequestModifier.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RequestModifier.swift; sourceTree = "<group>"; };
 		D12AB69E215D2BB50013BA68 /* Resource.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Resource.swift; sourceTree = "<group>"; };
@@ -443,6 +448,7 @@
 			isa = PBXGroup;
 			children = (
 				4B531FE322C35B7B0041899C /* KFImage.swift */,
+				D10CEBD522C4E6AC0065C727 /* ImageBinder.swift */,
 			);
 			path = SwiftUI;
 			sourceTree = "<group>";
@@ -1112,6 +1118,7 @@
 				4B8E291E216F40AA0095FAD1 /* AuthenticationChallengeResponsable.swift in Sources */,
 				D1BA781F2174D07800C69D7B /* CallbackQueue.swift in Sources */,
 				D1A37BD2215D2DBA009B39B7 /* Filter.swift in Sources */,
+				D10CEBD822C4E6AC0065C727 /* ImageBinder.swift in Sources */,
 				D1E56447219B16330057AAE3 /* ImageDataProvider.swift in Sources */,
 				D1A37BD3215D2DBA009B39B7 /* Placeholder.swift in Sources */,
 				4BD821692189FD330084CC21 /* SessionDataTask.swift in Sources */,
@@ -1224,6 +1231,7 @@
 				D12AB6E9215D2BB50013BA68 /* GIFAnimatedImage.swift in Sources */,
 				D13646752165A1A100A33652 /* Result.swift in Sources */,
 				D1A1CC9B219FAB4B00263AD8 /* Source.swift in Sources */,
+				D10CEBD722C4E6AC0065C727 /* ImageBinder.swift in Sources */,
 				4BD821632189FC0C0084CC21 /* SessionDelegate.swift in Sources */,
 				D12AB6E1215D2BB50013BA68 /* Filter.swift in Sources */,
 				D12AB6C5215D2BB50013BA68 /* Resource.swift in Sources */,
@@ -1247,6 +1255,7 @@
 				D12AB6CF215D2BB50013BA68 /* ImageModifier.swift in Sources */,
 				D1A37BF5215D3850009B39B7 /* SizeExtensions.swift in Sources */,
 				D12AB71B215D2BB50013BA68 /* CacheSerializer.swift in Sources */,
+				D10CEBD922C4E6AC0065C727 /* ImageBinder.swift in Sources */,
 				4B46CC6C21744AC500D90C4A /* DiskStorage.swift in Sources */,
 				D8B3692821C5CED6000F36F6 /* RedirectHandler.swift in Sources */,
 				4B531FE722C35B7B0041899C /* KFImage.swift in Sources */,
@@ -1325,6 +1334,7 @@
 				D12AB6E8215D2BB50013BA68 /* GIFAnimatedImage.swift in Sources */,
 				D13646742165A1A100A33652 /* Result.swift in Sources */,
 				D1A1CC9A219FAB4B00263AD8 /* Source.swift in Sources */,
+				D10CEBD622C4E6AC0065C727 /* ImageBinder.swift in Sources */,
 				4BD821622189FC0C0084CC21 /* SessionDelegate.swift in Sources */,
 				D12AB6E0215D2BB50013BA68 /* Filter.swift in Sources */,
 				D12AB6C4215D2BB50013BA68 /* Resource.swift in Sources */,

+ 59 - 0
Sources/SwiftUI/ImageBinder.swift

@@ -0,0 +1,59 @@
+//
+//  ImageBinder.swift
+//  Kingfisher
+//
+//  Created by onevcat on 2019/06/27.
+//
+//  Copyright (c) 2019 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.
+
+import Combine
+import SwiftUI
+
+@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
+class ImageBinder: BindableObject {
+    let url: URL
+    var didChange = PassthroughSubject<KFCrossPlatformImage?, Never>()
+
+    var onDone = PassthroughSubject<RetrieveImageResult, KingfisherError>()
+
+    var image: Kingfisher.KFCrossPlatformImage? {
+        didSet {
+            didChange.send(image)
+        }
+    }
+
+    init(url: URL) {
+        self.url = url
+    }
+
+    func start() {
+        _ = KingfisherManager.shared.retrieveImage(with: .network(url)) { r in
+            switch r {
+            case .success(let result):
+                self.image = result.image
+                self.onDone.send(result)
+                self.onDone.send(completion: .finished)
+            case .failure(let error):
+                self.onDone.send(completion: .failure(error))
+            }
+        }
+    }
+}

+ 40 - 37
Sources/SwiftUI/KFImage.swift

@@ -2,7 +2,7 @@
 //  KFImage.swift
 //  Kingfisher
 //
-//  Created by jp20028 on 2019/06/26.
+//  Created by onevcat on 2019/06/26.
 //
 //  Copyright (c) 2019 Wei Wang <onevcat@gmail.com>
 //
@@ -28,67 +28,70 @@ import SwiftUI
 import Combine
 
 @available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
-class ImageBinder: BindableObject {
-    let url: URL
-    var didChange = PassthroughSubject<Kingfisher.KFCrossPlatformImage?, Never>()
-
-    var image: Kingfisher.KFCrossPlatformImage? {
-        didSet {
-            didChange.send(image)
-        }
-    }
-
-    init(url: URL) {
-        self.url = url
-        _ = KingfisherManager.shared.retrieveImage(with: .network(url)) { r in
-            switch r {
-            case .success(let result): self.image = result.image
-            case .failure(let error): break
-            }
-        }
-    }
-}
-
-@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
-public struct KFImage: SwiftUI.View {
+public struct KFImage: View {
 
     static let empty = Kingfisher.KFCrossPlatformImage()
 
-    private var capInsets: EdgeInsets?
-    private var resizingMode: SwiftUI.Image.ResizingMode?
-
-    var config: [(SwiftUI.Image) -> SwiftUI.Image]
+    var configs: [(Image) -> Image]
 
     @ObjectBinding var binder: ImageBinder
 
     public init(url: URL) {
         binder = ImageBinder(url: url)
-        config = []
+        configs = []
     }
 
-    public var body: some SwiftUI.View {
+    public var body: some View {
         #if canImport(UIKit)
-        let image = SwiftUI.Image(uiImage: binder.image ?? KFImage.empty)
+        let image = Image(uiImage: binder.image ?? KFImage.empty)
         #elseif canImport(AppKit)
-        let image = SwiftUI.Image(nsImage: binder.image ?? KFImage.empty)
+        let image = Image(nsImage: binder.image ?? KFImage.empty)
         #endif
 
-        return config.reduce(image) { current, config in config(current) }
+        return configs
+            .reduce(image) { current, config in config(current) }
+            .onAppear { self.binder.start() }
     }
+}
 
-    public func resizable(capInsets: EdgeInsets = EdgeInsets(), resizingMode: SwiftUI.Image.ResizingMode = .stretch) -> KFImage {
+@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
+extension KFImage {
+
+    public func config(_ block: @escaping (Image) -> Image) -> KFImage {
         var result = self
-        result.config.append { $0.resizable(capInsets: capInsets, resizingMode: resizingMode) }
+        result.configs.append(block)
         return result
     }
+
+    public func resizable(
+        capInsets: EdgeInsets = EdgeInsets(),
+        resizingMode: Image.ResizingMode = .stretch) -> KFImage
+    {
+        config { $0.resizable(capInsets: capInsets, resizingMode: resizingMode) }
+    }
+
+    public func renderingMode(_ renderingMode: Image.TemplateRenderingMode?) -> KFImage {
+        config { $0.renderingMode(renderingMode) }
+    }
+
+    public func interpolation(_ interpolation: Image.Interpolation) -> KFImage {
+        config { $0.interpolation(interpolation) }
+    }
+
+    public func antialiased(_ isAntialiased: Bool) -> KFImage {
+        config { $0.antialiased(isAntialiased) }
+    }
 }
 
 #if DEBUG
 @available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
 struct KFImage_Previews : PreviewProvider {
-    static var previews: some SwiftUI.View {
+    static var previews: some View {
         KFImage(url:URL(string: "https://raw.githubusercontent.com/onevcat/Kingfisher/master/images/logo.png")!)
-        .resizable().aspectRatio(contentMode: .fit).padding()
+        .resizable()
+        .interpolation(.medium)
+        .aspectRatio(contentMode: .fit)
+        .padding()
     }
 }
 #endif