-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageLoader.swift
94 lines (78 loc) · 2.48 KB
/
ImageLoader.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//
// ImageLoader.swift
// Politifi
//
// Created by Ethan Hanlon on 3/25/21.
//
import SwiftUI
import Foundation
import Combine
class ImageLoader: ObservableObject {
@Published var image: UIImage?
private var url: URL
private static let imageProcessingQueue = DispatchQueue(label: "image-processing")
private(set) var isLoading = false
private var cancellable: AnyCancellable?
private var cache: ImageCache?
init(url: URL, cache: ImageCache? = nil) {
self.url = url
self.cache = cache
}
deinit {
cancel()
}
func load() {
// Exit early if image loading is in progress
guard !isLoading else { return }
if let image = cache?[url] {
self.image = image
return
}
//
cancellable = URLSession.shared.dataTaskPublisher(for: url)
.subscribe(on: Self.imageProcessingQueue) // Add image to processing queue
.map { UIImage(data: $0.data) } // Map remote image to UIImage
.replaceError(with: nil)
.handleEvents(
receiveSubscription: { [weak self] _ in self?.onStart() },
receiveOutput: { [weak self] in self?.cache($0) }, // Cache image
receiveCompletion: { [weak self] _ in self?.onFinish() },
receiveCancel: { [weak self] in self?.onFinish()}
)
.receive(on: DispatchQueue.main)
.sink { [weak self] in self?.image = $0 }
}
func onStart() {
isLoading = true
}
func onFinish() {
isLoading = false
}
func cache(_ image: UIImage?) {
image.map { cache?[url] = $0 }
}
func cancel() {
cancellable?.cancel()
}
}
// MARK: - NSCache Abstraction
protocol ImageCache {
subscript(_ url: URL) -> UIImage? { get set }
}
struct TemporaryImageCache: ImageCache {
private let cache = NSCache<NSURL, UIImage>()
subscript(_ key: URL) -> UIImage? {
get { cache.object(forKey: key as NSURL) }
set { newValue == nil ? cache.removeObject(forKey: key as NSURL) : cache.setObject(newValue!, forKey: key as NSURL) }
}
}
// MARK: - Add cache to environment
struct ImageCacheKey: EnvironmentKey {
static let defaultValue: ImageCache = TemporaryImageCache()
}
extension EnvironmentValues {
var imageCache: ImageCache {
get { self[ImageCacheKey.self] }
set { self[ImageCacheKey.self] = newValue }
}
}