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 }
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 }
self.themes.append(synthedTheme)
}

View File

@@ -51,26 +51,35 @@ struct Theme: Hashable, Equatable, Identifiable {
static func decodeTheme(name: String, data: Data?) -> Theme? {
guard let data else { return nil }
guard let string = String(data: data, encoding: .utf8) else { return nil }
let plistDecoder = PropertyListDecoder()
let jsonDecoder = JSONDecoder()
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,
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)
)
return theme
if string.contains("plist") {
guard let decoded = try? PropertyListDecoder().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)
)
} else {
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)
)
}
}
}

View File

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