From fd611d1f0412cc2663eb15919b790052457de4cf Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Tue, 2 Oct 2018 13:45:39 -0400 Subject: [PATCH] Cache profile header images --- Tusker/ImageCache.swift | 1 + .../Profile Header/ProfileHeaderTableViewCell.swift | 12 ++++++------ 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/Tusker/ImageCache.swift b/Tusker/ImageCache.swift index fecb6ea5e5..5248d4af09 100644 --- a/Tusker/ImageCache.swift +++ b/Tusker/ImageCache.swift @@ -12,6 +12,7 @@ import Cache class ImageCache { static let avatars = ImageCache(name: "Avatars") + static let headers = ImageCache(name: "Headers") let storage: Storage diff --git a/Tusker/Views/Profile Header/ProfileHeaderTableViewCell.swift b/Tusker/Views/Profile Header/ProfileHeaderTableViewCell.swift index a59c2c72de..aa77163034 100644 --- a/Tusker/Views/Profile Header/ProfileHeaderTableViewCell.swift +++ b/Tusker/Views/Profile Header/ProfileHeaderTableViewCell.swift @@ -28,8 +28,7 @@ class ProfileHeaderTableViewCell: UITableViewCell, PreferencesAdaptive { var accountID: String! var avatarURL: URL? - - var headerImageDownloadTask: URLSessionDataTask? + var headerURL: URL? override func awakeFromNib() { avatarContainerView.layer.masksToBounds = true @@ -65,14 +64,12 @@ class ProfileHeaderTableViewCell: UITableViewCell, PreferencesAdaptive { self.avatarURL = nil } } - headerImageDownloadTask = URLSession.shared.dataTask(with: account.header) { data, response, error in - guard error == nil, let data = data, let image = UIImage(data: data) else { return } + ImageCache.headers.get(account.header) { (image) in DispatchQueue.main.async { self.headerImageView.image = image - self.headerImageDownloadTask = nil + self.headerURL = nil } } - headerImageDownloadTask!.resume() noteLabel.setTextFromHtml(account.note) @@ -91,6 +88,9 @@ class ProfileHeaderTableViewCell: UITableViewCell, PreferencesAdaptive { if let url = avatarURL { ImageCache.avatars.cancel(url) } + if let url = headerURL { + ImageCache.headers.cancel(url) + } } @IBAction func morePressed(_ sender: Any) {