Skip to content

Commit

Permalink
Yet another theme fix
Browse files Browse the repository at this point in the history
  • Loading branch information
pylixonly committed Mar 11, 2024
1 parent b9a29aa commit 9a12ae9
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 42 deletions.
26 changes: 6 additions & 20 deletions src/core/ui/components/ThemeCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ import { formatString, Strings } from "@core/i18n";
import Card, { CardWrapper } from "@core/ui/components/Card";
import { getAssetIDByName } from "@lib/api/assets";
import { useProxy } from "@lib/api/storage";
import { applyTheme, fetchTheme, removeTheme, selectTheme, Theme, themes } from "@lib/managers/themes";
import { applyTheme, fetchTheme, removeTheme, selectTheme, Theme } from "@lib/managers/themes";
import { settings } from "@lib/settings";
import { ButtonColors } from "@lib/utils/types";
import { clipboard } from "@metro/common";
import { showConfirmationAlert } from "@ui/alerts";
import { showToast } from "@ui/toasts";

async function selectAndApply(value: boolean, id: string) {
function selectAndApply(value: boolean, theme: Theme) {
try {
await selectTheme(value ? id : null);
applyTheme(value ? themes[id] : null);
selectTheme(value ? theme : null);
applyTheme(value ? theme : null);
} catch (e: any) {
console.error("Error while selectAndApply,", e);
}
Expand All @@ -21,8 +21,6 @@ async function selectAndApply(value: boolean, id: string) {
export default function ThemeCard({ item: theme, index }: CardWrapper<Theme>) {
useProxy(theme);

// little hack, our useProxy is kinda not reliable here
const [, forceUpdate] = React.useReducer(n => ~n, 0);
const [removed, setRemoved] = React.useState(false);

// This is needed because of React™
Expand All @@ -39,8 +37,7 @@ export default function ThemeCard({ item: theme, index }: CardWrapper<Theme>) {
toggleType={!settings.safeMode?.enabled ? "radio" : undefined}
toggleValue={theme.selected}
onToggleChange={(v: boolean) => {
selectAndApply(v, theme.id);
forceUpdate();
selectAndApply(v, theme);
}}
overflowTitle={theme.data.name}
overflowActions={[
Expand All @@ -49,18 +46,7 @@ export default function ThemeCard({ item: theme, index }: CardWrapper<Theme>) {
label: Strings.REFETCH,
onPress: () => {
fetchTheme(theme.id, theme.selected).then(() => {
// if (theme.selected) {
// showConfirmationAlert({
// title: Strings.MODAL_THEME_REFETCHED,
// content: Strings.MODAL_THEME_REFETCHED_DESC,
// confirmText: Strings.RELOAD,
// cancelText: Strings.CANCEL,
// confirmColor: ButtonColors.RED,
// onConfirm: () => BundleUpdaterManager.reload(),
// });
// } else {
showToast(Strings.THEME_REFETCH_SUCCESSFUL, getAssetIDByName("toast_image_saved"));
// }
}).catch(() => {
showToast(Strings.THEME_REFETCH_FAILED, getAssetIDByName("Small"));
});
Expand All @@ -87,7 +73,7 @@ export default function ThemeCard({ item: theme, index }: CardWrapper<Theme>) {
onConfirm: () => {
removeTheme(theme.id).then(wasSelected => {
setRemoved(true);
if (wasSelected) selectAndApply(false, theme.id);
if (wasSelected) selectAndApply(false, theme);
}).catch((e: Error) => {
showToast(e.message, getAssetIDByName("Small"));
});
Expand Down
2 changes: 1 addition & 1 deletion src/core/vendettaObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export const initVendettaObject = (): any => {
themes: themes.themes,
fetchTheme: (id: string, selected?: boolean) => themes.fetchTheme(id, selected),
installTheme: (id: string) => themes.installTheme(id),
selectTheme: (id: string) => themes.selectTheme(id === "default" ? null : id),
selectTheme: (id: string) => themes.selectTheme(id === "default" ? null : themes.themes[id]),
removeTheme: (id: string) => themes.removeTheme(id),
getCurrentTheme: () => themes.getThemeFromLoader(),
updateThemes: () => themes.updateThemes()
Expand Down
6 changes: 3 additions & 3 deletions src/lib/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { getAssetIDByName } from "@lib/api/assets";
import { getLoaderName, isThemeSupported } from "@lib/api/native/loader";
import { BundleUpdaterManager, ClientInfoManager, DeviceManager } from "@lib/api/native/modules";
import { after } from "@lib/api/patcher";
import { getThemeFromLoader, selectTheme } from "@lib/managers/themes";
import { getThemeFromLoader, selectTheme, themes } from "@lib/managers/themes";
import { settings } from "@lib/settings";
import { logger } from "@lib/utils/logger";
import { showToast } from "@ui/toasts";
Expand Down Expand Up @@ -36,9 +36,9 @@ export async function toggleSafeMode() {
if (isThemeSupported()) {
if (getThemeFromLoader()?.id) settings.safeMode!.currentThemeId = getThemeFromLoader()!.id;
if (settings.safeMode?.enabled) {
await selectTheme("default");
await selectTheme(null);
} else if (settings.safeMode?.currentThemeId) {
await selectTheme(settings.safeMode?.currentThemeId);
await selectTheme(themes[settings.safeMode?.currentThemeId]);
}
}
setTimeout(BundleUpdaterManager.reload, 400);
Expand Down
30 changes: 12 additions & 18 deletions src/lib/managers/themes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,16 +187,16 @@ export async function installTheme(id: string) {
await fetchTheme(id);
}

export async function selectTheme(id: string | null) {
Object.values(themes).forEach(s => s.selected = s.id === id);
export function selectTheme(theme: Theme | null) {
Object.keys(themes).forEach(
k => themes[k].selected = themes[k].id === theme?.id
);

if (id === null) {
return await writeTheme({});
} else {
themes[id].selected = true;
if (theme === null) {
return writeTheme({});
}

await writeTheme(themes[id]);
return writeTheme(theme);
}

export async function removeTheme(id: string) {
Expand Down Expand Up @@ -225,7 +225,7 @@ const origRawColor = { ...color.RawColor };
let inc = 0;
let vdKey = "vd-theme";

let vdThemeFallback = "dark";
let vdThemeFallback = "darker";
let enabled = false;
let currentTheme: Theme | null;

Expand All @@ -234,13 +234,6 @@ function isDiscordTheme(name: string) {
return discordThemes.has(name);
}

const dMapThemeType = {
dark: "dark",
darker: "dark",
midnight: "dark",
light: "light"
};

function patchColor() {
const isThemeModule = find(m => m.isThemeDark && Object.getOwnPropertyDescriptor(m, "isThemeDark")?.value);
const callback = ([theme]: any[]) => theme === vdKey ? [vdThemeFallback] : void 0;
Expand Down Expand Up @@ -302,7 +295,7 @@ function patchColor() {
if (value._state?.theme) {
const { theme } = value._state;
if (isDiscordTheme(theme)) {
selectTheme("default");
selectTheme(null);
vdThemeFallback = theme;
} else {
value._state.theme = vdThemeFallback;
Expand All @@ -319,7 +312,9 @@ function patchColor() {

instead("resolveSemanticColor", color.default.meta ?? color.default.internal, (args, orig) => {
if (!enabled || !currentTheme) return orig(...args);
if (args[1] !== vdKey) return orig(...args);
if (args[0] !== vdKey) return orig(...args);

args[0] = vdThemeFallback;

const [name, colorDef] = extractInfo(vdThemeFallback, args[1]);

Expand Down Expand Up @@ -358,7 +353,6 @@ function getDefaultFallbackTheme(fallback: string = vdThemeFallback) {

export function applyTheme(appliedTheme: Theme | null, fallbackTheme?: string, update = true) {
if (!fallbackTheme) fallbackTheme = getDefaultFallbackTheme();
fallbackTheme = (dMapThemeType as any)[fallbackTheme] ?? "dark";

currentTheme = appliedTheme;
vdThemeFallback = fallbackTheme!!;
Expand Down

0 comments on commit 9a12ae9

Please sign in to comment.