Skip to content
This repository has been archived by the owner on Feb 6, 2024. It is now read-only.

Commit

Permalink
[Global] Fix for 204201+ (#185)
Browse files Browse the repository at this point in the history
* Fix for 204201+

* Hooki-ify safeMode styles

* Re-add theme fixer

* def.d.ts

* Alternate `createThemedStyleSheet` approach

* verbatim import aa

* make backwards compatible

---------

Co-authored-by: redstonekasi <kasi@hanneskann.de>
  • Loading branch information
pylixonly and redstonekasi authored Nov 13, 2023
1 parent 289ffa4 commit 0a78aa4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/def.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ interface LoaderIdentity {

interface DiscordStyleSheet {
[index: string]: any,
createStyles: <T extends _RN.StyleSheet.NamedStyles<T>>(sheet: T | (() => T)) => () => T;
createThemedStyleSheet: typeof import("react-native").StyleSheet.create;
}

Expand Down
6 changes: 4 additions & 2 deletions src/lib/fixes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ function onDispatch({ locale }: { locale: string }) {
// Theming
// Based on https://github.com/Aliucord/AliucordRN/blob/main/src/ui/patchTheme.ts
try {
ThemeManager.overrideTheme(ThemeStore?.theme ?? "dark");
if (AMOLEDThemeManager && UnsyncedUserSettingsStore.useAMOLEDTheme === 2) AMOLEDThemeManager.setAMOLEDThemeEnabled(true);
if (ThemeManager) {
ThemeManager.overrideTheme(ThemeStore?.theme ?? "dark");
if (AMOLEDThemeManager && UnsyncedUserSettingsStore.useAMOLEDTheme === 2) AMOLEDThemeManager.setAMOLEDThemeEnabled(true);
}
} catch(e) {
logger.error("Failed to fix theme...", e);
}
Expand Down
36 changes: 33 additions & 3 deletions src/lib/metro/common.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,43 @@
import { find, findByProps, findByStoreName } from "@metro/filters";
import { DiscordStyleSheet } from "@types";
import { find, findByProps } from "@metro/filters";
import { ReactNative as RN } from "@lib/preinit";
import type { StyleSheet } from "react-native";

const ThemeStore = findByStoreName("ThemeStore");
const colorResolver = findByProps("colors", "meta").meta;

// Reimplementation of Discord's createThemedStyleSheet, which was removed since 204201
// Not exactly a 1:1 reimplementation, but sufficient to keep compatibility with existing plugins
function createThemedStyleSheet<T extends StyleSheet.NamedStyles<T>>(sheet: T) {
for (const key in sheet) {
// @ts-ignore
sheet[key] = new Proxy(RN.StyleSheet.flatten(sheet[key]), {
get(target, prop, receiver) {
const res = Reflect.get(target, prop, receiver);
return colorResolver.isSemanticColor(res)
? colorResolver.resolveSemanticColor(ThemeStore.theme, res)
: res
}
});
}

return sheet;
}

// Discord
export const constants = findByProps("Fonts", "Permissions");
export const channels = findByProps("getVoiceChannelId");
export const i18n = findByProps("Messages");
export const url = findByProps("openURL", "openDeeplink");
export const toasts = find(m => m.open && m.close && !m.startDrag && !m.init && !m.openReplay && !m.setAlwaysOnTop);
export const stylesheet = findByProps("createThemedStyleSheet") as DiscordStyleSheet;

// Compatible with pre-204201 versions since createThemedStyleSheet is undefined.
export const stylesheet = {
...find(m => m.createStyles && !m.ActionSheet),
createThemedStyleSheet,
...findByProps("createThemedStyleSheet") as {},
} as DiscordStyleSheet;

export const clipboard = findByProps("setString", "getString", "hasString") as typeof import("@react-native-clipboard/clipboard").default;
export const assets = findByProps("registerAsset");
export const invites = findByProps("acceptInviteAndTransitionToInviteChannel");
Expand All @@ -31,4 +61,4 @@ export const moment = findByProps("isMoment") as typeof import("moment");
export { chroma } from "@lib/preinit";

// Lodash
export const lodash = findByProps("forEachRight") as typeof import("lodash");
export const lodash = findByProps("forEachRight") as typeof import("lodash");

0 comments on commit 0a78aa4

Please sign in to comment.