theme loading works!! before i tried decoding the entire array when it expected one theme lol

This commit is contained in:
neon443
2025-06-27 18:25:16 +01:00
parent d33b7d4eaf
commit 338957e986
3 changed files with 31 additions and 28 deletions

View File

@@ -29,7 +29,7 @@ class HostsManager: ObservableObject, @unchecked Sendable {
guard let decodedThemeNames = try? JSONDecoder().decode([String].self, from: dataThemeNames) else { return } guard let decodedThemeNames = try? JSONDecoder().decode([String].self, from: dataThemeNames) else { return }
for index in 0..<decodedThemes.count { for index in 0..<decodedThemes.count {
guard let encoded = try? JSONEncoder().encode(decodedThemes) else { return } guard let encoded = try? JSONEncoder().encode(decodedThemes[index]) else { return }
guard let synthedTheme = Theme.decodeTheme(name: decodedThemeNames[index], data: encoded) else { return } guard let synthedTheme = Theme.decodeTheme(name: decodedThemeNames[index], data: encoded) else { return }
self.themes.append(synthedTheme) self.themes.append(synthedTheme)
} }

View File

@@ -51,26 +51,35 @@ 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 }
let plistDecoder = PropertyListDecoder() if string.contains("plist") {
let jsonDecoder = JSONDecoder() guard let decoded = try? PropertyListDecoder().decode(ThemeCodable.self, from: data) else { return nil }
return Theme(
guard let decoded = name: name,
(try? plistDecoder.decode(ThemeCodable.self, from: data)) ?? ansi: decoded.ansi,
(try? jsonDecoder.decode(ThemeCodable.self, from: data)) foreground: Color(decoded.foreground),
else { return nil } background: Color(decoded.background),
let theme = Theme( cursor: Color(decoded.cursor),
name: name, cursorText: Color(decoded.cursorText),
ansi: decoded.ansi, bold: Color(decoded.bold),
foreground: Color(decoded.foreground), selectedText: Color(decoded.selectedText),
background: Color(decoded.background), selection: Color(decoded.selection)
cursor: Color(decoded.cursor), )
cursorText: Color(decoded.cursorText), } else {
bold: Color(decoded.bold), guard let decoded = try? JSONDecoder().decode(ThemeCodable.self, from: data) else { return nil }
selectedText: Color(decoded.selectedText), return Theme(
selection: Color(decoded.selection) name: name,
) ansi: decoded.ansi,
return theme 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)
)
}
} }
} }

View File

@@ -29,21 +29,15 @@ struct ThemesView: View {
HStack { HStack {
ForEach(0..<8, id: \.self) { index in ForEach(0..<8, id: \.self) { index in
Rectangle() Rectangle()
.frame(width: 12) .frame(width: 12, height: 12)
.foregroundStyle(theme.ansi[index].suiColor) .foregroundStyle(theme.ansi[index].suiColor)
.onAppear {
print(index)
}
} }
} }
HStack { HStack {
ForEach(8..<16, id: \.self) { index in ForEach(8..<16, id: \.self) { index in
Rectangle() Rectangle()
.frame(width: 12) .frame(width: 12, height: 12)
.foregroundStyle(theme.ansi[index].suiColor) .foregroundStyle(theme.ansi[index].suiColor)
.onAppear {
print(index)
}
} }
} }
} }