REF: BlueAlertWalletExportReminder

This commit is contained in:
Marcos Rodriguez Velez 2024-03-24 16:29:58 -04:00
parent b3beb795dd
commit 8cb565f928
No known key found for this signature in database
GPG Key ID: 6030B2F48CCE86D7
6 changed files with 72 additions and 81 deletions

View File

@ -4,7 +4,6 @@ import PropTypes from 'prop-types';
import { Icon, Text, Header } from 'react-native-elements';
import {
ActivityIndicator,
Alert,
Animated,
Dimensions,
Image,
@ -18,7 +17,6 @@ import {
View,
I18nManager,
ImageBackground,
findNodeHandle,
} from 'react-native';
import Clipboard from '@react-native-clipboard/clipboard';
import NetworkTransactionFees, { NetworkTransactionFee, NetworkTransactionFeeType } from './models/networkTransactionFees';
@ -27,8 +25,6 @@ import { BlueCurrentTheme, useTheme } from './components/themes';
import PlusIcon from './components/icons/PlusIcon';
import loc, { formatStringAddTwoWhiteSpaces } from './loc';
import SafeArea from './components/SafeArea';
import { isDesktop } from './blue_modules/environment';
import ActionSheet from './screen/ActionSheet';
const { height, width } = Dimensions.get('window');
const aspectRatio = height / width;
@ -196,39 +192,6 @@ export const BlueButtonLink = forwardRef((props, ref) => {
);
});
export const BlueAlertWalletExportReminder = ({ onSuccess = () => {}, onFailure, anchor }) => {
if (isDesktop) {
ActionSheet.showActionSheetWithOptions(
{
title: loc.wallets.details_title, // Changed from loc.send.header to loc.wallets.details_title
message: loc.pleasebackup.ask,
options: [loc.pleasebackup.ask_yes, loc.pleasebackup.ask_no],
anchor: findNodeHandle(anchor), // Kept the same for context
},
buttonIndex => {
switch (buttonIndex) {
case 0:
onSuccess(); // Assuming the first button (yes) triggers onSuccess
break;
case 1:
onFailure(); // Assuming the second button (no) triggers onFailure
break;
}
},
);
} else {
Alert.alert(
loc.wallets.details_title,
loc.pleasebackup.ask,
[
{ text: loc.pleasebackup.ask_yes, onPress: onSuccess, style: 'cancel' },
{ text: loc.pleasebackup.ask_no, onPress: onFailure },
],
{ cancelable: false },
);
}
};
export const BluePrivateBalance = () => {
return (
<View style={{ flexDirection: 'row', alignItems: 'center', marginTop: 13, borderRadius: 9 }}>

View File

@ -0,0 +1,16 @@
import { Alert } from 'react-native';
import loc from '../loc';
export const presentWalletExportReminder = (): Promise<void> => {
return new Promise<void>((resolve, reject) => {
Alert.alert(
loc.wallets.details_title,
loc.pleasebackup.ask,
[
{ text: loc.pleasebackup.ask_yes, onPress: () => resolve(), style: 'default' },
{ text: loc.pleasebackup.ask_no, onPress: () => reject(new Error('User has denied saving the wallet backup.')), style: 'cancel' },
],
{ cancelable: false },
);
});
};

View File

@ -1,16 +1,21 @@
import { useNavigation, NavigationProp, ParamListBase } from '@react-navigation/native';
import Biometric from '../class/biometrics';
import { navigationRef } from '../NavigationService';
import { BlueStorageContext } from '../blue_modules/storage-context';
import { useContext } from 'react';
import { presentWalletExportReminder } from '../helpers/presentWalletExportReminder';
export const useExtendedNavigation = (): NavigationProp<ParamListBase> => {
const originalNavigation = useNavigation<NavigationProp<ParamListBase>>();
const { wallets, saveToDisk } = useContext(BlueStorageContext);
const enhancedNavigate: any = (screenOrOptions: any, params?: any) => {
const enhancedNavigate: NavigationProp<ParamListBase>['navigate'] = (screenOrOptions: any, params?: any) => {
let screenName: string;
if (typeof screenOrOptions === 'string') {
screenName = screenOrOptions;
} else if (typeof screenOrOptions === 'object' && 'name' in screenOrOptions) {
screenName = screenOrOptions.name;
params = screenOrOptions.params; // Assign params from object if present
} else {
throw new Error('Invalid navigation options');
}
@ -21,33 +26,55 @@ export const useExtendedNavigation = (): NavigationProp<ParamListBase> => {
'ViewEditMultisigCosignersRoot',
'ExportMultisigCoordinationSetupRoot',
].includes(screenName);
const requiresWalletExportIsSaved = ['ReceiveDetailsRoot'].includes(screenName);
const proceedWithNavigation = () => {
if (navigationRef.current?.isReady()) {
typeof screenOrOptions === 'string'
? originalNavigation.navigate(screenOrOptions, params)
: originalNavigation.navigate(screenOrOptions);
: originalNavigation.navigate(screenName, params); // Fixed to use screenName and params
}
};
if (requiresBiometrics) {
Biometric.isBiometricUseEnabled().then(isBiometricsEnabled => {
(async () => {
if (requiresBiometrics) {
const isBiometricsEnabled = await Biometric.isBiometricUseEnabled();
if (isBiometricsEnabled) {
Biometric.unlockWithBiometrics().then(isAuthenticated => {
if (isAuthenticated) {
proceedWithNavigation();
} else {
console.error('Biometric authentication failed');
}
});
} else {
console.warn('Biometric authentication is not enabled');
proceedWithNavigation();
const isAuthenticated = await Biometric.unlockWithBiometrics();
if (isAuthenticated) {
proceedWithNavigation();
return; // Ensure the function exits if this path is taken
} else {
console.error('Biometric authentication failed');
// Decide if navigation should proceed or not after failed authentication
}
}
});
} else {
}
if (requiresWalletExportIsSaved) {
console.log('Checking if wallet export is saved');
const walletID = params?.params ? params.params.walletID : undefined;
if (!walletID) {
proceedWithNavigation();
return;
}
const wallet = wallets.find(w => w.getID() === walletID);
if (wallet && !wallet.getUserHasSavedExport()) {
await presentWalletExportReminder()
.then(() => {
wallet.setUserHasSavedExport(true);
saveToDisk().finally(() => proceedWithNavigation());
})
.catch(() => {
originalNavigation.navigate('WalletExportRoot', {
screen: 'WalletExport',
params: { walletID },
});
});
return; // Prevent proceeding with the original navigation if the reminder is shown
}
}
proceedWithNavigation();
}
})();
};
return {

View File

@ -10,7 +10,7 @@ import {
TextInput,
View,
} from 'react-native';
import { useNavigation, useRoute, useFocusEffect } from '@react-navigation/native';
import { useRoute, useFocusEffect } from '@react-navigation/native';
import Share from 'react-native-share';
import QRCodeComponent from '../../components/QRCodeComponent';
import {
@ -19,7 +19,6 @@ import {
BlueButtonLink,
BlueText,
BlueSpacing20,
BlueAlertWalletExportReminder,
BlueCard,
BlueSpacing40,
} from '../../BlueComponents';
@ -39,6 +38,7 @@ import { useTheme } from '../../components/themes';
import Button from '../../components/Button';
import triggerHapticFeedback, { HapticFeedbackTypes } from '../../blue_modules/hapticFeedback';
import { fiatToBTC, satoshiToBTC } from '../../blue_modules/currency';
import { useExtendedNavigation } from '../../hooks/useExtendedNavigation';
const ReceiveDetails = () => {
const { walletID, address } = useRoute().params;
@ -53,7 +53,7 @@ const ReceiveDetails = () => {
const [showPendingBalance, setShowPendingBalance] = useState(false);
const [showConfirmedBalance, setShowConfirmedBalance] = useState(false);
const [showAddress, setShowAddress] = useState(false);
const { navigate, goBack, setParams } = useNavigation();
const { goBack, setParams } = useExtendedNavigation();
const { colors } = useTheme();
const [intervalMs, setIntervalMs] = useState(5000);
const [eta, setEta] = useState('');
@ -341,22 +341,7 @@ const ReceiveDetails = () => {
useCallback(() => {
const task = InteractionManager.runAfterInteractions(async () => {
if (wallet) {
if (!wallet.getUserHasSavedExport()) {
BlueAlertWalletExportReminder({
onSuccess: obtainWalletAddress,
onFailure: () => {
navigate('WalletExportRoot', {
screen: 'WalletExport',
params: {
walletID: wallet.getID(),
},
});
},
anchor: receiveAddressButton.current,
});
} else {
obtainWalletAddress();
}
obtainWalletAddress();
} else if (!wallet && address) {
setAddressBIP21Encoded(address);
}

View File

@ -17,7 +17,6 @@ import {
import { Icon } from 'react-native-elements';
import { useRoute, useFocusEffect } from '@react-navigation/native';
import { Chain } from '../../models/bitcoinUnits';
import { BlueAlertWalletExportReminder } from '../../BlueComponents';
import WalletGradient from '../../class/wallet-gradient';
import navigationStyle from '../../components/navigationStyle';
import { LightningCustodianWallet, LightningLdkWallet, MultisigHDWallet, WatchOnlyWallet } from '../../class';
@ -36,6 +35,7 @@ import { scanQrHelper } from '../../helpers/scan-qr';
import { useTheme } from '../../components/themes';
import triggerHapticFeedback, { HapticFeedbackTypes } from '../../blue_modules/hapticFeedback';
import { useExtendedNavigation } from '../../hooks/useExtendedNavigation';
import { presentWalletExportReminder } from '../../helpers/presentWalletExportReminder';
const fs = require('../../blue_modules/fs');
const BlueElectrum = require('../../blue_modules/BlueElectrum');
@ -519,20 +519,20 @@ const WalletTransactions = ({ navigation }) => {
if (wallet.getUserHasSavedExport()) {
onManageFundsPressed({ id });
} else {
BlueAlertWalletExportReminder({
onSuccess: async () => {
presentWalletExportReminder()
.then(async () => {
wallet.setUserHasSavedExport(true);
await saveToDisk();
onManageFundsPressed({ id });
},
onFailure: () =>
})
.catch(() => {
navigate('WalletExportRoot', {
screen: 'WalletExport',
params: {
walletID: wallet.getID(),
},
}),
});
});
});
}
}
}}

View File

@ -2,7 +2,7 @@ const fs = require('fs');
const path = require('path');
const mainLocFile = './loc/en.json';
const dirsToInterate = ['components', 'screen', 'blue_modules', 'class'];
const dirsToInterate = ['components', 'screen', 'blue_modules', 'class', 'hooks', 'helpers'];
const addFiles = ['BlueComponents.js', 'App.js', 'BlueApp.ts', 'Navigation.tsx'];
const allowedLocPrefixes = ['loc.lnurl_auth', 'loc.units'];