1 |
efrain |
1 |
//
|
|
|
2 |
// ImageLoader.swift
|
|
|
3 |
// twogetskills
|
|
|
4 |
//
|
|
|
5 |
// Created by Efrain Yanez Recanatini on 3/9/22.
|
|
|
6 |
//
|
|
|
7 |
|
|
|
8 |
import Combine
|
|
|
9 |
import UIKit
|
|
|
10 |
|
|
|
11 |
class ImageLoader: ObservableObject {
|
|
|
12 |
@Published var image: UIImage?
|
|
|
13 |
|
|
|
14 |
private(set) var isLoading = false
|
|
|
15 |
|
|
|
16 |
private let url: URL
|
|
|
17 |
private var imageCache: ImageCache?
|
|
|
18 |
//private var imageCache: ImageCache
|
|
|
19 |
private var appData = AppData.sharedInstance
|
|
|
20 |
private let key : String
|
|
|
21 |
|
|
|
22 |
/*
|
|
|
23 |
init(url: URL) {
|
|
|
24 |
self.url = url
|
|
|
25 |
self.imageCache = ImageCache.sharedInstance
|
|
|
26 |
|
|
|
27 |
let urlString : String = url.absoluteString
|
|
|
28 |
let arrayFullFilename = urlString.split(separator: "/")
|
|
|
29 |
self.key = String(arrayFullFilename[arrayFullFilename.count - 1])
|
|
|
30 |
|
|
|
31 |
}*/
|
|
|
32 |
|
|
|
33 |
|
|
|
34 |
init(url: URL, cache: ImageCache? = nil) {
|
|
|
35 |
self.url = url
|
|
|
36 |
self.imageCache = cache
|
|
|
37 |
|
|
|
38 |
let urlString : String = url.absoluteString
|
|
|
39 |
let arrayFullFilename = urlString.split(separator: "/")
|
|
|
40 |
self.key = String(arrayFullFilename[arrayFullFilename.count - 1])
|
|
|
41 |
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
|
|
|
45 |
func load() {
|
|
|
46 |
|
|
|
47 |
if imageCache!.cache.hasData(forKey: key) {
|
|
|
48 |
print("ImageLoader from cache key : \(key)")
|
|
|
49 |
self.image = imageCache!.cache.readImageForKey(key: key)
|
|
|
50 |
return
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
/*
|
|
|
54 |
if let image = cache?[key as NSString] {
|
|
|
55 |
print("ImageLoader from cache key: \(key)")
|
|
|
56 |
self.image = image
|
|
|
57 |
return
|
|
|
58 |
}*/
|
|
|
59 |
|
|
|
60 |
print("ImageLoader from url key: \(key)")
|
|
|
61 |
|
|
|
62 |
var request = URLRequest(url: url)
|
|
|
63 |
|
|
|
64 |
let headerSecurity = HeaderSecurity()
|
|
|
65 |
|
|
|
66 |
request.httpMethod = "GET"
|
|
|
67 |
request.addValue(appData.deviceUuid, forHTTPHeaderField: Constants.HTTP_HEADER_SECURITY_TOKEN)
|
|
|
68 |
request.addValue(headerSecurity.secret, forHTTPHeaderField: Constants.HTTP_HEADER_SECURITY_SECRET)
|
|
|
69 |
request.addValue(String(headerSecurity.created), forHTTPHeaderField: Constants.HTTP_HEADER_SECURITY_CREATED)
|
|
|
70 |
request.addValue(String(headerSecurity.rand), forHTTPHeaderField: Constants.HTTP_HEADER_SECURITY_RAND)
|
|
|
71 |
|
|
|
72 |
|
|
|
73 |
|
|
|
74 |
let task = URLSession.shared.dataTask(with: request, completionHandler: setImageFromData)
|
|
|
75 |
task.resume()
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
private func setImageFromData(data: Data?, urlResponse: URLResponse?, error: Error?) {
|
|
|
79 |
guard error == nil else {
|
|
|
80 |
// print("\(error!)")
|
|
|
81 |
return
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
guard let content = data else {
|
|
|
85 |
// print("No data")
|
|
|
86 |
return
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
DispatchQueue.main.async {
|
|
|
90 |
self.image = UIImage(data: content)
|
|
|
91 |
// self.dataHasLoaded = true
|
|
|
92 |
|
|
|
93 |
print("ImageLoader save to cache key: \(self.key)")
|
|
|
94 |
self.imageCache!.cache.write(image: self.image!, forKey: self.key)
|
|
|
95 |
|
|
|
96 |
//self.cache?[self.key as NSString] = self.image
|
|
|
97 |
}
|
|
|
98 |
}
|
|
|
99 |
}
|
|
|
100 |
|