Clean up ActionNotificationGroupTableViewCell avatar fetching code
This commit is contained in:
parent
0f6e9c97cc
commit
90809811c1
|
@ -78,6 +78,14 @@ class ImageCache {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func get(_ url: URL, loadOriginal: Bool = false) async -> (Data?, UIImage?) {
|
||||||
|
return await withCheckedContinuation { continuation in
|
||||||
|
_ = get(url, loadOriginal: loadOriginal) { data, image in
|
||||||
|
continuation.resume(returning: (data, image))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func fetchIfNotCached(_ url: URL) {
|
func fetchIfNotCached(_ url: URL) {
|
||||||
// if caching is disabled, don't bother fetching since nothing will be done with the result
|
// if caching is disabled, don't bother fetching since nothing will be done with the result
|
||||||
guard !ImageCache.disableCaching else { return }
|
guard !ImageCache.disableCaching else { return }
|
||||||
|
|
|
@ -25,7 +25,6 @@ class ActionNotificationGroupTableViewCell: UITableViewCell {
|
||||||
var group: NotificationGroup!
|
var group: NotificationGroup!
|
||||||
var statusID: String!
|
var statusID: String!
|
||||||
|
|
||||||
private var avatarRequests = [String: ImageCache.Request]()
|
|
||||||
private var updateTimestampWorkItem: DispatchWorkItem?
|
private var updateTimestampWorkItem: DispatchWorkItem?
|
||||||
private var isGrayscale = false
|
private var isGrayscale = false
|
||||||
|
|
||||||
|
@ -47,7 +46,9 @@ class ActionNotificationGroupTableViewCell: UITableViewCell {
|
||||||
}
|
}
|
||||||
|
|
||||||
if isGrayscale != Preferences.shared.grayscaleImages {
|
if isGrayscale != Preferences.shared.grayscaleImages {
|
||||||
updateGrayscaleableUI()
|
Task {
|
||||||
|
await updateGrayscaleableUI()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,29 +79,11 @@ class ActionNotificationGroupTableViewCell: UITableViewCell {
|
||||||
|
|
||||||
actionAvatarStackView.arrangedSubviews.forEach { $0.removeFromSuperview() }
|
actionAvatarStackView.arrangedSubviews.forEach { $0.removeFromSuperview() }
|
||||||
var imageViews = [UIImageView]()
|
var imageViews = [UIImageView]()
|
||||||
for account in people {
|
for _ in people {
|
||||||
let imageView = UIImageView()
|
let imageView = UIImageView()
|
||||||
imageView.translatesAutoresizingMaskIntoConstraints = false
|
imageView.translatesAutoresizingMaskIntoConstraints = false
|
||||||
imageView.layer.masksToBounds = true
|
imageView.layer.masksToBounds = true
|
||||||
imageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadiusFraction * 30
|
imageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadiusFraction * 30
|
||||||
if let avatarURL = account.avatar {
|
|
||||||
avatarRequests[account.id] = ImageCache.avatars.get(avatarURL) { [weak self] (_, image) in
|
|
||||||
guard let self = self else { return }
|
|
||||||
guard let image = image,
|
|
||||||
self.group.id == group.id,
|
|
||||||
let transformedImage = ImageGrayscalifier.convertIfNecessary(url: avatarURL, image: image) else {
|
|
||||||
DispatchQueue.main.async {
|
|
||||||
self.avatarRequests.removeValue(forKey: account.id)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
DispatchQueue.main.async {
|
|
||||||
self.avatarRequests.removeValue(forKey: account.id)
|
|
||||||
imageView.image = transformedImage
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
actionAvatarStackView.addArrangedSubview(imageView)
|
actionAvatarStackView.addArrangedSubview(imageView)
|
||||||
imageViews.append(imageView)
|
imageViews.append(imageView)
|
||||||
|
|
||||||
|
@ -115,6 +98,10 @@ class ActionNotificationGroupTableViewCell: UITableViewCell {
|
||||||
}
|
}
|
||||||
NSLayoutConstraint.activate(imageViews.map { $0.widthAnchor.constraint(equalTo: $0.heightAnchor) })
|
NSLayoutConstraint.activate(imageViews.map { $0.widthAnchor.constraint(equalTo: $0.heightAnchor) })
|
||||||
|
|
||||||
|
Task {
|
||||||
|
await updateGrayscaleableUI()
|
||||||
|
}
|
||||||
|
|
||||||
updateTimestamp()
|
updateTimestamp()
|
||||||
actionLabel.setEmojis(pairs: people.map { ($0.displayOrUserName, $0.emojis) }, identifier: group.id)
|
actionLabel.setEmojis(pairs: people.map { ($0.displayOrUserName, $0.emojis) }, identifier: group.id)
|
||||||
|
|
||||||
|
@ -122,36 +109,29 @@ class ActionNotificationGroupTableViewCell: UITableViewCell {
|
||||||
statusContentLabel.text = try! doc.text()
|
statusContentLabel.text = try! doc.text()
|
||||||
}
|
}
|
||||||
|
|
||||||
private func updateGrayscaleableUI() {
|
@MainActor
|
||||||
|
private func updateGrayscaleableUI() async {
|
||||||
let people = group.notifications.compactMap { mastodonController.persistentContainer.account(for: $0.account.id) }
|
let people = group.notifications.compactMap { mastodonController.persistentContainer.account(for: $0.account.id) }
|
||||||
let groupID = group.id
|
let groupID = group.id
|
||||||
|
|
||||||
for (index, account) in people.enumerated() {
|
for (index, account) in people.enumerated() {
|
||||||
guard actionAvatarStackView.arrangedSubviews.count > index,
|
guard actionAvatarStackView.arrangedSubviews.count > index,
|
||||||
let imageView = actionAvatarStackView.arrangedSubviews[index] as? UIImageView else {
|
let imageView = actionAvatarStackView.arrangedSubviews[index] as? UIImageView,
|
||||||
|
let avatarURL = account.avatar else {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if let avatarURL = account.avatar {
|
Task {
|
||||||
avatarRequests[account.id] = ImageCache.avatars.get(avatarURL) { [weak self] (_, image) in
|
let (_, image) = await ImageCache.avatars.get(avatarURL)
|
||||||
guard let self = self else { return }
|
|
||||||
guard let image = image,
|
guard let image = image,
|
||||||
self.group.id == groupID,
|
self.group.id == groupID,
|
||||||
let transformedImage = ImageGrayscalifier.convertIfNecessary(url: avatarURL, image: image) else {
|
let transformedImage = ImageGrayscalifier.convertIfNecessary(url: avatarURL, image: image) else {
|
||||||
DispatchQueue.main.async {
|
|
||||||
self.avatarRequests.removeValue(forKey: account.id)
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
DispatchQueue.main.async {
|
|
||||||
self.avatarRequests.removeValue(forKey: account.id)
|
|
||||||
imageView.image = transformedImage
|
imageView.image = transformedImage
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private func updateTimestamp() {
|
private func updateTimestamp() {
|
||||||
guard let notification = group.notifications.first else {
|
guard let notification = group.notifications.first else {
|
||||||
|
@ -217,7 +197,6 @@ class ActionNotificationGroupTableViewCell: UITableViewCell {
|
||||||
override func prepareForReuse() {
|
override func prepareForReuse() {
|
||||||
super.prepareForReuse()
|
super.prepareForReuse()
|
||||||
|
|
||||||
avatarRequests.values.forEach { $0.cancel() }
|
|
||||||
updateTimestampWorkItem?.cancel()
|
updateTimestampWorkItem?.cancel()
|
||||||
updateTimestampWorkItem = nil
|
updateTimestampWorkItem = nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue