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
This commit is contained in:
neon443
2025-10-17 20:27:09 +01:00
parent f4e85f7c50
commit f1fd056c60

View File

@@ -8,22 +8,57 @@
import Foundation import Foundation
import SwiftUI 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 name: String
var urlString: String var urlString: String
var url: URL { var url: URL {
return URL(string: urlString) ?? URL(string: "https://")! return URL(string: urlString) ?? URL(string: "https://")!
} }
var image: Image {
if let data = try? Data(contentsOf: url), var image: Image { Image(uiImage: uiImage) }
let uiimage = UIImage(data: data) { private var uiImage: UIImage
return Image(uiImage: uiimage)
} // var image: Image {
return Image(uiImage: UIImage()) // 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) { init(name: String, url: String) {
self.name = name self.name = name
self.urlString = url 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))!
} }
} }