mirror of
https://github.com/neon443/StickerSlack.git
synced 2026-03-11 05:19:13 +00:00
async is hard chat
This commit is contained in:
@@ -32,14 +32,17 @@ struct Emoji: EmojiProtocol {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
init(name: String, url: String) {
|
init(
|
||||||
|
name: String,
|
||||||
|
url: String,
|
||||||
|
image: UIImage = UIImage()
|
||||||
|
) {
|
||||||
self.name = name
|
self.name = name
|
||||||
self.urlString = url
|
self.urlString = url
|
||||||
grabImage()
|
self.uiImage = UIImage()
|
||||||
}
|
}
|
||||||
|
|
||||||
enum CodingKeys: CodingKey {
|
enum CodingKeys: CodingKey {
|
||||||
|
|
||||||
case name
|
case name
|
||||||
case urlString
|
case urlString
|
||||||
}
|
}
|
||||||
@@ -50,8 +53,13 @@ struct Emoji: EmojiProtocol {
|
|||||||
try container.encode(self.urlString, forKey: .urlString)
|
try container.encode(self.urlString, forKey: .urlString)
|
||||||
}
|
}
|
||||||
|
|
||||||
mutating func grabImage() {
|
func grabImage() async -> Emoji {
|
||||||
uiImage = UIImage(data: try! Data(contentsOf: url))!
|
let req = URLRequest(url: url, cachePolicy: .reloadRevalidatingCacheData, timeoutInterval: 10)
|
||||||
|
guard let response = try? await URLSession.shared.data(for: req) else {
|
||||||
|
return self
|
||||||
|
}
|
||||||
|
print(UIImage(data: response.0))
|
||||||
|
return Emoji(name: name, url: urlString, image: UIImage(data: response.0)!)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -29,6 +29,11 @@ class EmojiHoarder: ObservableObject {
|
|||||||
|
|
||||||
let data = try! Data(contentsOf: endpoint)
|
let data = try! Data(contentsOf: endpoint)
|
||||||
let decoded: [SlackResponse] = try! JSONDecoder().decode([SlackResponse].self, from: data)
|
let decoded: [SlackResponse] = try! JSONDecoder().decode([SlackResponse].self, from: data)
|
||||||
emojis = decoded.prefix(10).map { Emoji(name: $0.name, url: $0.imageUrl) }
|
emojis = decoded.prefix(100).map { Emoji(name: $0.name, url: $0.imageUrl) }
|
||||||
|
Task {
|
||||||
|
for i in 0..<emojis.count {
|
||||||
|
await emojis[i] = emojis[i].grabImage()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user