Skip to content

Commit

Permalink
Merge pull request #2211 from Aelx-Vaiman/master
Browse files Browse the repository at this point in the history
reducePriorityOnDisappear, flag to reduce download task priority.
  • Loading branch information
onevcat authored Jun 9, 2024
2 parents 8bf3571 + b02f3cc commit d51f178
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Sources/SwiftUI/ImageBinder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 change: 1 addition & 0 deletions Sources/SwiftUI/ImageContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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>()
Expand Down
9 changes: 9 additions & 0 deletions Sources/SwiftUI/KFImageOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions Sources/SwiftUI/KFImageRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -66,6 +70,8 @@ struct KFImageRenderer<HoldingView> : View where HoldingView: KFImageHoldingView
}
if context.cancelOnDisappear {
binder.cancel()
} else if context.reducePriorityOnDisappear {
binder.reducePriorityOnDisappear()
}
}
}
Expand Down

0 comments on commit d51f178

Please sign in to comment.