Cache profile header images

This commit is contained in:
Shadowfacts 2018-10-02 13:45:39 -04:00
parent 982e42ca2f
commit fd611d1f04
Signed by untrusted user: shadowfacts
GPG Key ID: 94A5AB95422746E5
2 changed files with 7 additions and 6 deletions

View File

@ -12,6 +12,7 @@ import Cache
class ImageCache { class ImageCache {
static let avatars = ImageCache(name: "Avatars") static let avatars = ImageCache(name: "Avatars")
static let headers = ImageCache(name: "Headers")
let storage: Storage<UIImage> let storage: Storage<UIImage>

View File

@ -28,8 +28,7 @@ class ProfileHeaderTableViewCell: UITableViewCell, PreferencesAdaptive {
var accountID: String! var accountID: String!
var avatarURL: URL? var avatarURL: URL?
var headerURL: URL?
var headerImageDownloadTask: URLSessionDataTask?
override func awakeFromNib() { override func awakeFromNib() {
avatarContainerView.layer.masksToBounds = true avatarContainerView.layer.masksToBounds = true
@ -65,14 +64,12 @@ class ProfileHeaderTableViewCell: UITableViewCell, PreferencesAdaptive {
self.avatarURL = nil self.avatarURL = nil
} }
} }
headerImageDownloadTask = URLSession.shared.dataTask(with: account.header) { data, response, error in ImageCache.headers.get(account.header) { (image) in
guard error == nil, let data = data, let image = UIImage(data: data) else { return }
DispatchQueue.main.async { DispatchQueue.main.async {
self.headerImageView.image = image self.headerImageView.image = image
self.headerImageDownloadTask = nil self.headerURL = nil
} }
} }
headerImageDownloadTask!.resume()
noteLabel.setTextFromHtml(account.note) noteLabel.setTextFromHtml(account.note)
@ -91,6 +88,9 @@ class ProfileHeaderTableViewCell: UITableViewCell, PreferencesAdaptive {
if let url = avatarURL { if let url = avatarURL {
ImageCache.avatars.cancel(url) ImageCache.avatars.cancel(url)
} }
if let url = headerURL {
ImageCache.headers.cancel(url)
}
} }
@IBAction func morePressed(_ sender: Any) { @IBAction func morePressed(_ sender: Any) {