From 6f31c3a8d56578a046a368e5de841ea729dfe120 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Wed, 3 Oct 2018 08:53:05 -0400 Subject: [PATCH] Cache image attachments --- Tusker/ImageCache.swift | 1 + Tusker/Views/AttachmentView.swift | 6 +----- .../Views/Status/ConversationMainStatusTableViewCell.swift | 5 +---- Tusker/Views/Status/StatusTableViewCell.swift | 5 +---- 4 files changed, 4 insertions(+), 13 deletions(-) diff --git a/Tusker/ImageCache.swift b/Tusker/ImageCache.swift index 5248d4af..fd8a386b 100644 --- a/Tusker/ImageCache.swift +++ b/Tusker/ImageCache.swift @@ -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 diff --git a/Tusker/Views/AttachmentView.swift b/Tusker/Views/AttachmentView.swift index ab3abaf5..d85aa832 100644 --- a/Tusker/Views/AttachmentView.swift +++ b/Tusker/Views/AttachmentView.swift @@ -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() { diff --git a/Tusker/Views/Status/ConversationMainStatusTableViewCell.swift b/Tusker/Views/Status/ConversationMainStatusTableViewCell.swift index 7f07666a..fe4f2ad7 100644 --- a/Tusker/Views/Status/ConversationMainStatusTableViewCell.swift +++ b/Tusker/Views/Status/ConversationMainStatusTableViewCell.swift @@ -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() { diff --git a/Tusker/Views/Status/StatusTableViewCell.swift b/Tusker/Views/Status/StatusTableViewCell.swift index db5b5efe..54912a2f 100644 --- a/Tusker/Views/Status/StatusTableViewCell.swift +++ b/Tusker/Views/Status/StatusTableViewCell.swift @@ -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) {