Skip to content

Commit

Permalink
Merge pull request #764 from mlight3/mlight3/placeholder
Browse files Browse the repository at this point in the history
Add usePlaceholderAsFailure to show placeholder in case of image loading failure
  • Loading branch information
kean authored Mar 23, 2024
2 parents d205efe + 4887e94 commit eb7c5b8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
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

0 comments on commit eb7c5b8

Please sign in to comment.