diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e15e3bac..0fffd4b0f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,8 @@ *Mar 25, 2023* -- Add `makeImageView` closure to `LazyImageView` to allow using custom view for rendering images +- Add `makeImageView` closure to `LazyImageView` to allow using custom view for rendering images +- Add `onCompletion` closure to `LazyImage` - Fix an issue with `.videoAssetKey` value missing from `ImageContainer` - Fix an issue with `.gif` being encoded as `.jpeg` when `.storeEncodedImages` policy is used diff --git a/Sources/NukeUI/FetchImage.swift b/Sources/NukeUI/FetchImage.swift index 7d5d06f23..ec8850f5c 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 current request is completed. + public var onCompletion: ((Result) -> Void)? + private var imageTask: ImageTask? private var lastResponse: ImageResponse? private var cancellable: AnyCancellable? @@ -163,6 +166,7 @@ public final class FetchImage: ObservableObject, Identifiable { self.imageContainer = response.container } self.result = result + self.onCompletion?(result) } // MARK: Load (Async/Await) diff --git a/Sources/NukeUI/LazyImage.swift b/Sources/NukeUI/LazyImage.swift index 4a3343d4e..4c39dc04e 100644 --- a/Sources/NukeUI/LazyImage.swift +++ b/Sources/NukeUI/LazyImage.swift @@ -25,6 +25,7 @@ public struct LazyImage: View { private var transaction: Transaction private var pipeline: ImagePipeline = .shared private var onDisappearBehavior: DisappearBehavior? = .cancel + private var onCompletion: ((Result) -> Void)? // MARK: Initializers @@ -112,6 +113,11 @@ public struct LazyImage: View { map { $0.onDisappearBehavior = behavior } } + /// Gets called when the current request is completed. + public func onCompletion(_ closure: @escaping (Result) -> Void) -> Self { + map { $0.onCompletion = closure } + } + private func map(_ closure: (inout LazyImage) -> Void) -> Self { var copy = self closure(©) @@ -172,6 +178,7 @@ public struct LazyImage: View { private func onAppear() { viewModel.transaction = transaction viewModel.pipeline = pipeline + viewModel.onCompletion = onCompletion guard viewModel.cachedResponse == nil else { return } viewModel.load(context?.request)