2018-10-02 17:42:37 +00:00
|
|
|
//
|
|
|
|
// ImageCache.swift
|
|
|
|
// Tusker
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 8/21/18.
|
|
|
|
// Copyright © 2018 Shadowfacts. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
|
|
|
|
class ImageCache {
|
|
|
|
|
2022-02-06 19:36:01 +00:00
|
|
|
static let avatars = ImageCache(name: "Avatars", memoryExpiry: .seconds(60 * 60), diskExpiry: .seconds(60 * 60 * 24 * 7), desiredSize: CGSize(width: 50, height: 50))
|
|
|
|
static let headers = ImageCache(name: "Headers", memoryExpiry: .seconds(60 * 5), diskExpiry: .seconds(60 * 60 * 24 * 7))
|
2018-11-08 00:24:52 +00:00
|
|
|
static let attachments = ImageCache(name: "Attachments", memoryExpiry: .seconds(60 * 2))
|
2022-02-06 19:36:01 +00:00
|
|
|
static let emojis = ImageCache(name: "Emojis", memoryExpiry: .seconds(60 * 5), diskExpiry: .seconds(60 * 60 * 24 * 7))
|
2020-09-21 22:03:51 +00:00
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
private static let disableCaching = ProcessInfo.processInfo.environment.keys.contains("DISABLE_IMAGE_CACHE")
|
|
|
|
#else
|
|
|
|
private static let disableCaching = false
|
|
|
|
#endif
|
2018-10-02 17:42:37 +00:00
|
|
|
|
2021-01-16 20:24:15 +00:00
|
|
|
private let cache: ImageDataCache
|
2022-02-06 15:24:07 +00:00
|
|
|
private let desiredPixelSize: CGSize?
|
2018-10-02 17:42:37 +00:00
|
|
|
|
2022-09-12 02:09:04 +00:00
|
|
|
private var groups = MultiThreadDictionary<URL, RequestGroup>()
|
2018-10-02 17:42:37 +00:00
|
|
|
|
2020-10-18 15:11:47 +00:00
|
|
|
private var backgroundQueue = DispatchQueue(label: "ImageCache completion queue", qos: .default)
|
|
|
|
|
2021-01-18 23:46:59 +00:00
|
|
|
init(name: String, memoryExpiry: CacheExpiry, diskExpiry: CacheExpiry? = nil, desiredSize: CGSize? = nil) {
|
2021-01-17 18:27:30 +00:00
|
|
|
// todo: might not always want to use UIScreen.main for this, e.g. Catalyst?
|
|
|
|
let pixelSize = desiredSize?.applying(.init(scaleX: UIScreen.main.scale, y: UIScreen.main.scale))
|
2022-02-06 15:24:07 +00:00
|
|
|
self.desiredPixelSize = pixelSize
|
2021-01-17 18:27:30 +00:00
|
|
|
self.cache = ImageDataCache(name: name, memoryExpiry: memoryExpiry, diskExpiry: diskExpiry, storeOriginalDataInMemory: diskExpiry == nil, desiredPixelSize: pixelSize)
|
2018-11-08 00:24:52 +00:00
|
|
|
}
|
|
|
|
|
2021-01-20 23:31:30 +00:00
|
|
|
func get(_ url: URL, loadOriginal: Bool = false, completion: ((Data?, UIImage?) -> Void)?) -> Request? {
|
2018-10-02 17:42:37 +00:00
|
|
|
let key = url.absoluteString
|
2022-02-06 15:24:07 +00:00
|
|
|
|
|
|
|
let wrappedCompletion: ((Data?, UIImage?) -> Void)?
|
|
|
|
if let completion = completion {
|
|
|
|
wrappedCompletion = { (data, image) in
|
2022-07-01 01:41:05 +00:00
|
|
|
if !loadOriginal,
|
|
|
|
let size = self.desiredPixelSize {
|
|
|
|
image?.prepareThumbnail(of: size, completionHandler: {
|
|
|
|
completion(data, $0)
|
|
|
|
})
|
2022-02-06 15:24:07 +00:00
|
|
|
} else {
|
2022-07-01 01:41:05 +00:00
|
|
|
image?.prepareForDisplay {
|
|
|
|
completion(data, $0)
|
2022-02-06 15:24:07 +00:00
|
|
|
}
|
2021-01-18 18:46:07 +00:00
|
|
|
}
|
2020-10-18 15:11:47 +00:00
|
|
|
}
|
2022-02-06 15:24:07 +00:00
|
|
|
} else {
|
|
|
|
wrappedCompletion = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ImageCache.disableCaching,
|
|
|
|
let entry = try? cache.get(key, loadOriginal: loadOriginal) {
|
|
|
|
wrappedCompletion?(entry.data, entry.image)
|
2020-01-25 15:06:27 +00:00
|
|
|
return nil
|
2018-10-02 17:42:37 +00:00
|
|
|
} else {
|
2021-01-18 18:46:07 +00:00
|
|
|
if let group = groups[url] {
|
2022-02-06 15:24:07 +00:00
|
|
|
return group.addCallback(wrappedCompletion)
|
2018-10-02 17:42:37 +00:00
|
|
|
} else {
|
2021-01-18 19:50:56 +00:00
|
|
|
let group = createGroup(url: url)
|
2022-02-06 15:24:07 +00:00
|
|
|
let request = group.addCallback(wrappedCompletion)
|
2020-06-16 02:34:42 +00:00
|
|
|
group.run()
|
2020-01-25 15:06:27 +00:00
|
|
|
return request
|
2018-10-02 17:42:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-16 16:31:42 +00:00
|
|
|
func get(_ url: URL, loadOriginal: Bool = false) async -> (Data?, UIImage?) {
|
|
|
|
return await withCheckedContinuation { continuation in
|
|
|
|
_ = get(url, loadOriginal: loadOriginal) { data, image in
|
|
|
|
continuation.resume(returning: (data, image))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-18 19:50:56 +00:00
|
|
|
func fetchIfNotCached(_ url: URL) {
|
|
|
|
// if caching is disabled, don't bother fetching since nothing will be done with the result
|
|
|
|
guard !ImageCache.disableCaching else { return }
|
|
|
|
|
|
|
|
if !((try? cache.has(url.absoluteString)) ?? false),
|
|
|
|
!groups.contains(key: url) {
|
|
|
|
let group = createGroup(url: url)
|
|
|
|
group.run()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func createGroup(url: URL) -> RequestGroup {
|
|
|
|
let group = RequestGroup(url: url) { (data, image) in
|
|
|
|
if let data = data {
|
2021-01-20 23:31:30 +00:00
|
|
|
try? self.cache.set(url.absoluteString, data: data, image: image)
|
2021-01-18 19:50:56 +00:00
|
|
|
}
|
2022-09-12 02:09:04 +00:00
|
|
|
_ = self.groups.removeValue(forKey: url)
|
2021-01-18 19:50:56 +00:00
|
|
|
}
|
|
|
|
groups[url] = group
|
|
|
|
return group
|
|
|
|
}
|
|
|
|
|
2021-01-16 20:24:15 +00:00
|
|
|
func getData(_ url: URL) -> Data? {
|
|
|
|
return try? cache.getData(url.absoluteString)
|
|
|
|
}
|
|
|
|
|
2021-01-20 23:31:30 +00:00
|
|
|
func get(_ url: URL, loadOriginal: Bool = false) -> ImageDataCache.Entry? {
|
|
|
|
return try? cache.get(url.absoluteString, loadOriginal: loadOriginal)
|
2018-10-12 01:20:58 +00:00
|
|
|
}
|
2020-01-25 15:06:27 +00:00
|
|
|
|
|
|
|
func cancelWithoutCallback(_ url: URL) {
|
2020-06-16 02:34:42 +00:00
|
|
|
groups[url]?.cancelWithoutCallback()
|
2018-10-02 17:42:37 +00:00
|
|
|
}
|
|
|
|
|
2020-09-12 14:49:08 +00:00
|
|
|
func reset() throws {
|
|
|
|
try cache.removeAll()
|
|
|
|
}
|
|
|
|
|
2020-06-16 02:34:42 +00:00
|
|
|
private class RequestGroup {
|
2018-10-02 17:42:37 +00:00
|
|
|
let url: URL
|
2021-01-16 20:24:15 +00:00
|
|
|
private let onFinished: (Data?, UIImage?) -> Void
|
2020-01-25 15:06:27 +00:00
|
|
|
private var task: URLSessionDataTask?
|
|
|
|
private var requests = [Request]()
|
|
|
|
|
2021-01-16 20:24:15 +00:00
|
|
|
init(url: URL, onFinished: @escaping (Data?, UIImage?) -> Void) {
|
2018-10-02 17:42:37 +00:00
|
|
|
self.url = url
|
2020-06-16 02:34:42 +00:00
|
|
|
self.onFinished = onFinished
|
2018-10-02 17:42:37 +00:00
|
|
|
}
|
2020-01-25 15:06:27 +00:00
|
|
|
|
|
|
|
deinit {
|
|
|
|
task?.cancel()
|
|
|
|
}
|
2018-10-02 17:42:37 +00:00
|
|
|
|
2020-06-16 02:34:42 +00:00
|
|
|
func run() {
|
2018-10-02 17:42:37 +00:00
|
|
|
task = URLSession.shared.dataTask(with: url, completionHandler: { (data, response, error) in
|
2018-11-09 20:48:08 +00:00
|
|
|
guard error == nil, let data = data else {
|
2018-10-02 17:42:37 +00:00
|
|
|
self.complete(with: nil)
|
|
|
|
return
|
|
|
|
}
|
2018-11-09 20:48:08 +00:00
|
|
|
self.complete(with: data)
|
2018-10-02 17:42:37 +00:00
|
|
|
})
|
|
|
|
task!.resume()
|
|
|
|
}
|
|
|
|
|
2021-01-16 20:24:15 +00:00
|
|
|
func addCallback(_ completion: ((Data?, UIImage?) -> Void)?) -> Request {
|
2020-01-25 15:06:27 +00:00
|
|
|
let request = Request(callback: completion)
|
|
|
|
requests.append(request)
|
|
|
|
return request
|
|
|
|
}
|
|
|
|
|
|
|
|
func cancelWithoutCallback() {
|
|
|
|
if let request = requests.first(where: { $0.callback == nil && !$0.cancelled }) {
|
|
|
|
request.cancel()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fileprivate func requestCancelled() {
|
|
|
|
let remaining = requests.filter { !$0.cancelled }.count
|
|
|
|
if remaining <= 0 {
|
|
|
|
task?.cancel()
|
|
|
|
complete(with: nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-09 20:48:08 +00:00
|
|
|
func complete(with data: Data?) {
|
2021-01-16 20:24:15 +00:00
|
|
|
let image = data != nil ? UIImage(data: data!) : nil
|
|
|
|
|
2020-01-25 15:06:27 +00:00
|
|
|
requests.filter { !$0.cancelled }.forEach {
|
|
|
|
if let callback = $0.callback {
|
2021-01-16 20:24:15 +00:00
|
|
|
callback(data, image)
|
2020-01-25 15:06:27 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-16 20:24:15 +00:00
|
|
|
|
|
|
|
self.onFinished(data, image)
|
2020-01-25 15:06:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Request {
|
2020-06-16 02:34:42 +00:00
|
|
|
private weak var group: RequestGroup?
|
2021-01-16 20:24:15 +00:00
|
|
|
private(set) var callback: ((Data?, UIImage?) -> Void)?
|
2020-01-25 15:06:27 +00:00
|
|
|
private(set) var cancelled: Bool = false
|
|
|
|
|
2021-01-16 20:24:15 +00:00
|
|
|
init(callback: ((Data?, UIImage?) -> Void)?) {
|
2020-01-25 15:06:27 +00:00
|
|
|
self.callback = callback
|
|
|
|
}
|
|
|
|
|
|
|
|
func cancel() {
|
|
|
|
cancelled = true
|
|
|
|
callback = nil
|
|
|
|
group?.requestCancelled()
|
2018-10-02 17:42:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|