From 55dbd800645edd055187836769b1df495bd439db Mon Sep 17 00:00:00 2001 From: paul-han Date: Wed, 6 Mar 2024 23:13:19 +0900 Subject: [PATCH 1/2] Add guard statement to prevent crash by force unwrapping --- Sources/NukeUI/Internal.swift | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/Sources/NukeUI/Internal.swift b/Sources/NukeUI/Internal.swift index 91c770e65..e440e5d23 100644 --- a/Sources/NukeUI/Internal.swift +++ b/Sources/NukeUI/Internal.swift @@ -29,12 +29,14 @@ 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 @@ -42,10 +44,12 @@ extension _PlatformBaseView { @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 From 4887e94b06a8370e916648f6770e9576b3dbae4e Mon Sep 17 00:00:00 2001 From: paul-han Date: Thu, 7 Mar 2024 13:28:29 +0900 Subject: [PATCH 2/2] Add `usePlaceholderAsFailure` to show placeholder in case of image loading failure --- Sources/NukeUI/LazyImageView.swift | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Sources/NukeUI/LazyImageView.swift b/Sources/NukeUI/LazyImageView.swift index 48ee27b85..cd1b7ddac 100644 --- a/Sources/NukeUI/LazyImageView.swift +++ b/Sources/NukeUI/LazyImageView.swift @@ -53,6 +53,8 @@ public final class LazyImageView: _PlatformBaseView { } } + public var usePlaceholderAsFailure = true + private var placeholderViewConstraints: [NSLayoutConstraint] = [] // MARK: Failure View @@ -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