REF: BlueClipboard to not need prefix

This commit is contained in:
Marcos Rodriguez Velez 2024-11-10 10:25:39 -04:00
parent 0688bde325
commit f77df0e949
5 changed files with 61 additions and 76 deletions

View File

@ -1,43 +1,32 @@
import { useAsyncStorage } from '@react-native-async-storage/async-storage';
import AsyncStorage from '@react-native-async-storage/async-storage';
import Clipboard from '@react-native-clipboard/clipboard';
const BlueClipboard = () => {
const STORAGE_KEY = 'ClipboardReadAllowed';
const { getItem, setItem } = useAsyncStorage(STORAGE_KEY);
const STORAGE_KEY: string = 'ClipboardReadAllowed';
const isReadClipboardAllowed = async () => {
try {
const clipboardAccessAllowed = await getItem();
if (clipboardAccessAllowed === null) {
await setItem(JSON.stringify(true));
return true;
}
return !!JSON.parse(clipboardAccessAllowed);
} catch {
await setItem(JSON.stringify(true));
export const isReadClipboardAllowed = async (): Promise<boolean> => {
try {
const clipboardAccessAllowed = await AsyncStorage.getItem(STORAGE_KEY);
if (clipboardAccessAllowed === null) {
await AsyncStorage.setItem(STORAGE_KEY, JSON.stringify(true));
return true;
}
};
const setReadClipboardAllowed = (value: boolean) => {
setItem(JSON.stringify(!!value));
};
const getClipboardContent = async () => {
const isAllowed = await isReadClipboardAllowed();
const hasString = (await Clipboard.hasString()) || false;
if (isAllowed && hasString) {
return Clipboard.getString();
} else {
return '';
}
};
return {
isReadClipboardAllowed,
setReadClipboardAllowed,
getClipboardContent,
};
return !!JSON.parse(clipboardAccessAllowed);
} catch {
await AsyncStorage.setItem(STORAGE_KEY, JSON.stringify(true));
return true;
}
};
export default BlueClipboard;
export const setReadClipboardAllowed = (value: boolean): Promise<void> => {
return AsyncStorage.setItem(STORAGE_KEY, JSON.stringify(!!value));
};
export const getClipboardContent = async (): Promise<string | undefined> => {
const isAllowed = await isReadClipboardAllowed();
const hasString = (await Clipboard.hasString()) || false;
if (isAllowed && hasString) {
return Clipboard.getString();
} else {
return undefined;
}
};

View File

@ -4,7 +4,7 @@ import { CommonActions } from '@react-navigation/native';
import React, { lazy, Suspense, useCallback, useEffect, useRef } from 'react';
import { AppState, AppStateStatus, Linking } from 'react-native';
import A from '../blue_modules/analytics';
import BlueClipboard from '../blue_modules/clipboard';
import { getClipboardContent } from '../blue_modules/clipboard';
import { updateExchangeRate } from '../blue_modules/currency';
import triggerHapticFeedback, { HapticFeedbackTypes } from '../blue_modules/hapticFeedback';
import Notifications from '../blue_modules/notifications';
@ -157,27 +157,26 @@ const CompanionDelegates = () => {
const showClipboardAlert = useCallback(
({ contentType }: { contentType: undefined | string }) => {
triggerHapticFeedback(HapticFeedbackTypes.ImpactLight);
BlueClipboard()
.getClipboardContent()
.then(clipboard => {
ActionSheet.showActionSheetWithOptions(
{
title: loc._.clipboard,
message: contentType === ClipboardContentType.BITCOIN ? loc.wallets.clipboard_bitcoin : loc.wallets.clipboard_lightning,
options: [loc._.cancel, loc._.continue],
cancelButtonIndex: 0,
},
buttonIndex => {
switch (buttonIndex) {
case 0:
break;
case 1:
handleOpenURL({ url: clipboard });
break;
}
},
);
});
getClipboardContent().then(clipboard => {
if (!clipboard) return;
ActionSheet.showActionSheetWithOptions(
{
title: loc._.clipboard,
message: contentType === ClipboardContentType.BITCOIN ? loc.wallets.clipboard_bitcoin : loc.wallets.clipboard_lightning,
options: [loc._.cancel, loc._.continue],
cancelButtonIndex: 0,
},
buttonIndex => {
switch (buttonIndex) {
case 0:
break;
case 1:
handleOpenURL({ url: clipboard });
break;
}
},
);
});
},
[handleOpenURL],
);
@ -190,7 +189,8 @@ const CompanionDelegates = () => {
updateExchangeRate();
const processed = await processPushNotifications();
if (processed) return;
const clipboard = await BlueClipboard().getClipboardContent();
const clipboard = await getClipboardContent();
if (!clipboard) return;
const isAddressFromStoredWallet = wallets.some(wallet => {
if (wallet.chain === Chain.ONCHAIN) {
return wallet.isAddressValid && wallet.isAddressValid(clipboard) && wallet.weOwnAddress(clipboard);

View File

@ -1,7 +1,6 @@
import { useAsyncStorage } from '@react-native-async-storage/async-storage';
import React, { createContext, useCallback, useEffect, useMemo, useState } from 'react';
import DefaultPreference from 'react-native-default-preference';
import BlueClipboard from '../../blue_modules/clipboard';
import { isReadClipboardAllowed, setReadClipboardAllowed } from '../../blue_modules/clipboard';
import { getPreferredCurrency, GROUP_IO_BLUEWALLET, initCurrencyDaemon, PREFERRED_CURRENCY_STORAGE_KEY } from '../../blue_modules/currency';
import { clearUseURv1, isURv1Enabled, setUseURv1 } from '../../blue_modules/ur';
import { BlueApp } from '../../class';
@ -15,6 +14,7 @@ import { TotalWalletsBalanceKey, TotalWalletsBalancePreferredUnit } from '../Tot
import { BLOCK_EXPLORERS, getBlockExplorerUrl, saveBlockExplorer, BlockExplorer, normalizeUrl } from '../../models/blockExplorer';
import * as BlueElectrum from '../../blue_modules/BlueElectrum';
import { isBalanceDisplayAllowed, setBalanceDisplayAllowed } from '../../hooks/useWidgetCommunication';
import AsyncStorage from '@react-native-async-storage/async-storage';
const getDoNotTrackStorage = async (): Promise<boolean> => {
try {
@ -149,7 +149,6 @@ export const SettingsProvider: React.FC<{ children: React.ReactNode }> = React.m
const [selectedBlockExplorer, setSelectedBlockExplorer] = useState<BlockExplorer>(BLOCK_EXPLORERS.default);
const [isElectrumDisabled, setIsElectrumDisabled] = useState<boolean>(true);
const languageStorage = useAsyncStorage(STORAGE_KEY);
const { walletsInitialized } = useStorage();
useEffect(() => {
@ -167,7 +166,7 @@ export const SettingsProvider: React.FC<{ children: React.ReactNode }> = React.m
getIsHandOffUseEnabled().then(handOff => {
setIsHandOffUseEnabledState(handOff);
}),
languageStorage.getItem().then(lang => {
AsyncStorage.getItem(STORAGE_KEY).then(lang => {
setLanguage(lang ?? 'en');
}),
isBalanceDisplayAllowed().then(balanceDisplayAllowed => {
@ -176,11 +175,9 @@ export const SettingsProvider: React.FC<{ children: React.ReactNode }> = React.m
isURv1Enabled().then(urv1Enabled => {
setIsLegacyURv1Enabled(urv1Enabled);
}),
BlueClipboard()
.isReadClipboardAllowed()
.then(clipboardEnabled => {
setIsClipboardGetContentEnabled(clipboardEnabled);
}),
isReadClipboardAllowed().then(clipboardEnabled => {
setIsClipboardGetContentEnabled(clipboardEnabled);
}),
getIsDeviceQuickActionsEnabled().then(quickActionsEnabled => {
setIsQuickActionsEnabled(quickActionsEnabled);
}),
@ -209,7 +206,6 @@ export const SettingsProvider: React.FC<{ children: React.ReactNode }> = React.m
};
loadSettings();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
useEffect(() => {
@ -299,7 +295,7 @@ export const SettingsProvider: React.FC<{ children: React.ReactNode }> = React.m
const setIsClipboardGetContentEnabledStorage = useCallback(async (value: boolean): Promise<void> => {
try {
await BlueClipboard().setReadClipboardAllowed(value);
await setReadClipboardAllowed(value);
setIsClipboardGetContentEnabled(value);
} catch (e) {
console.error('Error setting isClipboardGetContentEnabled:', e);

View File

@ -17,7 +17,6 @@ import {
} from 'react-native';
import { Icon } from '@rneui/themed';
import * as BlueElectrum from '../../blue_modules/BlueElectrum';
import BlueClipboard from '../../blue_modules/clipboard';
import { isDesktop } from '../../blue_modules/environment';
import * as fs from '../../blue_modules/fs';
import triggerHapticFeedback, { HapticFeedbackTypes } from '../../blue_modules/hapticFeedback';
@ -44,6 +43,7 @@ import selectWallet from '../../helpers/select-wallet';
import assert from 'assert';
import useMenuElements from '../../hooks/useMenuElements';
import { useSettings } from '../../hooks/context/useSettings';
import { getClipboardContent } from '../../blue_modules/clipboard';
const buttonFontSize =
PixelRatio.roundToNearestPixel(Dimensions.get('window').width / 26) > 22
@ -293,7 +293,7 @@ const WalletTransactions: React.FC<WalletTransactionsProps> = ({ route }) => {
const _keyExtractor = (_item: any, index: number) => index.toString();
const pasteFromClipboard = async () => {
onBarCodeRead({ data: await BlueClipboard().getClipboardContent() });
onBarCodeRead({ data: await getClipboardContent() });
};
const sendButtonPress = () => {
@ -325,7 +325,7 @@ const WalletTransactions: React.FC<WalletTransactionsProps> = ({ route }) => {
};
const sendButtonLongPress = async () => {
const isClipboardEmpty = (await BlueClipboard().getClipboardContent()).trim().length === 0;
const isClipboardEmpty = (await getClipboardContent())?.trim().length === 0;
const options = [loc._.cancel, loc.wallets.list_long_choose, loc.wallets.list_long_scan];
const cancelButtonIndex = 0;

View File

@ -2,7 +2,7 @@ import React, { useCallback, useEffect, useReducer, useRef } from 'react';
import { useFocusEffect, useIsFocused, useRoute, RouteProp } from '@react-navigation/native';
import { findNodeHandle, Image, InteractionManager, SectionList, StyleSheet, Text, useWindowDimensions, View } from 'react-native';
import A from '../../blue_modules/analytics';
import BlueClipboard from '../../blue_modules/clipboard';
import { getClipboardContent } from '../../blue_modules/clipboard';
import { isDesktop } from '../../blue_modules/environment';
import * as fs from '../../blue_modules/fs';
import triggerHapticFeedback, { HapticFeedbackTypes } from '../../blue_modules/hapticFeedback';
@ -365,11 +365,11 @@ const WalletsList: React.FC = () => {
);
const pasteFromClipboard = useCallback(async () => {
onBarScanned(await BlueClipboard().getClipboardContent());
onBarScanned(await getClipboardContent());
}, [onBarScanned]);
const sendButtonLongPress = useCallback(async () => {
const isClipboardEmpty = (await BlueClipboard().getClipboardContent()).trim().length === 0;
const isClipboardEmpty = (await getClipboardContent())?.trim().length === 0;
const options = [loc._.cancel, loc.wallets.list_long_choose, loc.wallets.list_long_scan];
if (!isClipboardEmpty) {