Skip to content

Commit

Permalink
refactor!: update spec 3 changes
Browse files Browse the repository at this point in the history
  • Loading branch information
pylixonly committed Nov 23, 2024
1 parent c168237 commit 237de91
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 17 deletions.
2 changes: 2 additions & 0 deletions src/lib/addons/plugins/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export interface PluginSettingsStorage {
}

export interface BunnyPluginManifest extends BunnyManifest {
readonly type: "plugin";
readonly spec: 3;
readonly main: string;
}

Expand Down
8 changes: 5 additions & 3 deletions src/lib/addons/themes/colors/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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"] = {};
Expand Down
11 changes: 7 additions & 4 deletions src/lib/addons/themes/colors/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
31 changes: 21 additions & 10 deletions src/lib/addons/themes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions src/lib/addons/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ export type Author = { name: string, id?: `${bigint}`; };

export interface BunnyManifest {
readonly id: string;
readonly spec: number;
readonly version: string;
readonly type: string;
readonly display: {
readonly name: string;
readonly description?: string;
readonly authors?: Author[];
};
readonly main: unknown;
readonly extras?: {
readonly [key: string]: any;
};
Expand Down

0 comments on commit 237de91

Please sign in to comment.