async is hard chat

This commit is contained in:
neon443
2025-10-18 16:22:04 +01:00
parent 7c798a8e55
commit 409b09eb3e
2 changed files with 19 additions and 6 deletions

View File

@@ -32,14 +32,17 @@ struct Emoji: EmojiProtocol {
)
}
init(name: String, url: String) {
init(
name: String,
url: String,
image: UIImage = UIImage()
) {
self.name = name
self.urlString = url
grabImage()
self.uiImage = UIImage()
}
enum CodingKeys: CodingKey {
case name
case urlString
}
@@ -50,8 +53,13 @@ struct Emoji: EmojiProtocol {
try container.encode(self.urlString, forKey: .urlString)
}
mutating func grabImage() {
uiImage = UIImage(data: try! Data(contentsOf: url))!
func grabImage() async -> Emoji {
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)!)
}
}

View File

@@ -29,6 +29,11 @@ class EmojiHoarder: ObservableObject {
let data = try! Data(contentsOf: endpoint)
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()
}
}
}
}