Ver Fonte

Use StateObject for image binder

onevcat há 4 anos atrás
pai
commit
ae88f5315b

+ 2 - 2
Demo/Demo/Kingfisher-Demo/SwiftUIViews/AnimatedImageDemo.swift

@@ -27,7 +27,7 @@
 import SwiftUI
 import Kingfisher
 
-@available(iOS 13.0, *)
+@available(iOS 14.0, *)
 struct AnimatedImageDemo: View {
     
     @State private var index = 1
@@ -66,7 +66,7 @@ struct AnimatedImageDemo: View {
     
 }
 
-@available(iOS 13.0, *)
+@available(iOS 14.0, *)
 struct AnimatedImageDemo_Previews: PreviewProvider {
     
     static var previews: some View {

+ 3 - 3
Demo/Demo/Kingfisher-Demo/SwiftUIViews/ListDemo.swift

@@ -27,7 +27,7 @@
 import Kingfisher
 import SwiftUI
 
-@available(iOS 13.0, *)
+@available(iOS 14.0, *)
 struct ListDemo : View {
 
     let index = 1 ..< 700
@@ -40,7 +40,7 @@ struct ListDemo : View {
     }
 }
 
-@available(iOS 13.0, *)
+@available(iOS 14.0, *)
 struct ImageCell: View {
 
     @State var done = false
@@ -89,7 +89,7 @@ struct ImageCell: View {
 
 }
 
-@available(iOS 13.0, *)
+@available(iOS 14.0, *)
 struct SwiftUIList_Previews : PreviewProvider {
     static var previews: some View {
         ListDemo()

+ 4 - 6
Demo/Demo/Kingfisher-Demo/SwiftUIViews/MainView.swift

@@ -27,7 +27,7 @@
 import SwiftUI
 import Kingfisher
 
-@available(iOS 13.0, *)
+@available(iOS 14.0, *)
 struct MainView: View {
     var body: some View {
         List {
@@ -43,16 +43,14 @@ struct MainView: View {
             NavigationLink(destination: SingleViewDemo()) { Text("Basic Image") }
             NavigationLink(destination: SizingAnimationDemo()) { Text("Sizing Toggle") }
             NavigationLink(destination: ListDemo()) { Text("List") }
-            if #available(iOS 14.0, *) {
-                NavigationLink(destination: LazyVStackDemo()) { Text("Stack") }
-                NavigationLink(destination: GridDemo()) { Text("Grid") }
-            }
+            NavigationLink(destination: LazyVStackDemo()) { Text("Stack") }
+            NavigationLink(destination: GridDemo()) { Text("Grid") }
             NavigationLink(destination: AnimatedImageDemo()) { Text("Animated Image") }
         }.navigationBarTitle(Text("SwiftUI Sample"))
     }
 }
 
-@available(iOS 13.0, *)
+@available(iOS 14.0, *)
 struct MainView_Previews: PreviewProvider {
     static var previews: some View {
         MainView()

+ 2 - 2
Demo/Demo/Kingfisher-Demo/SwiftUIViews/SingleViewDemo.swift

@@ -27,7 +27,7 @@
 import Kingfisher
 import SwiftUI
 
-@available(iOS 13.0, *)
+@available(iOS 14.0, *)
 struct SingleViewDemo : View {
 
     @State private var index = 1
@@ -72,7 +72,7 @@ struct SingleViewDemo : View {
     }
 }
 
-@available(iOS 13.0, *)
+@available(iOS 14.0, *)
 struct SingleViewDemo_Previews : PreviewProvider {
     static var previews: some View {
         SingleViewDemo()

+ 2 - 2
Demo/Demo/Kingfisher-Demo/SwiftUIViews/SizingAnimationDemo.swift

@@ -27,7 +27,7 @@
 import SwiftUI
 import Kingfisher
 
-@available(iOS 13.0, *)
+@available(iOS 14.0, *)
 struct SizingAnimationDemo: View {
     @State var imageSize: CGFloat = 250
     @State var isPlaying = false
@@ -62,7 +62,7 @@ struct SizingAnimationDemo: View {
     }
 }
 
-@available(iOS 13.0, *)
+@available(iOS 14.0, *)
 struct SizingAnimationDemo_Previews: PreviewProvider {
     static var previews: some View {
         SizingAnimationDemo()

+ 1 - 1
Demo/Demo/Kingfisher-Demo/ViewControllers/SwiftUIViewController.swift

@@ -27,7 +27,7 @@
 import SwiftUI
 import UIKit
 
-@available(iOS 13.0, *)
+@available(iOS 14.0, *)
 class SwiftUIViewController: UIHostingController<MainView> {
     required init?(coder: NSCoder) {
         super.init(coder: coder, rootView: MainView())

+ 6 - 1
Sources/SwiftUI/KFImageProtocol.swift

@@ -37,7 +37,12 @@ public protocol KFImageProtocol: View, KFOptionSetter {
 @available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
 extension KFImageProtocol {
     public var body: some View {
-        KFImageRenderer<HoldingView>(context)
+        KFImageRenderer<HoldingView>(
+            binder: context.binder,
+            placeholder: context.placeholder,
+            cancelOnDisappear: context.cancelOnDisappear,
+            configurations: context.configurations
+        )
             .id(context.binder)
     }
     

+ 1 - 8
Sources/SwiftUI/KFImageRenderer.swift

@@ -33,7 +33,7 @@ import SwiftUI
 struct KFImageRenderer<HoldingView> : View where HoldingView: KFImageHoldingView {
     
     /// An image binder that manages loading and cancelling image related task.
-    @ObservedObject var binder: KFImage.ImageBinder
+    @StateObject var binder: KFImage.ImageBinder
 
     // Acts as a placeholder when loading an image.
     var placeholder: AnyView?
@@ -44,13 +44,6 @@ struct KFImageRenderer<HoldingView> : View where HoldingView: KFImageHoldingView
     // Configurations should be performed on the image.
     let configurations: [(HoldingView) -> HoldingView]
 
-    init(_ context: KFImage.Context<HoldingView>) {
-        self.binder = context.binder
-        self.configurations = context.configurations
-        self.placeholder = context.placeholder
-        self.cancelOnDisappear = context.cancelOnDisappear
-    }
-
     /// Declares the content and behavior of this view.
     @ViewBuilder
     var body: some View {