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 8f819770b..a7002274b 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)? @@ -85,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 @@ -147,6 +150,7 @@ public final class FetchImage: ObservableObject, Identifiable { } ) imageTask = task + onStart?(task) } private func handle(preview: ImageResponse) { @@ -184,6 +188,7 @@ public final class FetchImage: ObservableObject, Identifiable { handle(result: .failure(error)) } } + cancellable = AnyCancellable { task.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) } 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.