Avoid unnecessarily recreating avatar views in notifications cells

This commit is contained in:
Shadowfacts 2023-09-25 21:44:43 -04:00
parent bf1ed57180
commit a2fe0dfb78
2 changed files with 35 additions and 23 deletions

View File

@ -139,19 +139,25 @@ class ActionNotificationGroupCollectionViewCell: UICollectionViewListCell {
mastodonController.persistentContainer.account(for: $0.account.id)
}
avatarStack.arrangedSubviews.forEach { $0.removeFromSuperview() }
for avatarURL in people.lazy.compactMap(\.avatar).prefix(10) {
let imageView = CachedImageView(cache: .avatars)
let visibleAvatars = Array(people.lazy.compactMap(\.avatar).prefix(10))
for (index, avatarURL) in visibleAvatars.enumerated() {
let imageView: CachedImageView
if index < avatarStack.arrangedSubviews.count {
imageView = avatarStack.arrangedSubviews[index] as! CachedImageView
} else {
imageView = CachedImageView(cache: .avatars)
imageView.contentMode = .scaleAspectFill
imageView.layer.masksToBounds = true
imageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadiusFraction * 30
imageView.layer.cornerCurve = .continuous
avatarStack.addArrangedSubview(imageView)
imageView.widthAnchor.constraint(equalTo: imageView.heightAnchor).isActive = true
}
imageView.update(for: avatarURL)
}
NSLayoutConstraint.activate(avatarStack.arrangedSubviews.map {
$0.widthAnchor.constraint(equalTo: $0.heightAnchor)
})
for imageView in avatarStack.arrangedSubviews.dropFirst(visibleAvatars.count) {
avatarStack.removeArrangedSubview(imageView)
}
actionLabel.setEmojis(pairs: people.map { ($0.displayOrUserName, $0.emojis) }, identifier: group.id)

View File

@ -116,19 +116,25 @@ class FollowNotificationGroupCollectionViewCell: UICollectionViewListCell {
}, identifier: group.id)
updateTimestamp()
avatarStack.arrangedSubviews.forEach { $0.removeFromSuperview() }
for avatarURL in people.lazy.compactMap(\.avatar).prefix(10) {
let imageView = CachedImageView(cache: .avatars)
let visibleAvatars = Array(people.lazy.compactMap(\.avatar).prefix(10))
for (index, avatarURL) in visibleAvatars.enumerated() {
let imageView: CachedImageView
if index < avatarStack.arrangedSubviews.count {
imageView = avatarStack.arrangedSubviews[index] as! CachedImageView
} else {
imageView = CachedImageView(cache: .avatars)
imageView.contentMode = .scaleAspectFill
imageView.layer.masksToBounds = true
imageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadiusFraction * 30
imageView.layer.cornerCurve = .continuous
imageView.update(for: avatarURL)
avatarStack.addArrangedSubview(imageView)
imageView.widthAnchor.constraint(equalTo: imageView.heightAnchor).isActive = true
}
imageView.update(for: avatarURL)
}
for imageView in avatarStack.arrangedSubviews.dropFirst(visibleAvatars.count) {
avatarStack.removeArrangedSubview(imageView)
}
NSLayoutConstraint.activate(avatarStack.arrangedSubviews.map {
$0.widthAnchor.constraint(equalTo: $0.heightAnchor)
})
}
private func updateActionLabel(names: [NSAttributedString]) -> NSAttributedString {