[Global] Tidy usage of native modules

This commit is contained in:
Beef 2023-04-05 20:40:17 +01:00
parent 10618ae217
commit aff1c83e94
9 changed files with 36 additions and 35 deletions

2
src/def.d.ts vendored
View file

@ -239,7 +239,7 @@ interface MMKVManager {
clear: () => void;
}
interface DCDFileManager {
interface FileManager {
/**
* @param path **Full** path to file
*/

View file

@ -1,6 +1,7 @@
import { RNConstants } from "@types";
import { ReactNative as RN } from "@metro/common";
import { after } from "@lib/patcher";
import { ClientInfoManager, DeviceManager } from "@lib/native";
import { getAssetIDByName } from "@ui/assets";
import { showToast } from "@ui/toasts";
import logger from "@lib/logger";
@ -47,12 +48,6 @@ export function patchLogHook() {
export const versionHash: string = __vendettaVersion;
export function getDebugInfo() {
// Discord
//! 173.13 renamed this to RTNClientInfoManager.
const InfoDictionaryManager = RN.NativeModules.InfoDictionaryManager ?? RN.NativeModules.RTNClientInfoManager;
//! 173.14 renamed this to RTNDeviceManager.
const DCDDeviceManager = RN.NativeModules.DCDDeviceManager ?? RN.NativeModules.RTNDeviceManager;
// Hermes
const hermesProps = window.HermesInternal.getRuntimeProperties();
const hermesVer = hermesProps["OSS Release Version"];
@ -68,8 +63,8 @@ export function getDebugInfo() {
loader: window.__vendetta_loader?.name ?? "Unknown",
},
discord: {
version: InfoDictionaryManager.Version,
build: InfoDictionaryManager.Build,
version: ClientInfoManager.Version,
build: ClientInfoManager.Build,
},
react: {
version: React.version,
@ -104,15 +99,15 @@ export function getDebugInfo() {
manufacturer: PlatformConstants.Manufacturer,
brand: PlatformConstants.Brand,
model: PlatformConstants.Model,
codename: DCDDeviceManager.device
codename: DeviceManager.device
}
},
ios: {
device: {
manufacturer: DCDDeviceManager.deviceManufacturer,
brand: DCDDeviceManager.deviceBrand,
model: DCDDeviceManager.deviceModel,
codename: DCDDeviceManager.device
manufacturer: DeviceManager.deviceManufacturer,
brand: DeviceManager.deviceBrand,
model: DeviceManager.deviceModel,
codename: DeviceManager.device
}
}
}

11
src/lib/native.ts Normal file
View file

@ -0,0 +1,11 @@
import { MMKVManager as _MMKVManager, FileManager as _FileManager } from "@types";
const nmp = window.nativeModuleProxy;
export const MMKVManager = nmp.MMKVManager as _MMKVManager;
//! 173.10 renamed this to RTNFileManager.
export const FileManager = (nmp.DCDFileManager ?? nmp.RTNFileManager) as _FileManager;
//! 173.13 renamed this to RTNClientInfoManager.
export const ClientInfoManager = nmp.InfoDictionaryManager ?? nmp.RTNClientInfoManager;
//! 173.14 renamed this to RTNDeviceManager.
export const DeviceManager = nmp.DCDDeviceManager ?? nmp.RTNDeviceManager;
export const BundleUpdaterManager = nmp.BundleUpdaterManager;

View file

@ -1,10 +1,9 @@
import { Indexable, PluginManifest, Plugin, MMKVManager } from "@types";
import { Indexable, PluginManifest, Plugin } from "@types";
import { awaitSyncWrapper, createMMKVBackend, createStorage, wrapSync } from "@lib/storage";
import { MMKVManager } from "@lib/native";
import logger, { logModule } from "@lib/logger";
import safeFetch from "@utils/safeFetch";
const MMKVManager = window.nativeModuleProxy.MMKVManager as MMKVManager;
type EvaledPlugin = {
onLoad?(): void;
onUnload(): void;

View file

@ -1,9 +1,6 @@
import { DCDFileManager, MMKVManager, StorageBackend } from "@types";
import { StorageBackend } from "@types";
import { ReactNative as RN } from "@metro/common";
const MMKVManager = window.nativeModuleProxy.MMKVManager as MMKVManager;
//! 173.10 renamed this to RTNFileManager.
const DCDFileManager = (window.nativeModuleProxy.DCDFileManager ?? window.nativeModuleProxy.RTNFileManager) as DCDFileManager;
import { MMKVManager, FileManager } from "@lib/native";
export const createMMKVBackend = (store: string): StorageBackend => ({
get: async () => JSON.parse((await MMKVManager.getItem(store)) ?? "{}"),
@ -20,10 +17,10 @@ export const createFileBackend = (file: string): StorageBackend => {
let created: boolean;
return {
get: async () => {
const path = `${DCDFileManager.getConstants().DocumentsDirPath}/${file}`;
if (!created && !(await DCDFileManager.fileExists(path))) return (created = true), DCDFileManager.writeFile("documents", filePathFixer(file), "{}", "utf8");
return JSON.parse(await DCDFileManager.readFile(path, "utf8"));
const path = `${FileManager.getConstants().DocumentsDirPath}/${file}`;
if (!created && !(await FileManager.fileExists(path))) return (created = true), FileManager.writeFile("documents", filePathFixer(file), "{}", "utf8");
return JSON.parse(await FileManager.readFile(path, "utf8"));
},
set: (data) => void DCDFileManager.writeFile("documents", filePathFixer(file), JSON.stringify(data), "utf8"),
set: (data) => void FileManager.writeFile("documents", filePathFixer(file), JSON.stringify(data), "utf8"),
};
};

View file

@ -1,8 +1,6 @@
import { Emitter, MMKVManager, StorageBackend } from "@types";
import { Emitter, StorageBackend } from "@types";
import createEmitter from "../emitter";
const MMKVManager = window.nativeModuleProxy.MMKVManager as MMKVManager;
const emitterSymbol = Symbol("emitter accessor");
const syncAwaitSymbol = Symbol("wrapSync promise accessor");
@ -115,4 +113,4 @@ export function wrapSync<T extends Promise<any>>(store: T): Awaited<T> {
export const awaitSyncWrapper = (store: any) => new Promise<void>((res) => store[syncAwaitSymbol](res));
export * from "./backends";
export * from "@lib/storage/backends";

View file

@ -1,4 +1,4 @@
import { DCDFileManager, Indexable, Theme, ThemeData } from "@types";
import { Indexable, Theme, ThemeData } from "@types";
import { findByProps } from "@metro/filters";
import { ReactNative, chroma } from "@metro/common";
import { instead } from "@lib/patcher";
@ -9,7 +9,6 @@ import { safeFetch } from "@utils";
// Somehow, this is late enough, though?
export const color = findByProps("SemanticColor");
const DCDFileManager = window.nativeModuleProxy.DCDFileManager as DCDFileManager;
export const themes = wrapSync(createStorage<Indexable<Theme>>(createMMKVBackend("VENDETTA_THEMES")));
async function writeTheme(theme: Theme | {}) {

View file

@ -1,6 +1,7 @@
import { ButtonColors, Theme } from "@types";
import { ReactNative as RN, clipboard } from "@metro/common";
import { fetchTheme, removeTheme, selectTheme } from "@lib/themes";
import { BundleUpdaterManager } from "@lib/native";
import { getAssetIDByName } from "@ui/assets";
import { showConfirmationAlert } from "@ui/alerts";
import { showToast } from "@ui/toasts";
@ -13,7 +14,7 @@ interface ThemeCardProps {
async function selectAndReload(value: boolean, id: string) {
await selectTheme(value ? id : "default");
RN.NativeModules.BundleUpdaterManager.reload();
BundleUpdaterManager.reload();
}
export default function ThemeCard({ theme, index }: ThemeCardProps) {
@ -48,7 +49,7 @@ export default function ThemeCard({ theme, index }: ThemeCardProps) {
confirmText: "Reload",
cancelText: "Cancel",
confirmColor: ButtonColors.RED,
onConfirm: () => RN.NativeModules.BundleUpdaterManager.reload(),
onConfirm: () => BundleUpdaterManager.reload(),
})
} else {
showToast("Successfully refetched theme.", getAssetIDByName("toast_image_saved"));

View file

@ -4,6 +4,7 @@ import { Forms, Summary } from "@ui/components";
import { DISCORD_SERVER, GITHUB } from "@lib/constants";
import { getDebugInfo } from "@lib/debug";
import { useProxy } from "@lib/storage";
import { BundleUpdaterManager } from "@lib/native";
import settings from "@lib/settings";
import Version from "@ui/settings/components/Version";
import ErrorBoundary from "@ui/components/ErrorBoundary";
@ -102,7 +103,7 @@ export default function General() {
<FormRow
label="Reload Discord"
leading={<FormRow.Icon source={getAssetIDByName("ic_message_retry")} />}
onPress={() => RN.NativeModules.BundleUpdaterManager.reload()}
onPress={() => BundleUpdaterManager.reload()}
/>
<FormDivider />
<FormSwitchRow