mirror of
https://github.com/neon443/StickerSlack.git
synced 2026-03-11 05:19:13 +00:00
okay!
this is not good...
This commit is contained in:
@@ -71,13 +71,14 @@ struct Emoji: Codable, Identifiable, Hashable {
|
||||
self.remoteImageURL = url
|
||||
}
|
||||
|
||||
nonisolated
|
||||
func downloadImage() async throws -> UIImage {
|
||||
if let data = try? Data(contentsOf: localImageURL),
|
||||
if let data = try? await Data(contentsOf: localImageURL),
|
||||
let uiimage = UIImage(data: data) {
|
||||
return uiimage
|
||||
}
|
||||
let (data, _) = try await URLSession.shared.data(from: remoteImageURL)
|
||||
try! data.write(to: localImageURL)
|
||||
try! await data.write(to: localImageURL)
|
||||
return UIImage(data: data)!
|
||||
}
|
||||
|
||||
|
||||
@@ -145,10 +145,24 @@ class EmojiHoarder: ObservableObject {
|
||||
}
|
||||
|
||||
func downloadEmoji(_ toDownload: Emoji) {
|
||||
|
||||
Task.detached {
|
||||
if (try? await toDownload.downloadImage()) != nil {
|
||||
let index = await self.emojis.firstIndex { $0 == toDownload }
|
||||
guard let index else { return }
|
||||
await MainActor.run {
|
||||
self.localEmojis.insert(toDownload)
|
||||
self.emojis[index].refresh()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
func deleteEmoji(_ toDelete: Emoji) {
|
||||
|
||||
toDelete.deleteImage()
|
||||
let index = self.emojis.firstIndex { $0 == toDelete }
|
||||
guard let index else { return }
|
||||
self.localEmojis.remove(toDelete)
|
||||
self.emojis[index].refresh()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,16 +66,12 @@ struct ContentView: View {
|
||||
.buttonStyle(.plain)
|
||||
if hoarder.localEmojis.contains(emoji) {
|
||||
Button("", systemImage: "trash") {
|
||||
emoji.deleteImage()
|
||||
emoji.refresh()
|
||||
hoarder.deleteEmoji(emoji)
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
} else {
|
||||
Button("", systemImage: "arrow.down.circle") {
|
||||
Task {
|
||||
let _ = try? await emoji.downloadImage()
|
||||
emoji.refresh()
|
||||
}
|
||||
hoarder.downloadEmoji(emoji)
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
}
|
||||
@@ -83,8 +79,7 @@ struct ContentView: View {
|
||||
.swipeActions(edge: .trailing, allowsFullSwipe: true) {
|
||||
if hoarder.localEmojis.contains(emoji) {
|
||||
Button("Remove", systemImage: "trash") {
|
||||
emoji.deleteImage()
|
||||
emoji.refresh()
|
||||
hoarder.deleteEmoji(emoji)
|
||||
}
|
||||
.tint(.red)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user