forked from shadowfacts/Tusker
Cache image attachments
This commit is contained in:
parent
fd611d1f04
commit
82e351465e
|
@ -13,6 +13,7 @@ class ImageCache {
|
||||||
|
|
||||||
static let avatars = ImageCache(name: "Avatars")
|
static let avatars = ImageCache(name: "Avatars")
|
||||||
static let headers = ImageCache(name: "Headers")
|
static let headers = ImageCache(name: "Headers")
|
||||||
|
static let attachments = ImageCache(name: "Attachments", diskExpiry: .seconds(60 * 60), memoryExpiry: .seconds(60))
|
||||||
|
|
||||||
let storage: Storage<UIImage>
|
let storage: Storage<UIImage>
|
||||||
|
|
||||||
|
|
|
@ -19,8 +19,6 @@ class AttachmentView: UIImageView {
|
||||||
|
|
||||||
var attachment: Attachment!
|
var attachment: Attachment!
|
||||||
|
|
||||||
var task: URLSessionDataTask?
|
|
||||||
|
|
||||||
required init?(coder aDecoder: NSCoder) {
|
required init?(coder aDecoder: NSCoder) {
|
||||||
super.init(coder: aDecoder)
|
super.init(coder: aDecoder)
|
||||||
commonInit()
|
commonInit()
|
||||||
|
@ -45,13 +43,11 @@ class AttachmentView: UIImageView {
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadImage() {
|
func loadImage() {
|
||||||
task = URLSession.shared.dataTask(with: attachment.url) { data, response, error in
|
ImageCache.attachments.get(attachment.url) { (image) in
|
||||||
guard error == nil, let data = data, let image = UIImage(data: data) else { return }
|
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
self.image = image
|
self.image = image
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
task!.resume()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@objc func imagePressed() {
|
@objc func imagePressed() {
|
||||||
|
|
|
@ -85,7 +85,6 @@ class ConversationMainStatusTableViewCell: UITableViewCell, PreferencesAdaptive
|
||||||
}
|
}
|
||||||
updateTimestamp()
|
updateTimestamp()
|
||||||
|
|
||||||
attachmentsView.subviews.forEach { $0.removeFromSuperview() }
|
|
||||||
let attachments = status.attachments.filter({ $0.kind == .image })
|
let attachments = status.attachments.filter({ $0.kind == .image })
|
||||||
if attachments.count > 0 {
|
if attachments.count > 0 {
|
||||||
attachmentsView.isHidden = false
|
attachmentsView.isHidden = false
|
||||||
|
@ -155,9 +154,7 @@ class ConversationMainStatusTableViewCell: UITableViewCell, PreferencesAdaptive
|
||||||
}
|
}
|
||||||
updateTimestampWorkItem?.cancel()
|
updateTimestampWorkItem?.cancel()
|
||||||
updateTimestampWorkItem = nil
|
updateTimestampWorkItem = nil
|
||||||
attachmentsView.subviews.forEach { view in
|
attachmentsView.subviews.forEach { $0.removeFromSuperview() }
|
||||||
(view as? AttachmentView)?.task?.cancel()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@objc func accountPressed() {
|
@objc func accountPressed() {
|
||||||
|
|
|
@ -170,10 +170,7 @@ class StatusTableViewCell: UITableViewCell, PreferencesAdaptive {
|
||||||
}
|
}
|
||||||
updateTimestampWorkItem?.cancel()
|
updateTimestampWorkItem?.cancel()
|
||||||
updateTimestampWorkItem = nil
|
updateTimestampWorkItem = nil
|
||||||
attachmentsView.subviews.forEach { view in
|
attachmentsView.subviews.forEach { $0.removeFromSuperview() }
|
||||||
(view as? AttachmentView)?.task?.cancel()
|
|
||||||
view.removeFromSuperview()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override func setSelected(_ selected: Bool, animated: Bool) {
|
override func setSelected(_ selected: Bool, animated: Bool) {
|
||||||
|
|
Loading…
Reference in New Issue