refactor!: update spec 3 changes

This commit is contained in:
pylixonly 2024-11-23 11:31:19 +08:00
parent c168237fc9
commit 237de91dce
5 changed files with 37 additions and 17 deletions

View file

@ -24,6 +24,8 @@ export interface PluginSettingsStorage {
}
export interface BunnyPluginManifest extends BunnyManifest {
readonly type: "plugin";
readonly spec: 3;
readonly main: string;
}

View file

@ -14,7 +14,7 @@ export function parseColorManifest(manifest: ColorManifest): InternalColorDefini
if (manifest.spec === 3) {
const semanticColorDefinitions: InternalColorDefinition["semantic"] = {};
for (const [semanticColorKey, semanticColorValue] of Object.entries(manifest.semantic ?? {})) {
for (const [semanticColorKey, semanticColorValue] of Object.entries(manifest.main.semantic ?? {})) {
if (typeof semanticColorValue === "object") {
const { type, value, opacity: semanticColorOpacity } = semanticColorValue;
@ -47,12 +47,14 @@ export function parseColorManifest(manifest: ColorManifest): InternalColorDefini
}
}
if (Platform.OS === "android") applyAndroidAlphaKeys(manifest.main.raw);
return {
spec: 3,
reference: resolveType(manifest.type),
semantic: semanticColorDefinitions,
raw: manifest.raw ?? {},
background: manifest.background,
raw: manifest.main.raw ?? {},
background: manifest.main.background,
};
} else if (manifest.spec === 2) { // is Vendetta theme
const semanticDefinitions: InternalColorDefinition["semantic"] = {};

View file

@ -13,11 +13,14 @@ interface BackgroundDefinition {
}
export interface BunnyColorManifest extends BunnyManifest {
type: "color";
spec: 3;
type: "dark" | "light";
semantic?: Record<string, string | SemanticReference>;
raw?: Record<string, string>;
background?: BackgroundDefinition;
main: {
type: "dark" | "light";
semantic?: Record<string, string | SemanticReference>;
raw?: Record<string, string>;
background?: BackgroundDefinition;
}
}
export interface VendettaThemeManifest {

View file

@ -60,30 +60,41 @@ function processData(data: VendettaThemeManifest) {
return data;
}
export async function fetchTheme(id: string, selected = false) {
function validateTheme(themeJSON: any): boolean {
if (typeof themeJSON !== "object" || themeJSON === null) return false;
if (themeJSON.spec !== 2 && themeJSON.spec !== 3) return false;
if (!themeJSON.main) return false;
return true;
}
export async function fetchTheme(url: string, selected = false) {
let themeJSON: any;
try {
themeJSON = await (await safeFetch(id, { cache: "no-store" })).json();
themeJSON = await (await safeFetch(url, { cache: "no-store" })).json();
} catch {
throw new Error(`Failed to fetch theme at ${id}`);
throw new Error(`Failed to fetch theme at ${url}`);
}
themes[id] = {
id: id,
// Validate theme
if (!validateTheme(themeJSON)) throw new Error(`Invalid theme at ${url}`);
themes[url] = {
id: url,
selected: selected,
data: processData(themeJSON),
};
if (selected) {
writeThemeToNative(themes[id]);
updateBunnyColor(themes[id].data, { update: true });
writeThemeToNative(themes[url]);
updateBunnyColor(themes[url].data, { update: true });
}
}
export async function installTheme(id: string) {
if (typeof id !== "string" || id in themes) throw new Error("Theme already installed");
await fetchTheme(id);
export async function installTheme(url: string) {
if (typeof url !== "string" || url in themes) throw new Error("Theme already installed");
await fetchTheme(url);
}
export function selectTheme(theme: VdThemeInfo | null, write = true) {

View file

@ -2,6 +2,7 @@ export type Author = { name: string, id?: `${bigint}`; };
export interface BunnyManifest {
readonly id: string;
readonly spec: number;
readonly version: string;
readonly type: string;
readonly display: {
@ -9,6 +10,7 @@ export interface BunnyManifest {
readonly description?: string;
readonly authors?: Author[];
};
readonly main: unknown;
readonly extras?: {
readonly [key: string]: any;
};