From 8e6c1dc252f7498d9aff477f91afa5c525dd0fba Mon Sep 17 00:00:00 2001 From: mihajlo saric Date: Thu, 16 Nov 2023 16:43:46 +0100 Subject: [PATCH 1/2] Added the onStart callback to SwiftUI.LazyImage --- Sources/NukeUI/FetchImage.swift | 12 ++++++++++++ Sources/NukeUI/LazyImage.swift | 7 +++++++ 2 files changed, 19 insertions(+) diff --git a/Sources/NukeUI/FetchImage.swift b/Sources/NukeUI/FetchImage.swift index 8f819770b..0f92d5ea5 100644 --- a/Sources/NukeUI/FetchImage.swift +++ b/Sources/NukeUI/FetchImage.swift @@ -74,6 +74,9 @@ public final class FetchImage: ObservableObject, Identifiable { /// request. `[]` by default. public var processors: [any ImageProcessing] = [] + /// Gets called when the request is started. + public var onStart: ((ImageTask) -> Void)? + /// Gets called when the current request is completed. public var onCompletion: ((Result) -> Void)? @@ -147,6 +150,7 @@ public final class FetchImage: ObservableObject, Identifiable { } ) imageTask = task + onStart?(task) } private func handle(preview: ImageResponse) { @@ -184,6 +188,10 @@ public final class FetchImage: ObservableObject, Identifiable { handle(result: .failure(error)) } } + + if let task = imageTask { + onStart?(task) + } cancellable = AnyCancellable { task.cancel() } } @@ -215,6 +223,10 @@ public final class FetchImage: ObservableObject, Identifiable { self.lastResponse = response self.imageContainer = response.container }) + + if let task = imageTask { + onStart?(task) + } } // MARK: Cancel diff --git a/Sources/NukeUI/LazyImage.swift b/Sources/NukeUI/LazyImage.swift index c85fdd06b..653390732 100644 --- a/Sources/NukeUI/LazyImage.swift +++ b/Sources/NukeUI/LazyImage.swift @@ -24,6 +24,7 @@ public struct LazyImage: View { private var makeContent: ((LazyImageState) -> Content)? private var transaction: Transaction private var pipeline: ImagePipeline = .shared + private var onStart: ((ImageTask) -> Void)? private var onDisappearBehavior: DisappearBehavior? = .cancel private var onCompletion: ((Result) -> Void)? @@ -108,6 +109,11 @@ public struct LazyImage: View { case lowerPriority } + /// Gets called when the request is started. + public func onStart(_ closure: @escaping (ImageTask) -> Void) -> Self { + map { $0.viewModel.onStart = closure } + } + /// Override the behavior on disappear. By default, the view is reset. public func onDisappear(_ behavior: DisappearBehavior?) -> Self { map { $0.onDisappearBehavior = behavior } @@ -153,6 +159,7 @@ public struct LazyImage: View { private func onAppear() { viewModel.transaction = transaction viewModel.pipeline = pipeline + viewModel.onStart = onStart viewModel.onCompletion = onCompletion viewModel.load(context?.request) } From 8a3c5d843938998e03aa3354c2f3211f0cfd20a7 Mon Sep 17 00:00:00 2001 From: urbaneewe Date: Sat, 18 Nov 2023 12:02:05 +0100 Subject: [PATCH 2/2] Removed unnecessary onStart() calls, fixed typos --- Sources/Nuke/ImageRequest.swift | 2 +- Sources/Nuke/Tasks/TaskLoadImage.swift | 2 +- Sources/NukeUI/FetchImage.swift | 9 +-------- Tests/XCTestCaseExtensions.swift | 2 +- 4 files changed, 4 insertions(+), 11 deletions(-) diff --git a/Sources/Nuke/ImageRequest.swift b/Sources/Nuke/ImageRequest.swift index 6da4bb5d5..06b0fa803 100644 --- a/Sources/Nuke/ImageRequest.swift +++ b/Sources/Nuke/ImageRequest.swift @@ -279,7 +279,7 @@ public struct ImageRequest: CustomStringConvertible, Sendable, ExpressibleByStri /// Returns a raw value. public let rawValue: UInt16 - /// Initialializes options with a given raw values. + /// Initializes options with a given raw values. public init(rawValue: UInt16) { self.rawValue = rawValue } diff --git a/Sources/Nuke/Tasks/TaskLoadImage.swift b/Sources/Nuke/Tasks/TaskLoadImage.swift index 578410b7e..886e19cad 100644 --- a/Sources/Nuke/Tasks/TaskLoadImage.swift +++ b/Sources/Nuke/Tasks/TaskLoadImage.swift @@ -7,7 +7,7 @@ import Foundation /// Wrapper for tasks created by `loadImage` calls. /// /// Performs all the quick cache lookups and also manages image processing. -/// The coalesing for image processing is implemented on demand (extends the +/// The coalescing for image processing is implemented on demand (extends the /// scenarios in which coalescing can kick in). final class TaskLoadImage: ImagePipelineTask { override func start() { diff --git a/Sources/NukeUI/FetchImage.swift b/Sources/NukeUI/FetchImage.swift index 0f92d5ea5..a7002274b 100644 --- a/Sources/NukeUI/FetchImage.swift +++ b/Sources/NukeUI/FetchImage.swift @@ -88,7 +88,7 @@ public final class FetchImage: ObservableObject, Identifiable { imageTask?.cancel() } - /// Initialiazes the image. To load an image, use one of the `load()` methods. + /// Initializes the image. To load an image, use one of the `load()` methods. public init() {} // MARK: Loading Images @@ -189,9 +189,6 @@ public final class FetchImage: ObservableObject, Identifiable { } } - if let task = imageTask { - onStart?(task) - } cancellable = AnyCancellable { task.cancel() } } @@ -223,10 +220,6 @@ public final class FetchImage: ObservableObject, Identifiable { self.lastResponse = response self.imageContainer = response.container }) - - if let task = imageTask { - onStart?(task) - } } // MARK: Cancel diff --git a/Tests/XCTestCaseExtensions.swift b/Tests/XCTestCaseExtensions.swift index 45b21649b..b5b976d16 100644 --- a/Tests/XCTestCaseExtensions.swift +++ b/Tests/XCTestCaseExtensions.swift @@ -129,7 +129,7 @@ struct TestExpectationOperationQueue { return observer } - /// Fulfills an expectation as soon as a queue finished exeucting `n` + /// Fulfills an expectation as soon as a queue finished executing `n` /// operations (doesn't matter whether they were cancelled or executed). /// /// Automatically resumes a queue as soon as `n` operations are enqueued.