Cache image attachments

This commit is contained in:
Shadowfacts 2018-10-03 08:53:05 -04:00
parent fd611d1f04
commit 82e351465e
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
4 changed files with 4 additions and 13 deletions

View File

@ -13,6 +13,7 @@ class ImageCache {
static let avatars = ImageCache(name: "Avatars")
static let headers = ImageCache(name: "Headers")
static let attachments = ImageCache(name: "Attachments", diskExpiry: .seconds(60 * 60), memoryExpiry: .seconds(60))
let storage: Storage<UIImage>

View File

@ -19,8 +19,6 @@ class AttachmentView: UIImageView {
var attachment: Attachment!
var task: URLSessionDataTask?
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
@ -45,13 +43,11 @@ class AttachmentView: UIImageView {
}
func loadImage() {
task = URLSession.shared.dataTask(with: attachment.url) { data, response, error in
guard error == nil, let data = data, let image = UIImage(data: data) else { return }
ImageCache.attachments.get(attachment.url) { (image) in
DispatchQueue.main.async {
self.image = image
}
}
task!.resume()
}
@objc func imagePressed() {

View File

@ -85,7 +85,6 @@ class ConversationMainStatusTableViewCell: UITableViewCell, PreferencesAdaptive
}
updateTimestamp()
attachmentsView.subviews.forEach { $0.removeFromSuperview() }
let attachments = status.attachments.filter({ $0.kind == .image })
if attachments.count > 0 {
attachmentsView.isHidden = false
@ -155,9 +154,7 @@ class ConversationMainStatusTableViewCell: UITableViewCell, PreferencesAdaptive
}
updateTimestampWorkItem?.cancel()
updateTimestampWorkItem = nil
attachmentsView.subviews.forEach { view in
(view as? AttachmentView)?.task?.cancel()
}
attachmentsView.subviews.forEach { $0.removeFromSuperview() }
}
@objc func accountPressed() {

View File

@ -170,10 +170,7 @@ class StatusTableViewCell: UITableViewCell, PreferencesAdaptive {
}
updateTimestampWorkItem?.cancel()
updateTimestampWorkItem = nil
attachmentsView.subviews.forEach { view in
(view as? AttachmentView)?.task?.cancel()
view.removeFromSuperview()
}
attachmentsView.subviews.forEach { $0.removeFromSuperview() }
}
override func setSelected(_ selected: Bool, animated: Bool) {