From f1fd056c6062e995e59a257532bb25cc225842d9 Mon Sep 17 00:00:00 2001 From: neon443 <69979447+neon443@users.noreply.github.com> Date: Fri, 17 Oct 2025 20:27:09 +0100 Subject: [PATCH] tried to redo the image grabbing again to make it do it on init add init from decoder, to ignore the uiimage property add func enocde to ignore the uiimage property --- StickerSlack/Emoji/Emoji.swift | 49 +++++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 7 deletions(-) diff --git a/StickerSlack/Emoji/Emoji.swift b/StickerSlack/Emoji/Emoji.swift index 75e90fd..1e69463 100644 --- a/StickerSlack/Emoji/Emoji.swift +++ b/StickerSlack/Emoji/Emoji.swift @@ -8,22 +8,57 @@ import Foundation import SwiftUI -struct Emoji: Codable, Hashable { +protocol EmojiProtocol: Codable, Hashable { + var name: String { get set } + var urlString: String { get set } +} + +struct Emoji: EmojiProtocol { var name: String var urlString: String var url: URL { return URL(string: urlString) ?? URL(string: "https://")! } - var image: Image { - if let data = try? Data(contentsOf: url), - let uiimage = UIImage(data: data) { - return Image(uiImage: uiimage) - } - return Image(uiImage: UIImage()) + + var image: Image { Image(uiImage: uiImage) } + private var uiImage: UIImage + +// var image: Image { +// if let data = try? Data(contentsOf: url), +// let uiimage = UIImage(data: data) { +// return Image(uiImage: uiimage) +// } +// return Image(uiImage: UIImage()) +// } + + init(from decoder: any Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + self.init( + name: try container.decode(String.self, forKey: .name), + url: try container.decode(String.self, forKey: .urlString) + ) } init(name: String, url: String) { self.name = name self.urlString = url + self.uiImage = Emoji.grabImage(for: url) + } + + enum CodingKeys: CodingKey { + case name + case urlString + } + + func encode(to encoder: any Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(self.name, forKey: .name) + try container.encode(self.urlString, forKey: .urlString) + } + + static func grabImage(for url: String) -> UIImage { + let url = URL(string: url)! + return UIImage(data: try! Data(contentsOf: url))! } } +