Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add usePlaceholderAsFailure to show placeholder in case of image loading failure #764

Merged
merged 2 commits into from
Mar 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 10 additions & 6 deletions Sources/NukeUI/Internal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,27 @@ typealias _PlatformColor = UIColor
extension _PlatformBaseView {
@discardableResult
func pinToSuperview() -> [NSLayoutConstraint] {
guard let superview else { return [] }

translatesAutoresizingMaskIntoConstraints = false
let constraints = [
topAnchor.constraint(equalTo: superview!.topAnchor),
bottomAnchor.constraint(equalTo: superview!.bottomAnchor),
leftAnchor.constraint(equalTo: superview!.leftAnchor),
rightAnchor.constraint(equalTo: superview!.rightAnchor)
topAnchor.constraint(equalTo: superview.topAnchor),
bottomAnchor.constraint(equalTo: superview.bottomAnchor),
leftAnchor.constraint(equalTo: superview.leftAnchor),
rightAnchor.constraint(equalTo: superview.rightAnchor)
]
NSLayoutConstraint.activate(constraints)
return constraints
}

@discardableResult
func centerInSuperview() -> [NSLayoutConstraint] {
guard let superview else { return [] }

translatesAutoresizingMaskIntoConstraints = false
let constraints = [
centerXAnchor.constraint(equalTo: superview!.centerXAnchor),
centerYAnchor.constraint(equalTo: superview!.centerYAnchor)
centerXAnchor.constraint(equalTo: superview.centerXAnchor),
centerYAnchor.constraint(equalTo: superview.centerYAnchor)
]
NSLayoutConstraint.activate(constraints)
return constraints
Expand Down
8 changes: 7 additions & 1 deletion Sources/NukeUI/LazyImageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public final class LazyImageView: _PlatformBaseView {
}
}

public var usePlaceholderAsFailure = true

private var placeholderViewConstraints: [NSLayoutConstraint] = []

// MARK: Failure View
Expand Down Expand Up @@ -323,7 +325,11 @@ public final class LazyImageView: _PlatformBaseView {
case let .success(response):
display(response.container, isFromMemory: isSync)
case .failure:
setFailureViewHidden(false)
if usePlaceholderAsFailure {
setPlaceholderViewHidden(false)
} else {
setFailureViewHidden(false)
}
}

imageTask = nil
Expand Down
Loading