Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
DerGoogler committed Oct 28, 2023
1 parent a6b9969 commit 83a1826
Show file tree
Hide file tree
Showing 13 changed files with 230 additions and 197 deletions.
2 changes: 1 addition & 1 deletion Website/src/activitys/AboutActivity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const CheckRoot = (props: CheckRootProps): React.JSX.Element => {

const AboutActivity = () => {
const { strings } = useStrings();
const { settings, _modConf } = useSettings();
const { settings } = useSettings();
const { theme } = useTheme();
const { context, extra } = useActivity();

Expand Down
4 changes: 2 additions & 2 deletions Website/src/activitys/ConfigureActivity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { Toolbar } from "@Components/onsenui/Toolbar";
import { useActivity } from "@Hooks/useActivity";
import React from "react";
import { SuFile } from "@Native/SuFile";
import { useSettings } from "@Hooks/useSettings";
import { ConfigureView } from "@Components/ConfigureView";
import { PreviewErrorBoundary } from "./PlaygroundsActivity";
import { useModConf } from "@Hooks/useModConf";

type Extra = {
raw_data?: string;
Expand All @@ -14,7 +14,7 @@ type Extra = {
};

const ConfigureActivity = () => {
const { modConf } = useSettings();
const { modConf } = useModConf();
const { context, extra } = useActivity<Extra>();

const config: string = React.useMemo(() => {
Expand Down
4 changes: 3 additions & 1 deletion Website/src/activitys/MainApplication.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { AnimatePresence, motion } from "framer-motion";
import InputBase from "@mui/material/InputBase";
import SearchIcon from "@mui/icons-material/Search";
import ClearIcon from "@mui/icons-material/Clear";
import { useModConf } from "@Hooks/useModConf";

interface SearchbarRef {
clear(): void;
Expand Down Expand Up @@ -91,7 +92,8 @@ const SearchBar = React.forwardRef<SearchbarRef, SearchbarProps>((props, ref) =>

const MainApplication = () => {
const { strings } = useStrings();
const { settings, modConf } = useSettings();
const { settings } = useSettings();
const { modConf } = useModConf();
const { context } = useActivity();
const { theme } = useTheme();
const { modules } = useRepos();
Expand Down
8 changes: 4 additions & 4 deletions Website/src/activitys/ModConfActivity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { Alert, Box, Divider, List, ListSubheader } from "@mui/material";
import { Toolbar } from "@Components/onsenui/Toolbar";
import { Page } from "@Components/onsenui/Page";
import { useActivity } from "@Hooks/useActivity";
import { ModConf, useSettings } from "@Hooks/useSettings";
import { useSettings } from "@Hooks/useSettings";
import { ModConf, useModConf } from "@Hooks/useModConf";
import { StyledListItemText } from "@Components/StyledListItemText";
import { Shell } from "@Native/Shell";
import { DialogEditTextListItem } from "@Components/DialogEditTextListItem";
Expand All @@ -29,7 +30,7 @@ interface ModConfListItem<K extends keyof ModConf> {
function ModConfActivity() {
const { context } = useActivity();

const { _modConf, setModConf } = useSettings();
const { _modConf, setModConf } = useModConf();

const renderToolbar = () => {
return (
Expand Down Expand Up @@ -171,8 +172,7 @@ function ModConfActivity() {
<StyledListItemText
primary={
<Box sx={{ display: "flex", alignItems: "center", justifyItems: "center" }}>
{item.logoText && <item.logoText sx={{ mr: 1 }} width="1rem" height="1rem" />}
[{item.confKey}]: {item.text}
{item.logoText && <item.logoText sx={{ mr: 1 }} width="1rem" height="1rem" />}[{item.confKey}]: {item.text}
</Box>
}
secondary={_modConf[item.confKey]}
Expand Down
7 changes: 4 additions & 3 deletions Website/src/activitys/TerminalActivity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import Ansi from "ansi-to-react";
import ArrowBackIcon from "@mui/icons-material/ArrowBack";
import React from "react";
import { Shell } from "@Native/Shell";
import { formatString, useSettings } from "@Hooks/useSettings";
import { useNewerVersion } from "@Hooks/useNewerVersion";
import { useSettings } from "@Hooks/useSettings";
import { BuildConfig } from "@Native/BuildConfig";
import { useModConf, formatString } from "@Hooks/useModConf";

const TerminalActivity = () => {
const { context, extra } = useActivity<any>();
const { settings, modConf, _modConf } = useSettings();
const { settings } = useSettings();
const { modConf, _modConf } = useModConf();
const [active, setActive] = React.useState<bool>(true);

const [lines, setLines] = React.useState<string[]>([]);
Expand Down
4 changes: 2 additions & 2 deletions Website/src/components/ConfigureView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { PluginObj } from "@babel/core";
import { globals, libraries } from "./libs";
import { DialogEditListItem, StyledListSubheader } from "./components";
import { SuFile, wasmFs } from "@Native/SuFile";
import { ModConf, useSettings } from "@Hooks/useSettings";
import { ModConf, useModConf } from "@Hooks/useModConf";

function plugin({ types: t }): PluginObj {
return {
Expand Down Expand Up @@ -69,7 +69,7 @@ const scope = {

export const ConfigureView = React.memo<PreviewErrorBoundaryChildren>((props) => {
const { theme } = useTheme();
const { modConf } = useSettings();
const { modConf } = useModConf();

const format = React.useCallback<<K extends keyof ModConf>(key: K) => ModConf[K]>((key) => modConf(key, { MODID: props.modid }), []);

Expand Down
6 changes: 4 additions & 2 deletions Website/src/components/DeviceModule.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import { ConfigureActivity } from "@Activitys/ConfigureActivity";
import { StyledIconButton } from "./StyledIconButton";
import { useLog } from "@Hooks/native/useLog";
import { Properties } from "properties-file";
import { ModConf, colors, useSettings } from "@Hooks/useSettings";
import { colors, useSettings } from "@Hooks/useSettings";
import { useTheme } from "@Hooks/useTheme";
import TerminalActivity from "@Activitys/TerminalActivity";
import { useRepos } from "@Hooks/useRepos";
import { ModConf, useModConf } from "@Hooks/useModConf";

export const badgeStyle: (color: (typeof colors)["blue" | "teal" | "red" | "orange"]) => SxProps<Theme> = (color) => {
return {
Expand All @@ -33,7 +34,8 @@ interface Props {

const DeviceModule = React.memo<Props>((props) => {
const { strings } = useStrings();
const { settings, modConf } = useSettings();
const { settings } = useSettings();
const { modConf } = useModConf();
const { theme } = useTheme();
const { context, extra } = useActivity<any>();
const { modules } = useRepos();
Expand Down
39 changes: 17 additions & 22 deletions Website/src/hooks/useLocalForage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,27 @@ export function useLocalForage<D>(key: string, initialValue: D, errorHandler?: E
};

useEffect(() => {
(async function () {
try {
const value: D | null = await localForage.getItem(key);
setStoredValue(value == null ? initialValue : value);
} catch (e) {
error(e as any);
localForage.getItem<D>(key).then((value) => {
if (value !== null) {
setStoredValue(value);
} else {
setStoredValue(initialValue);
}
})();
});
}, []);

const setValue: SetValue<D> = useCallback(
(value, callback) => {
const newValue = value instanceof Function ? value(storedValue) : value;
async function set(value: D, callback: ((state: D) => void) | undefined) {
try {
setStoredValue(value, callback);
await localForage.setItem(key, value);
} catch (e) {
error(e as any);
}
const setValue: SetValue<D> = (value, callback) => {
setStoredValue(value, callback);
const newValue = value instanceof Function ? value(storedValue) : value;
localForage.setItem<D>(key, newValue).then((_value) => {
const _newValue = _value instanceof Function ? _value(storedValue) : _value;
if (_newValue !== null) {
setStoredValue(_newValue, callback);
} else {
setStoredValue(initialValue, callback);
}

set(newValue, callback);
},
[key]
);
});
};

const removeValue = useCallback(() => {
async function remove() {
Expand Down
148 changes: 148 additions & 0 deletions Website/src/hooks/useModConf.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import React, { createContext, useContext } from "react";
import { colors as kolors } from "@mui/material";
import { defaultComposer } from "default-composer";
import { useNativeStorage } from "./useNativeStorage";
import { os } from "@Native/Os";
import { SetStateAction } from "./useStateCallback";
import { useLanguageMap } from "./../locales/declaration";
import { useLocalForage } from "./useLocalForage";

export interface ModConf {
//cli
MSUCLI: string;
KSUCLI: string;

// default paths
ADB: string;
MODULES: string;
MODULECWD: string;
PROPS: string;
SYSTEM: string;
SEPOLICY: string;
CONFIG: string;

// service paths
LATESERVICE: string;
POSTSERVICE: string;
POSTMOUNT: string;
BOOTCOMP: string;

// status paths
SKIPMOUNT: string;
DISABLE: string;
REMOVE: string;
UPDATE: string;

// others
MMRLINI: string;
CONFCWD: string;
CONFINDEX: string;
}

export const INITIAL_MOD_CONF: ModConf = {
//cli
MSUCLI: "/system/bin/magisk --install-module <ZIPFILE>",
KSUCLI: "<ADB>/ksu/bin/ksud module install <ZIPFILE>",

// default paths
ADB: "/data/adb",
MODULES: "<ADB>/modules",
MODULECWD: "<MODULES>/<MODID>",
PROPS: "<MODULECWD>/module.prop",
SYSTEM: "<MODULECWD>/system.prop",
SEPOLICY: "<MODULECWD>/sepolicy.rule",
CONFIG: `<MODULECWD>/system/usr/share/mmrl/config/<MODID>.mdx`,

// service paths
LATESERVICE: "<MODULECWD>/service.sh",
POSTSERVICE: "<MODULECWD>/post-fs-data.sh",
POSTMOUNT: "<MODULECWD>/post-mount.sh",
BOOTCOMP: "<MODULECWD>/boot-completed.sh",

// status paths
SKIPMOUNT: "<MODULECWD>/skip_mount",
DISABLE: "<MODULECWD>/disable",
REMOVE: "<MODULECWD>/remove",
UPDATE: "<MODULECWD>/update",

// others
MMRLINI: "<MODULECWD>/mmrl_install_tools",
CONFCWD: "<MODULECWD>/system/usr/share/mmrl/config/<MODID>",
CONFINDEX: "<CONFCWD>/index.jsx",
};

export interface ModConfContext {
_modConf: ModConf;
modConf<K extends keyof ModConf>(key: K, adds?: Record<string, any>): ModConf[K];
setModConf<K extends keyof ModConf>(key: K, state: SetStateAction<ModConf[K]>, callback?: (state: ModConf[K]) => void): void;
}

export const ModConfContext = createContext<ModConfContext>({
_modConf: INITIAL_MOD_CONF,
modConf<K extends keyof ModConf>(key: K, adds?: Record<string, any>) {
return key;
},
setModConf<K extends keyof ModConf>(key: K, state: SetStateAction<ModConf[K]>, callback?: (state: ModConf[K]) => void) {},
});

export const useModConf = () => {
return useContext(ModConfContext);
};

export function formatString(template: string, object: object): string {
return template.replace(/\<(\w+(\.\w+)*)\>/gi, (match, key) => {
const keys = key.split(".");
let value = object;
for (const k of keys) {
if (k in value) {
value = value[k];
} else {
return match;
}
}
return formatString(String(value), object);
});
}

export const ModConfProvider = (props: React.PropsWithChildren) => {
const [modConf, setModConf] = useNativeStorage("modconf", INITIAL_MOD_CONF);

// Test purposes
// React.useEffect(() => {
// for (const k in modConf) {
// console.info(
// formatString(defaultComposer(INITIAL_MOD_CONF, modConf)[k], {
// ...modConf,
// ...{
// MODID: "node_on_android",
// ZIPFILE: "/sdard/xh.zip",
// },
// })
// );
// }
// }, [modConf]);

const contextValue = React.useMemo(
() => ({
_modConf: defaultComposer(INITIAL_MOD_CONF, modConf),
modConf: (key, adds) => {
return formatString(defaultComposer(INITIAL_MOD_CONF, modConf)[key], { ...modConf, ...adds });
},
setModConf: (name, state, callback) => {
setModConf(
(prev) => {
const newValue = React.useMemo(() => (state instanceof Function ? state(prev[name]) : state), [name]);
return {
...prev,
[name]: newValue,
};
},
(state) => callback && callback(state[name])
);
},
}),
[modConf]
);

return <ModConfContext.Provider value={contextValue} children={props.children} />;
};
2 changes: 0 additions & 2 deletions Website/src/hooks/useModulesFilter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import UpdateDisabledIcon from "@mui/icons-material/UpdateDisabled";
import CalendarMonthIcon from "@mui/icons-material/CalendarMonth";
import AbcIcon from "@mui/icons-material/Abc";
import { useTheme } from "./useTheme";
import Button from "@mui/material/Button";
import Avatar from "@mui/material/Avatar";
import List from "@mui/material/List";
import ListItem from "@mui/material/ListItem";
Expand All @@ -12,7 +11,6 @@ import ListItemButton from "@mui/material/ListItemButton";
import ListItemText from "@mui/material/ListItemText";
import Dialog from "@mui/material/Dialog";
import DialogTitle from "@mui/material/DialogTitle";
import { SortInterface } from "flatlist-react/lib";
import React from "react";

export const filters = [
Expand Down
2 changes: 1 addition & 1 deletion Website/src/hooks/useRepos.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ type SetRepoStateData = {
export const RepoProvider = (props: React.PropsWithChildren) => {
const TAG = "RepoProvider";
const log = useLog(TAG);
const [repos, setRepos, removeRepos] = useLocalForage<StoredRepo[]>("repos", [
const [repos, setRepos] = useNativeStorage<StoredRepo[]>("repos", [
{
name: "Magisk Modules Alt Repo (pre-configured)",
website: "",
Expand Down
Loading

0 comments on commit 83a1826

Please sign in to comment.