فهرست منبع

An option to allow load before view appear

onevcat 3 سال پیش
والد
کامیت
ebd0535fa5
3فایلهای تغییر یافته به همراه12 افزوده شده و 1 حذف شده
  1. 2 0
      Sources/SwiftUI/ImageContext.swift
  2. 5 0
      Sources/SwiftUI/KFImageOptions.swift
  3. 5 1
      Sources/SwiftUI/KFImageRenderer.swift

+ 2 - 0
Sources/SwiftUI/ImageContext.swift

@@ -46,6 +46,8 @@ extension KFImage {
         let onSuccessDelegate = Delegate<RetrieveImageResult, Void>()
         let onProgressDelegate = Delegate<(Int64, Int64), Void>()
         
+        var startLoadingBeforeViewAppear: Bool = false
+        
         init(source: Source?) {
             self.source = source
         }

+ 5 - 0
Sources/SwiftUI/KFImageOptions.swift

@@ -134,5 +134,10 @@ extension KFImageProtocol {
         context.options.transition = .fade(duration)
         return self
     }
+    
+    public func startLoadingBeforeViewAppear(_ flag: Bool = true) -> Self {
+        context.startLoadingBeforeViewAppear = flag
+        return self
+    }
 }
 #endif

+ 5 - 1
Sources/SwiftUI/KFImageRenderer.swift

@@ -37,7 +37,11 @@ struct KFImageRenderer<HoldingView> : View where HoldingView: KFImageHoldingView
     let context: KFImage.Context<HoldingView>
     
     var body: some View {
-        ZStack {
+        if context.startLoadingBeforeViewAppear && !binder.loadingOrSucceeded {
+            DispatchQueue.main.async { binder.start(context: context) }
+        }
+        
+        return ZStack {
             context.configurations
                 .reduce(HoldingView.created(from: binder.loadedImage, context: context)) {
                     current, config in config(current)