Skip to content

Commit

Permalink
Add onCompletion closure to LazyImage
Browse files Browse the repository at this point in the history
  • Loading branch information
kean committed Mar 25, 2023
1 parent f163ffe commit f4d9b95
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions Sources/NukeUI/FetchImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<ImageResponse, Error>) -> Void)?

private var imageTask: ImageTask?
private var lastResponse: ImageResponse?
private var cancellable: AnyCancellable?
Expand Down Expand Up @@ -163,6 +166,7 @@ public final class FetchImage: ObservableObject, Identifiable {
self.imageContainer = response.container
}
self.result = result
self.onCompletion?(result)
}

// MARK: Load (Async/Await)
Expand Down
7 changes: 7 additions & 0 deletions Sources/NukeUI/LazyImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public struct LazyImage<Content: View>: View {
private var transaction: Transaction
private var pipeline: ImagePipeline = .shared
private var onDisappearBehavior: DisappearBehavior? = .cancel
private var onCompletion: ((Result<ImageResponse, Error>) -> Void)?

// MARK: Initializers

Expand Down Expand Up @@ -112,6 +113,11 @@ public struct LazyImage<Content: View>: View {
map { $0.onDisappearBehavior = behavior }
}

/// Gets called when the current request is completed.
public func onCompletion(_ closure: @escaping (Result<ImageResponse, Error>) -> Void) -> Self {
map { $0.onCompletion = closure }
}

private func map(_ closure: (inout LazyImage) -> Void) -> Self {
var copy = self
closure(&copy)
Expand Down Expand Up @@ -172,6 +178,7 @@ public struct LazyImage<Content: View>: View {
private func onAppear() {
viewModel.transaction = transaction
viewModel.pipeline = pipeline
viewModel.onCompletion = onCompletion

guard viewModel.cachedResponse == nil else { return }
viewModel.load(context?.request)
Expand Down

0 comments on commit f4d9b95

Please sign in to comment.