Skip to content

Commit

Permalink
Some changes to the Properties system
Browse files Browse the repository at this point in the history
  • Loading branch information
DerGoogler committed Nov 11, 2023
1 parent cf780a1 commit 26a06ca
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 165 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ Introducing Magisk Module Repo Loader (MMRL) - the ultimate module manager for M
- [x] Magisk Delta
- [x] KernelSU

## Retive configs

```shell
function getconf {
/system/bin/getprop "$1" "$2" | sed 's/"//g'
}
```

## Screenshots

<table>
Expand Down
65 changes: 0 additions & 65 deletions Website/src/hooks/useLocalForage.ts

This file was deleted.

4 changes: 0 additions & 4 deletions Website/src/hooks/useModConf.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
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
Expand Down
110 changes: 24 additions & 86 deletions Website/src/hooks/useNativeProperties.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { os } from "@Native/Os";
import { Dispatch, SetStateAction, useStateCallback } from "./useStateCallback";
import { useLog } from "./native/useLog";
import { Shell } from "@Native/Shell";
import { useNativeStorage } from "./useNativeStorage";
import { parseJSON, useNativeStorage } from "./useNativeStorage";
import { Properties } from "@Native/Properties";

declare global {
interface WindowEventMap {
Expand All @@ -18,80 +19,51 @@ declare global {

export type SetValue<T> = Dispatch<SetStateAction<T>, T>;

export const nativeProperties = {
setItem(key: str, val: str): void {
Shell.cmd(`setprop "${key}" "${val}"`).exec();
},
getItem(key: str, initialValue: str): string | "" {
return window.__properties__.get(key, initialValue);
},
};

if (window.__nativeStorage__) {
window.__nativeStorage__.defineName("localstorage");
}

/**
* Fallback for playground
* @returns
*/
export function useNativeProperties(key: string, initialValue: str): [str, SetValue<str>] {
if (os.isAndroid) {
return __useProperties(key, initialValue);
function convertToProperType(value: string) {
if (/^(true|1|y|yes|on)$/i.test(value)) {
return /^(true|1|y|yes|on)$/i.test(value); // Convert to boolean true
} else if (!isNaN(value as unknown as number)) {
return parseFloat(value); // Convert to number if it's a valid number
} else {
return useNativeStorage<str>(key, initialValue);
return value; // Return the original string if no conversion is possible
}
}

function __useProperties(key: string, initialValue: str): [str, SetValue<str>] {
export function useNativeProperties(
key: string,
initialValue: string | boolean | number
): [string | boolean | number, SetValue<string | boolean | number>] {
const log = useLog("useNativeProperties");
// Get from local storage then

// parse stored json or return initialValue

const readValue = useCallback((): str => {
const readValue = useCallback((): string | boolean | number => {
// Prevent build error "window is undefined" but keeps working

if (typeof window === "undefined") {
return initialValue;
}

return nativeProperties.getItem(key, initialValue);
}, [initialValue, key]);

// State to store our value

// Pass initial state function to useState so logic is only executed once

const [storedValue, setStoredValue] = useStateCallback<str>(readValue);
try {
const item = Properties.get(key, String(initialValue));

// Return a wrapped version of useState's setter function that ...
return item ? (parseJSON(item) as string | boolean | number) : initialValue;
} catch (error) {
log.w(`Error reading nativeStorage key “${key}”: ${error}`);

// ... persists the new value to localStorage.
return initialValue;
}
}, [initialValue, key]);

const setValue: SetValue<str> = (value, callback) => {
// Prevent build error "window is undefined" but keeps working
const [storedValue, setStoredValue] = useStateCallback<string | boolean | number>(readValue);

const setValue: SetValue<string | boolean | number> = (value, callback) => {
if (typeof window === "undefined") {
log.w(`Tried setting nativeProperties key “${key}” even though environment is not a client`);
}

try {
// Allow value to be a function so we have the same API as useState

const newValue = value instanceof Function ? value(storedValue) : value;

// Save to local storage

nativeProperties.setItem(key, JSON.stringify(newValue));

// Save state

Properties.set(key, JSON.stringify(newValue));
setStoredValue(newValue, callback);

// We dispatch a custom event so every useLocalStorage hook are notified

// window.dispatchEvent(new Event("local-storage"));
} catch (error) {
log.w(`Error setting localStorage key “${key}”: ${error}`);
}
Expand All @@ -101,39 +73,5 @@ function __useProperties(key: string, initialValue: str): [str, SetValue<str>] {
setStoredValue(readValue());
}, []);

// const handleStorageChange = useCallback(
// (event: StorageEvent | CustomEvent) => {
// if ((event as StorageEvent)?.key && (event as StorageEvent).key !== key) {
// return;
// }

// setStoredValue(readValue());
// },

// [key, readValue]
// );

// // this only works for other documents, not the current one

// useEventListener("storage", handleStorageChange);

// // this is a custom event, triggered in writeValueToLocalStorage

// // See: useLocalStorage()

// useEventListener("native-storage", handleStorageChange);

return [storedValue, setValue];
}

// A wrapper for "JSON.parse()"" to support "undefined" value

function parseJSON<T>(value: string | null): T | Error {
try {
return value === "undefined" ? undefined : JSON.parse(value ?? "");
} catch (e) {
console.log("parsing error on", { value });

return e as Error;
}
}
2 changes: 1 addition & 1 deletion Website/src/hooks/useNativeStorage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export function useNativeStorage<T>(key: string, initialValue: T): [T, SetValue<

// A wrapper for "JSON.parse()"" to support "undefined" value

function parseJSON<T>(value: string | null): T | Error {
export function parseJSON<T>(value: string | null): T | Error {
try {
return value === "undefined" ? undefined : JSON.parse(value ?? "");
} catch (e) {
Expand Down
1 change: 0 additions & 1 deletion Website/src/hooks/useRepos.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import _ from "underscore";
import { useSettings } from "./useSettings";
import { os } from "@Native/Os";
import { useLog } from "./native/useLog";
import { useLocalForage } from "./useLocalForage";

export interface RepoContextActions {
addRepo: (data: AddRepoData) => void;
Expand Down
4 changes: 0 additions & 4 deletions Website/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ import { strs } from "./locales/declaration";
ons.platform.select("android");

ons.ready(() => {
if (window.__nativeStorage__) {
window.__nativeStorage__.defineName("localstorage");
}

customElements.define("mmrl-app", MMRLApp);
customElements.define("mmrl-anchor", MMRLAnchor);

Expand Down
1 change: 0 additions & 1 deletion Website/src/native/Native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export class Native<I = any> implements INative<I> {
* @required true
*/
public constructor(i: I) {
if (typeof i === "undefined") throw new Error("No interface defined");
this._internal_interface = i;
}

Expand Down
4 changes: 3 additions & 1 deletion Website/src/native/Properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ class PropertiesClass extends Native<IProperties> {
if (this.isAndroid) {
return this.interface.get(key, def);
} else {
return "";
return window.localStorage.getItem(key) || def;
}
}

public set(key: string, value: string): void {
if (this.isAndroid) {
Shell.cmd(`setprop "${key}" "${value}"`).exec();
} else {
return window.localStorage.setItem(key, value);
}
}
}
Expand Down
10 changes: 8 additions & 2 deletions Website/src/typings/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ declare global {
}
}

interface NativeStorage extends Storage {
getItem(key: string, def?: string): string;
}

/**
* Native window properties for Android
*/
Expand Down Expand Up @@ -74,7 +78,7 @@ declare global {
*
* - This interface is not configurable
*/
readonly __nativeStorage__: Pick<Storage, "getItem" | "setItem" | "removeItem" | "clear"> & { defineName: (name: string) => void };
readonly __nativeStorage__: NativeStorage;
}

namespace Terminal {
Expand All @@ -101,7 +105,9 @@ declare global {
export function getFile(type: string, successCallback: SuccessCallback, ErrorCallback: ErrorCallback): any;
}

interface Window extends AndroidWindow<any> {}
interface Window extends AndroidWindow<any> {
localStorage: NativeStorage;
}

const Toast: {
LENGTH_LONG: "long";
Expand Down

0 comments on commit 26a06ca

Please sign in to comment.