Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reducePriorityOnDisappear, flag to reduce download task priority. #2211

Merged
merged 4 commits into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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