Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 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 request is started.
public var onStart: ((ImageTask) -> Void)?

/// Gets called when the current request is completed.
public var onCompletion: ((Result<ImageResponse, Error>) -> Void)?

Expand Down Expand Up @@ -147,6 +150,7 @@ public final class FetchImage: ObservableObject, Identifiable {
}
)
imageTask = task
onStart?(task)
}

private func handle(preview: ImageResponse) {
Expand Down Expand Up @@ -184,6 +188,10 @@ public final class FetchImage: ObservableObject, Identifiable {
handle(result: .failure(error))
}
}

if let task = imageTask {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The task is always going to be empty at this point, to it's not going to get called. I'm not entire sure why I added this load() variant in the first place, to be honest, but I think it would be acceptable to not call onStart here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, I agree, I wasn't sure if you had something planned so I added it just in case. I'll remove it

onStart?(task)
}
cancellable = AnyCancellable { task.cancel() }
}

Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions Sources/NukeUI/LazyImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public struct LazyImage<Content: View>: 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<ImageResponse, Error>) -> Void)?

Expand Down Expand Up @@ -108,6 +109,11 @@ public struct LazyImage<Content: View>: 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 }
Expand Down Expand Up @@ -153,6 +159,7 @@ public struct LazyImage<Content: View>: View {
private func onAppear() {
viewModel.transaction = transaction
viewModel.pipeline = pipeline
viewModel.onStart = onStart
viewModel.onCompletion = onCompletion
viewModel.load(context?.request)
}
Expand Down