Sfoglia il codice sorgente

Merge pull request #2211 from Aelx-Vaiman/master

reducePriorityOnDisappear, flag to reduce download task priority.
Wei Wang 1 anno fa
parent
commit
d51f17811c

+ 12 - 0
Sources/SwiftUI/ImageBinder.swift

@@ -144,6 +144,18 @@ extension KFImage {
             downloadTask = nil
             loading = false
         }
+        
+        /// Restores the download task priority to default if it is in progress.
+        func restorePriorityOnAppear() {
+            guard let downloadTask = downloadTask, loading == true else { return }
+            downloadTask.sessionTask.task.priority = URLSessionTask.defaultPriority
+        }
+        
+        /// Reduce the download task priority if it is in progress.
+        func reducePriorityOnDisappear() {
+            guard let downloadTask = downloadTask, loading == true else { return }
+            downloadTask.sessionTask.task.priority = URLSessionTask.lowPriority
+        }
     }
 }
 #endif

+ 1 - 0
Sources/SwiftUI/ImageContext.swift

@@ -41,6 +41,7 @@ extension KFImage {
         var contentConfiguration: ((HoldingView) -> AnyView)? = nil
         
         var cancelOnDisappear: Bool = false
+        var reducePriorityOnDisappear: Bool = false
         var placeholder: ((Progress) -> AnyView)? = nil
 
         let onFailureDelegate = Delegate<KingfisherError, Void>()

+ 9 - 0
Sources/SwiftUI/KFImageOptions.swift

@@ -121,6 +121,15 @@ extension KFImageProtocol {
         context.cancelOnDisappear = flag
         return self
     }
+    
+    /// Sets reduce priority  of the download task to low,  bound to `self` when the view disappearing.
+    /// - Parameter flag: Whether reduce the priority task or not.
+    /// - Returns: A `KFImage` view that reduces downloading task priority when disappears.
+    public func reducePriorityOnDisappear(_ flag: Bool) -> Self {
+        context.reducePriorityOnDisappear = flag
+        return self
+    }
+
 
     /// Sets a fade transition for the image task.
     /// - Parameter duration: The duration of the fade transition.

+ 6 - 0
Sources/SwiftUI/KFImageRenderer.swift

@@ -58,6 +58,10 @@ struct KFImageRenderer<HoldingView> : View where HoldingView: KFImageHoldingView
                     }
                     if !binder.loadingOrSucceeded {
                         binder.start(context: context)
+                    } else {
+                        if context.reducePriorityOnDisappear {
+                            binder.restorePriorityOnAppear()
+                        }
                     }
                 }
                 .onDisappear { [weak binder = self.binder] in
@@ -66,6 +70,8 @@ struct KFImageRenderer<HoldingView> : View where HoldingView: KFImageHoldingView
                     }
                     if context.cancelOnDisappear {
                         binder.cancel()
+                    } else if context.reducePriorityOnDisappear {
+                        binder.reducePriorityOnDisappear()
                     }
                 }
             }