Hide placeholder image from link card when none provided

Closes #358
This commit is contained in:
Shadowfacts 2023-02-24 18:22:27 -05:00
parent b2977540e0
commit daf3741c9a
1 changed files with 23 additions and 46 deletions

View File

@ -23,15 +23,16 @@ class StatusCardView: UIView {
private let activeBackgroundColor = UIColor.secondarySystemFill
private let inactiveBackgroundColor = UIColor.secondarySystemBackground
private var imageRequest: ImageCache.Request?
private var isGrayscale = false
private var hStack: UIStackView!
private var titleLabel: UILabel!
private var descriptionLabel: UILabel!
private var domainLabel: UILabel!
private var imageView: UIImageView!
private var imageView: CachedImageView!
private var placeholderImageView: UIImageView!
private var leadingSpacer: UIView!
private var trailingSpacer: UIView!
override init(frame: CGRect) {
super.init(frame: frame)
@ -76,20 +77,22 @@ class StatusCardView: UIView {
])
vStack.axis = .vertical
vStack.alignment = .leading
vStack.distribution = .fill
vStack.spacing = 0
imageView = UIImageView()
imageView = CachedImageView(cache: .attachments)
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
let spacer = UIView()
spacer.backgroundColor = .clear
leadingSpacer = UIView()
leadingSpacer.backgroundColor = .clear
trailingSpacer = UIView()
trailingSpacer.backgroundColor = .clear
hStack = UIStackView(arrangedSubviews: [
leadingSpacer,
imageView,
vStack,
spacer,
trailingSpacer,
])
hStack.translatesAutoresizingMaskIntoConstraints = false
hStack.axis = .horizontal
@ -107,6 +110,7 @@ class StatusCardView: UIView {
placeholderImageView.translatesAutoresizingMaskIntoConstraints = false
placeholderImageView.contentMode = .scaleAspectFit
placeholderImageView.tintColor = .gray
placeholderImageView.isHidden = true
addSubview(placeholderImageView)
@ -114,9 +118,10 @@ class StatusCardView: UIView {
imageView.heightAnchor.constraint(equalTo: heightAnchor),
imageView.widthAnchor.constraint(equalTo: imageView.heightAnchor),
vStack.heightAnchor.constraint(equalTo: heightAnchor, constant: -8),
vStack.heightAnchor.constraint(lessThanOrEqualTo: heightAnchor, constant: -8),
spacer.widthAnchor.constraint(equalToConstant: 4),
leadingSpacer.widthAnchor.constraint(equalToConstant: 4),
trailingSpacer.widthAnchor.constraint(equalToConstant: 4),
hStack.leadingAnchor.constraint(equalTo: leadingAnchor),
hStack.trailingAnchor.constraint(equalTo: trailingAnchor),
@ -128,8 +133,6 @@ class StatusCardView: UIView {
placeholderImageView.centerXAnchor.constraint(equalTo: imageView.centerXAnchor),
placeholderImageView.centerYAnchor.constraint(equalTo: imageView.centerYAnchor),
])
NotificationCenter.default.addObserver(self, selector: #selector(updateUIForPreferences), name: .preferencesChanged, object: nil)
}
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
@ -161,10 +164,15 @@ class StatusCardView: UIView {
return
}
self.imageView.image = nil
updateGrayscaleableUI(card: card)
updateUIForPreferences()
if let image = card.image {
imageView.update(for: URL(image), blurhash: card.blurhash)
imageView.isHidden = false
leadingSpacer.isHidden = true
} else {
imageView.update(for: nil)
imageView.isHidden = true
leadingSpacer.isHidden = false
}
let title = card.title.trimmingCharacters(in: .whitespacesAndNewlines)
titleLabel.text = title
@ -182,37 +190,6 @@ class StatusCardView: UIView {
}
}
@objc private func updateUIForPreferences() {
if isGrayscale != Preferences.shared.grayscaleImages,
let card = card {
updateGrayscaleableUI(card: card)
}
}
private func updateGrayscaleableUI(card: Card) {
isGrayscale = Preferences.shared.grayscaleImages
if let imageURL = card.image {
placeholderImageView.isHidden = true
imageRequest = ImageCache.attachments.get(URL(imageURL)!, completion: { (_, image) in
guard let image = image,
self.card?.image == imageURL,
let transformedImage = ImageGrayscalifier.convertIfNecessary(url: URL(imageURL)!, image: image) else {
return
}
DispatchQueue.main.async {
self.imageView.image = transformedImage
}
})
if imageRequest != nil {
loadBlurHash()
}
} else {
placeholderImageView.isHidden = false
}
}
private func loadBlurHash() {
guard let card = card, let hash = card.blurhash else { return }