Tusker/Tusker/Views/CachedImageView.swift

92 lines
2.7 KiB
Swift
Raw Normal View History

2022-10-05 21:40:00 +00:00
//
// CachedImageView.swift
// Tusker
//
// Created by Shadowfacts on 10/4/22.
// Copyright © 2022 Shadowfacts. All rights reserved.
//
import UIKit
class CachedImageView: UIImageView {
var cache: ImageCache!
2022-10-05 21:40:00 +00:00
private var url: URL?
private var isGrayscale = false
private var fetchTask: Task<Void, Error>?
private var blurHashTask: DispatchWorkItem?
2022-10-05 21:40:00 +00:00
init(cache: ImageCache) {
self.cache = cache
super.init(frame: .zero)
commonInit()
2022-10-05 21:40:00 +00:00
}
required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
2022-10-05 21:40:00 +00:00
}
private func commonInit() {
NotificationCenter.default.addObserver(self, selector: #selector(preferencesChanged), name: .preferencesChanged, object: nil)
}
func update(for url: URL?, blurhash: String? = nil) {
if url != self.url || (url != nil && self.image == nil) {
2022-10-05 21:40:00 +00:00
self.url = url
self.image = nil
updateBlurhash(blurhash, for: url)
2022-10-05 21:40:00 +00:00
updateImage()
}
}
@objc private func preferencesChanged() {
if isGrayscale != Preferences.shared.grayscaleImages {
updateImage()
}
}
private func updateBlurhash(_ blurhash: String?, for url: URL?) {
blurHashTask?.cancel()
if let blurhash {
let aspectRatio = self.bounds.width > 0 ? self.bounds.height / self.bounds.width : 1
blurHashTask = DispatchWorkItem {
let size = CGSize(width: 60, height: aspectRatio * 60)
let image = UIImage(blurHash: blurhash, size: size)
DispatchQueue.main.async {
if self.image == nil && self.url == url && self.blurHashTask?.isCancelled == false {
self.image = image
}
}
}
AttachmentView.queue.async(execute: blurHashTask!)
}
}
2022-10-05 21:40:00 +00:00
private func updateImage() {
fetchTask?.cancel()
guard let url else {
return
}
2022-10-05 21:40:00 +00:00
fetchTask = Task(priority: .high) {
let (_, image) = await cache.get(url)
guard let image else {
return
}
try Task.checkCancellation()
// TODO: check that this isn't on the main thread
guard let transformedImage = ImageGrayscalifier.convertIfNecessary(url: url, image: image) else {
return
}
try Task.checkCancellation()
2022-10-05 21:40:00 +00:00
self.image = transformedImage
self.isGrayscale = Preferences.shared.grayscaleImages
self.blurHashTask?.cancel()
2022-10-05 21:40:00 +00:00
}
}
}