This commit is contained in:
Marcos Rodriguez Velez 2024-10-26 12:30:21 -04:00
parent 38c0ffce47
commit 0cec04c83a
4 changed files with 85 additions and 59 deletions

View File

@ -7,6 +7,9 @@ import { scanQrHelper } from '../helpers/scan-qr';
import { showFilePickerAndReadFile, showImagePickerAndReadImage } from '../blue_modules/fs';
import presentAlert from './Alert';
import { useTheme } from './themes';
import RNQRGenerator from 'rn-qr-generator';
import { CommonToolTipActions } from '../typings/CommonToolTipActions';
import { useSettings } from '../hooks/context/useSettings';
interface AddressInputScanButtonProps {
isLoading: boolean;
@ -24,6 +27,7 @@ export const AddressInputScanButton = ({
onChangeText,
}: AddressInputScanButtonProps) => {
const { colors } = useTheme();
const { isClipboardGetContentEnabled } = useSettings();
const stylesHook = StyleSheet.create({
scan: {
backgroundColor: colors.scanLabel,
@ -36,13 +40,28 @@ export const AddressInputScanButton = ({
const toolTipOnPress = useCallback(async () => {
await scanButtonTapped();
Keyboard.dismiss();
if (launchedBy) scanQrHelper(launchedBy).then(value => onBarScanned({ data: value }));
if (launchedBy) scanQrHelper(launchedBy, true).then(value => onBarScanned({ data: value }));
}, [launchedBy, onBarScanned, scanButtonTapped]);
const actions = useMemo(() => {
const availableActions = [
CommonToolTipActions.ScanQR,
CommonToolTipActions.ChoosePhoto,
CommonToolTipActions.ImportFile,
{
...CommonToolTipActions.PasteFromClipboard,
hidden: !isClipboardGetContentEnabled,
},
];
return availableActions;
}, [isClipboardGetContentEnabled]);
const onMenuItemPressed = useCallback(
(action: string) => {
async (action: string) => {
if (onBarScanned === undefined) throw new Error('onBarScanned is required');
switch (action) {
case actionKeys.ScanQR:
case CommonToolTipActions.ScanQR.id:
scanButtonTapped();
if (launchedBy) {
scanQrHelper(launchedBy)
@ -51,15 +70,46 @@ export const AddressInputScanButton = ({
presentAlert({ message: error.message });
});
}
break;
case actionKeys.CopyFromClipboard:
Clipboard.getString()
.then(onChangeText)
.catch(error => {
presentAlert({ message: error.message });
case CommonToolTipActions.PasteFromClipboard.id:
try {
let getImage: string | null = null;
if (Platform.OS === 'android') {
getImage = await Clipboard.getImage();
} else {
const hasImage = await Clipboard.hasImage();
if (hasImage) {
getImage = await Clipboard.getImageJPG();
}
}
if (getImage) {
try {
const base64Data = getImage.replace(/^data:image\/jpeg;base64,/, '');
const values = await RNQRGenerator.detect({
base64: base64Data,
});
if (values && values.values.length > 0) {
onChangeText(values.values[0]);
} else {
presentAlert({ message: loc.send.qr_error_no_qrcode });
}
} catch (error) {
presentAlert({ message: (error as Error).message });
}
} else {
const clipboardText = await Clipboard.getString();
onChangeText(clipboardText);
}
} catch (error) {
presentAlert({ message: (error as Error).message });
}
break;
case actionKeys.ChoosePhoto:
case CommonToolTipActions.ChoosePhoto.id:
showImagePickerAndReadImage()
.then(value => {
if (value) {
@ -70,7 +120,7 @@ export const AddressInputScanButton = ({
presentAlert({ message: error.message });
});
break;
case actionKeys.ImportFile:
case CommonToolTipActions.ImportFile.id:
showFilePickerAndReadFile()
.then(value => {
if (value.data) {
@ -124,48 +174,3 @@ const styles = StyleSheet.create({
marginLeft: 4,
},
});
const actionKeys = {
ScanQR: 'scan_qr',
CopyFromClipboard: 'copy_from_clipboard',
ChoosePhoto: 'choose_photo',
ImportFile: 'import_file',
};
const actionIcons = {
ScanQR: {
iconValue: Platform.OS === 'ios' ? 'qrcode' : 'ic_menu_camera',
},
ImportFile: {
iconValue: 'doc',
},
ChoosePhoto: {
iconValue: Platform.OS === 'ios' ? 'photo' : 'ic_menu_gallery',
},
Clipboard: {
iconValue: Platform.OS === 'ios' ? 'doc' : 'ic_menu_file',
},
};
const actions = [
{
id: actionKeys.ScanQR,
text: loc.wallets.list_long_scan,
icon: actionIcons.ScanQR,
},
{
id: actionKeys.CopyFromClipboard,
text: loc.wallets.list_long_clipboard,
icon: actionIcons.Clipboard,
},
{
id: actionKeys.ChoosePhoto,
text: loc.wallets.list_long_choose,
icon: actionIcons.ChoosePhoto,
},
{
id: actionKeys.ImportFile,
text: loc.wallets.import_file,
icon: actionIcons.ImportFile,
},
];

View File

@ -22,8 +22,8 @@
"close": "Close",
"change_input_currency": "Change input currency",
"refresh": "Refresh",
"pick_image": "Choose image from library",
"pick_file": "Choose a file",
"pick_image": "Choose from library",
"pick_file": "Choose file",
"enter_amount": "Enter amount",
"qr_custom_input_button": "Tap 10 times to enter custom input",
"unlock": "Unlock",

View File

@ -164,7 +164,7 @@ jest.mock('react-native-ios-context-menu', () => {
});
jest.mock('rn-qr-generator', () => ({
detect: jest.fn((uri) => {
detect: jest.fn(uri => {
if (uri === 'invalid-image') {
return Promise.reject(new Error('Failed to decode QR code'));
}

View File

@ -21,10 +21,13 @@ const keys = {
PaymentsCode: 'paymentsCode',
ResetToDefault: 'resetToDefault',
ClearHistory: 'clearHistory',
ScanQR: 'scan_qr',
RemoveAllRecipients: 'RemoveAllRecipients',
AddRecipient: 'AddRecipient',
RemoveRecipient: 'RemoveRecipient',
PasteFromClipboard: 'pasteFromClipboard',
ChoosePhoto: 'choosePhoto',
ImportFile: 'importFile',
};
const icons = {
@ -80,6 +83,9 @@ const icons = {
AddRecipient: { iconValue: 'person.badge.plus' },
RemoveRecipient: { iconValue: 'person.badge.minus' },
PasteFromClipboard: { iconValue: 'document.on.clipboard' },
ScanQR: { iconValue: 'qrcode.viewfinder' },
ChoosePhoto: { iconValue: 'photo.on.rectangle' },
ImportFile: { iconValue: 'doc.badge.plus' },
};
export const CommonToolTipActions = {
@ -206,4 +212,19 @@ export const CommonToolTipActions = {
text: loc.transactions.details_copy_amount,
icon: icons.PasteFromClipboard,
},
ScanQR: {
id: keys.ScanQR,
text: loc.send.details_scan,
icon: icons.ScanQR,
},
ChoosePhoto: {
id: keys.ChoosePhoto,
text: loc._.pick_image,
icon: icons.ChoosePhoto,
},
ImportFile: {
id: keys.ImportFile,
text: loc.wallets.import_file,
icon: icons.ImportFile,
},
};