mirror of
https://github.com/neon443/StickerSlack.git
synced 2026-03-11 05:19:13 +00:00
currently on an optimisation spree
remove the islocal thing i added, got a better idea
using a Set of local emojis
This commit is contained in:
@@ -26,13 +26,12 @@ struct Emoji: Codable, Identifiable, Hashable {
|
|||||||
}
|
}
|
||||||
var remoteImageURL: URL
|
var remoteImageURL: URL
|
||||||
|
|
||||||
var isLocal: Bool = false
|
var isLocal: Bool {
|
||||||
var isLocalold: Bool {
|
|
||||||
return (try? Data(contentsOf: localImageURL)) != nil
|
return (try? Data(contentsOf: localImageURL)) != nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var sticker: MSSticker? {
|
var sticker: MSSticker? {
|
||||||
guard isLocalold else {
|
guard isLocal else {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return try? MSSticker(contentsOfFileURL: localImageURL, localizedDescription: name)
|
return try? MSSticker(contentsOfFileURL: localImageURL, localizedDescription: name)
|
||||||
@@ -59,7 +58,6 @@ struct Emoji: Codable, Identifiable, Hashable {
|
|||||||
self.uiID = id
|
self.uiID = id
|
||||||
self.name = try container.decode(String.self, forKey: .name)
|
self.name = try container.decode(String.self, forKey: .name)
|
||||||
self.remoteImageURL = try container.decode(URL.self, forKey: .remoteImageURL)
|
self.remoteImageURL = try container.decode(URL.self, forKey: .remoteImageURL)
|
||||||
self.isLocal = FileManager.default.fileExists(atPath: localImageURLString)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
init(
|
init(
|
||||||
@@ -70,7 +68,6 @@ struct Emoji: Codable, Identifiable, Hashable {
|
|||||||
self.uiID = id
|
self.uiID = id
|
||||||
self.name = apiEmoji.name
|
self.name = apiEmoji.name
|
||||||
self.remoteImageURL = apiEmoji.url
|
self.remoteImageURL = apiEmoji.url
|
||||||
self.isLocal = FileManager.default.fileExists(atPath: localImageURLString)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func downloadImage() async throws -> UIImage {
|
func downloadImage() async throws -> UIImage {
|
||||||
@@ -85,6 +82,7 @@ struct Emoji: Codable, Identifiable, Hashable {
|
|||||||
|
|
||||||
func deleteImage() {
|
func deleteImage() {
|
||||||
try? FileManager.default.removeItem(at: localImageURL)
|
try? FileManager.default.removeItem(at: localImageURL)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@MainActor
|
@MainActor
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ class EmojiHoarder: ObservableObject {
|
|||||||
private let decoder = JSONDecoder()
|
private let decoder = JSONDecoder()
|
||||||
|
|
||||||
@Published var emojis: [Emoji] = []
|
@Published var emojis: [Emoji] = []
|
||||||
|
@Published var localEmojis: Set<Emoji> = []
|
||||||
@Published var filteredEmojis: [Emoji] = []
|
@Published var filteredEmojis: [Emoji] = []
|
||||||
@Published var prefix: Int = 100
|
@Published var prefix: Int = 100
|
||||||
|
|
||||||
@@ -29,6 +30,7 @@ class EmojiHoarder: ObservableObject {
|
|||||||
guard !localOnly else { return }
|
guard !localOnly else { return }
|
||||||
Task.detached {
|
Task.detached {
|
||||||
await self.loadRemoteDB()
|
await self.loadRemoteDB()
|
||||||
|
await self.loadLocalEmojis()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -52,6 +54,11 @@ class EmojiHoarder: ObservableObject {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func loadLocalEmojis() async {
|
||||||
|
self.localEmojis = Set(self.emojis.filter { $0.isLocal })
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func storeDB() {
|
func storeDB() {
|
||||||
try! encoder.encode(emojis).write(to: EmojiHoarder.localEmojiDB)
|
try! encoder.encode(emojis).write(to: EmojiHoarder.localEmojiDB)
|
||||||
}
|
}
|
||||||
@@ -76,13 +83,14 @@ class EmojiHoarder: ObservableObject {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@concurrent
|
||||||
func fetchRemoteDB() async -> [Emoji]? {
|
func fetchRemoteDB() async -> [Emoji]? {
|
||||||
do {
|
do {
|
||||||
async let (data, _) = try URLSession.shared.data(from: endpoint)
|
async let (data, _) = try URLSession.shared.data(from: endpoint)
|
||||||
decoder.dateDecodingStrategy = .iso8601
|
decoder.dateDecodingStrategy = .iso8601
|
||||||
let decoded: [SlackResponse] = try decoder.decode([SlackResponse].self, from: await data)
|
let decoded: [SlackResponse] = try decoder.decode([SlackResponse].self, from: await data)
|
||||||
try storeDB(data: await data)
|
try storeDB(data: await data)
|
||||||
return SlackResponse.toEmojis(from: decoded)
|
return await SlackResponse.toEmojis(from: decoded)
|
||||||
} catch {
|
} catch {
|
||||||
print(error.localizedDescription)
|
print(error.localizedDescription)
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
Reference in New Issue
Block a user