REF: BlueClipboard JS -> TS

This commit is contained in:
Marcos Rodriguez Velez 2023-03-28 07:51:53 -04:00
parent 6ed25c807c
commit edf92c9dbc
2 changed files with 35 additions and 38 deletions

View File

@ -1,38 +0,0 @@
import { useAsyncStorage } from '@react-native-async-storage/async-storage';
import Clipboard from '@react-native-clipboard/clipboard';
function BlueClipboard() {
BlueClipboard.STORAGE_KEY = 'ClipboardReadAllowed';
const isClipboardAccessAllowed = useAsyncStorage(BlueClipboard.STORAGE_KEY).getItem;
const setIsClipboardAccessAllowed = useAsyncStorage(BlueClipboard.STORAGE_KEY).setItem;
BlueClipboard.isReadClipboardAllowed = async () => {
try {
const clipboardAccessAllowed = await isClipboardAccessAllowed();
if (clipboardAccessAllowed === null) {
await setIsClipboardAccessAllowed(JSON.stringify(true));
return true;
}
return !!JSON.parse(clipboardAccessAllowed);
} catch {
await setIsClipboardAccessAllowed(JSON.stringify(true));
return true;
}
};
BlueClipboard.setReadClipboardAllowed = value => {
setIsClipboardAccessAllowed(JSON.stringify(!!value));
};
BlueClipboard.getClipboardContent = async () => {
const isAllowed = await BlueClipboard.isReadClipboardAllowed();
if (isAllowed) {
return Clipboard.getString();
} else {
return '';
}
};
}
BlueClipboard.default = new BlueClipboard();
export default BlueClipboard;

35
blue_modules/clipboard.ts Normal file
View File

@ -0,0 +1,35 @@
import { useAsyncStorage } from '@react-native-async-storage/async-storage';
import Clipboard from '@react-native-clipboard/clipboard';
class BlueClipboard {
static STORAGE_KEY = 'ClipboardReadAllowed';
static isReadClipboardAllowed = async (): Promise<boolean> => {
const isClipboardAccessAllowed = await useAsyncStorage(BlueClipboard.STORAGE_KEY).getItem();
try {
if (isClipboardAccessAllowed === null) {
await useAsyncStorage(BlueClipboard.STORAGE_KEY).setItem(JSON.stringify(true));
return true;
}
return !!JSON.parse(isClipboardAccessAllowed);
} catch {
await useAsyncStorage(BlueClipboard.STORAGE_KEY).setItem(JSON.stringify(true));
return true;
}
};
static setReadClipboardAllowed = (value: boolean): void => {
useAsyncStorage(BlueClipboard.STORAGE_KEY).setItem(JSON.stringify(!!value));
};
static getClipboardContent = async (): Promise<string> => {
const isAllowed = await BlueClipboard.isReadClipboardAllowed();
if (isAllowed) {
return Clipboard.getString();
} else {
return '';
}
};
}
export default new BlueClipboard();