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 {
|
|
|
|
|
2023-01-22 23:07:16 +00:00
|
|
|
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>?
|
2023-01-22 23:07:16 +00:00
|
|
|
private var blurHashTask: DispatchWorkItem?
|
2022-10-05 21:40:00 +00:00
|
|
|
|
|
|
|
init(cache: ImageCache) {
|
|
|
|
self.cache = cache
|
|
|
|
super.init(frame: .zero)
|
2023-01-22 23:07:16 +00:00
|
|
|
commonInit()
|
2022-10-05 21:40:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
2023-01-22 23:07:16 +00:00
|
|
|
super.init(coder: coder)
|
|
|
|
commonInit()
|
2022-10-05 21:40:00 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 23:07:16 +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
|
2023-01-22 23:07:16 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-22 23:07:16 +00:00
|
|
|
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()
|
2023-01-22 23:07:16 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2022-11-22 20:21:36 +00:00
|
|
|
try Task.checkCancellation()
|
2022-10-05 21:40:00 +00:00
|
|
|
self.image = transformedImage
|
2023-01-18 16:37:15 +00:00
|
|
|
self.isGrayscale = Preferences.shared.grayscaleImages
|
2023-01-22 23:07:16 +00:00
|
|
|
self.blurHashTask?.cancel()
|
2022-10-05 21:40:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|