make decodeTheme cleaner, still works yayyy

This commit is contained in:
neon443
2025-06-27 18:27:10 +01:00
parent 338957e986
commit c343066fb5

View File

@@ -51,11 +51,15 @@ struct Theme: Hashable, Equatable, Identifiable {
static func decodeTheme(name: String, data: Data?) -> Theme? { static func decodeTheme(name: String, data: Data?) -> Theme? {
guard let data else { return nil } guard let data else { return nil }
guard let string = String(data: data, encoding: .utf8) else { return nil }
if string.contains("plist") { let plistDecoder = PropertyListDecoder()
guard let decoded = try? PropertyListDecoder().decode(ThemeCodable.self, from: data) else { return nil } let jsonDecoder = JSONDecoder()
return Theme(
guard let decoded =
(try? plistDecoder.decode(ThemeCodable.self, from: data)) ??
(try? jsonDecoder.decode(ThemeCodable.self, from: data))
else { return nil }
let theme = Theme(
name: name, name: name,
ansi: decoded.ansi, ansi: decoded.ansi,
foreground: Color(decoded.foreground), foreground: Color(decoded.foreground),
@@ -66,20 +70,7 @@ struct Theme: Hashable, Equatable, Identifiable {
selectedText: Color(decoded.selectedText), selectedText: Color(decoded.selectedText),
selection: Color(decoded.selection) selection: Color(decoded.selection)
) )
} else { return theme
guard let decoded = try? JSONDecoder().decode(ThemeCodable.self, from: data) else { return nil }
return Theme(
name: name,
ansi: decoded.ansi,
foreground: Color(decoded.foreground),
background: Color(decoded.background),
cursor: Color(decoded.cursor),
cursorText: Color(decoded.cursorText),
bold: Color(decoded.bold),
selectedText: Color(decoded.selectedText),
selection: Color(decoded.selection)
)
}
} }
} }