Cache profile header images

This commit is contained in:
Shadowfacts 2018-10-02 13:45:39 -04:00
parent ba0ca042aa
commit 3715701935
Signed by: shadowfacts
GPG Key ID: 83FB3304046BADA4
2 changed files with 7 additions and 6 deletions

View File

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

View File

@ -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) {