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