REF: Use context

This commit is contained in:
Marcos Rodriguez Vélez 2021-08-24 01:00:57 -04:00
parent 681c256c17
commit 34994a9839
10 changed files with 31 additions and 27 deletions

View File

@ -24,6 +24,11 @@ export const BlueStorageProvider = ({ children }) => {
const getLanguageAsyncStorage = useAsyncStorage(loc.LANG).getItem;
const [isHandOffUseEnabled, setIsHandOffUseEnabled] = useState(false);
const [isDrawerListBlurred, _setIsDrawerListBlurred] = useState(false);
const [isElectrumDisabled, setIsElectrumDisabled] = useState(true);
useEffect(() => {
BlueElectrum.isDisabled().then(setIsElectrumDisabled);
}, []);
const setIsHandOffUseEnabledAsyncStorage = value => {
setIsHandOffUseEnabled(value);
@ -232,6 +237,8 @@ export const BlueStorageProvider = ({ children }) => {
setIsDrawerListBlurred,
setDoNotTrack,
isDoNotTrackEnabled,
isElectrumDisabled,
setIsElectrumDisabled,
}}
>
{children}

View File

@ -22,6 +22,10 @@ export default class NetworkTransactionFees {
// eslint-disable-next-line no-async-promise-executor
return new Promise(async resolve => {
try {
const isDisabled = await BlueElectrum.isDisabled();
if (isDisabled) {
throw new Error('Electrum is disabled. Dont attempt to fetch fees');
}
const response = await BlueElectrum.estimateFees();
if (typeof response === 'object') {
const networkFee = new NetworkTransactionFee(response.fast, response.medium, response.slow);

View File

@ -40,7 +40,7 @@ const currency = require('../../blue_modules/currency');
const ReceiveDetails = () => {
const { walletID, address } = useRoute().params;
const { wallets, saveToDisk, sleep } = useContext(BlueStorageContext);
const { wallets, saveToDisk, sleep, isElectrumDisabled } = useContext(BlueStorageContext);
const wallet = wallets.find(w => w.getID() === walletID);
const [customLabel, setCustomLabel] = useState();
const [customAmount, setCustomAmount] = useState();
@ -213,7 +213,7 @@ const ReceiveDetails = () => {
} else {
if (wallet.chain === Chain.ONCHAIN) {
try {
newAddress = await Promise.race([wallet.getAddressAsync(), sleep(1000)]);
if (!isElectrumDisabled) newAddress = await Promise.race([wallet.getAddressAsync(), sleep(1000)]);
} catch (_) {}
if (newAddress === undefined) {
// either sleep expired or getAddressAsync threw an exception

View File

@ -24,7 +24,7 @@ const bitcoin = require('bitcoinjs-lib');
const torrific = require('../../blue_modules/torrific');
const Confirm = () => {
const { wallets, fetchAndSaveWalletTransactions } = useContext(BlueStorageContext);
const { wallets, fetchAndSaveWalletTransactions, isElectrumDisabled } = useContext(BlueStorageContext);
const [isBiometricUseCapableAndEnabled, setIsBiometricUseCapableAndEnabled] = useState(false);
const { params } = useRoute();
const { recipients = [], walletID, fee, memo, tx, satoshiPerByte, psbt } = params;
@ -35,7 +35,6 @@ const Confirm = () => {
const feeSatoshi = new Bignumber(fee).multipliedBy(100000000).toNumber();
const { navigate } = useNavigation();
const { colors } = useTheme();
const [isElectrumDisabled, setIsElectrumDisabled] = useState(true);
const stylesHook = StyleSheet.create({
transactionDetailsTitle: {
color: colors.foregroundColor,
@ -69,7 +68,6 @@ const Confirm = () => {
console.log('send/confirm - useEffect');
console.log('address = ', recipients);
Biometric.isBiometricUseCapableAndEnabled().then(setIsBiometricUseCapableAndEnabled);
BlueElectrum.isDisabled().then(setIsElectrumDisabled);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

View File

@ -38,7 +38,7 @@ const bitcoin = require('bitcoinjs-lib');
const fs = require('../../blue_modules/fs');
const PsbtWithHardwareWallet = () => {
const { txMetadata, fetchAndSaveWalletTransactions } = useContext(BlueStorageContext);
const { txMetadata, fetchAndSaveWalletTransactions, isElectrumDisabled } = useContext(BlueStorageContext);
const navigation = useNavigation();
const route = useRoute();
const { fromWallet, memo, psbt, deepLinkPSBT } = route.params;
@ -46,7 +46,6 @@ const PsbtWithHardwareWallet = () => {
const routeParamsTXHex = route.params.txhex;
const { colors } = useTheme();
const [isLoading, setIsLoading] = useState(false);
const [isElectrumDisabled, setIsElectrumDisabled] = useState(true);
const [txHex, setTxHex] = useState(route.params.txhex);
const openScannerButton = useRef();
@ -114,10 +113,6 @@ const PsbtWithHardwareWallet = () => {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [deepLinkPSBT, routeParamsTXHex]);
useEffect(() => {
BlueElectrum.isDisabled().then(setIsElectrumDisabled);
}, []);
const broadcast = async () => {
setIsLoading(true);
const isBiometricsEnabled = await Biometric.isBiometricUseCapableAndEnabled();

View File

@ -37,10 +37,12 @@ import { BlueCurrentTheme } from '../../components/themes';
import { isTorCapable } from '../../blue_modules/environment';
import ReactNativeHapticFeedback from 'react-native-haptic-feedback';
import WidgetCommunication from '../../blue_modules/WidgetCommunication';
import { BlueStorageContext } from '../../blue_modules/storage-context';
const BlueElectrum = require('../../blue_modules/BlueElectrum');
export default class ElectrumSettings extends Component {
static contextType = BlueStorageContext;
constructor(props) {
super(props);
const server = props?.route?.params?.server;
@ -256,9 +258,11 @@ export default class ElectrumSettings extends Component {
onElectrumConnectionEnabledSwitchValueChangd = async value => {
if (value === true) {
await BlueElectrum.setDisabled(true);
this.context.setIsElectrumDisabled(true);
BlueElectrum.forceDisconnect();
} else {
await BlueElectrum.setDisabled(false);
this.context.setIsElectrumDisabled(false);
BlueElectrum.connectMain();
}
this.setState({ isOfflineMode: value });

View File

@ -126,12 +126,10 @@ export default class CPFP extends Component {
async componentDidMount() {
console.log('transactions/CPFP - componentDidMount');
const isElectrumDisabled = await BlueElectrum.isDisabled();
this.setState({
isLoading: true,
newFeeRate: '',
nonReplaceable: false,
isElectrumDisabled,
});
try {
await this.checkPossibilityOfCPFP();
@ -211,7 +209,7 @@ export default class CPFP extends Component {
>
<Text style={styles.actionText}>{loc.send.create_verify}</Text>
</TouchableOpacity>
<BlueButton disabled={this.state.isElectrumDisabled} onPress={this.broadcast} title={loc.send.confirm_sendNow} />
<BlueButton disabled={this.context.isElectrumDisabled} onPress={this.broadcast} title={loc.send.confirm_sendNow} />
</BlueCard>
</View>
);

View File

@ -29,7 +29,6 @@ import BlueClipboard from '../../blue_modules/clipboard';
import navigationStyle from '../../components/navigationStyle';
import { TransactionListItem } from '../../components/TransactionListItem';
const BlueElectrum = require('../../blue_modules/BlueElectrum');
const scanqrHelper = require('../../helpers/scan-qr');
const A = require('../../blue_modules/analytics');
const fs = require('../../blue_modules/fs');
@ -39,9 +38,15 @@ const WalletsList = () => {
const walletsCarousel = useRef();
const currentWalletIndex = useRef(0);
const colorScheme = useColorScheme();
const { wallets, getTransactions, isImportingWallet, getBalance, refreshAllWalletTransactions, setSelectedWallet } = useContext(
BlueStorageContext,
);
const {
wallets,
getTransactions,
isImportingWallet,
getBalance,
refreshAllWalletTransactions,
setSelectedWallet,
isElectrumDisabled,
} = useContext(BlueStorageContext);
const { width } = useWindowDimensions();
const { colors, scanImage } = useTheme();
const { navigate, setOptions } = useNavigation();
@ -54,7 +59,6 @@ const WalletsList = () => {
const dataSource = getTransactions(null, 10);
const walletsCount = useRef(wallets.length);
const walletActionButtonsRef = useRef();
const [isElectrumDisabled, setIsElectrumDisabled] = useState(true);
const stylesHook = StyleSheet.create({
walletsListWrapper: {
@ -82,7 +86,6 @@ const WalletsList = () => {
useCallback(() => {
verifyBalance();
setSelectedWallet('');
BlueElectrum.isDisabled().then(setIsElectrumDisabled);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []),
);

View File

@ -50,7 +50,7 @@ const buttonFontSize =
: PixelRatio.roundToNearestPixel(Dimensions.get('window').width / 26);
const WalletTransactions = () => {
const { wallets, saveToDisk, setSelectedWallet, walletTransactionUpdateStatus } = useContext(BlueStorageContext);
const { wallets, saveToDisk, setSelectedWallet, walletTransactionUpdateStatus, isElectrumDisabled } = useContext(BlueStorageContext);
const [isLoading, setIsLoading] = useState(false);
const [isManageFundsModalVisible, setIsManageFundsModalVisible] = useState(false);
const { walletID } = useRoute().params;
@ -62,7 +62,6 @@ const WalletTransactions = () => {
const [limit, setLimit] = useState(15);
const [pageSize, setPageSize] = useState(20);
const { setParams, setOptions, navigate } = useNavigation();
const [isElectrumDisabled, setIsElectrumDisabled] = useState(true);
const { colors } = useTheme();
const walletActionButtonsRef = useRef();
@ -161,7 +160,6 @@ const WalletTransactions = () => {
// if description of transaction has been changed we want to show new one
useFocusEffect(
useCallback(() => {
BlueElectrum.isDisabled().then(setIsElectrumDisabled);
setTimeElapsed(prev => prev + 1);
}, []),
);

View File

@ -44,12 +44,11 @@ import { SquareButton } from '../../components/SquareButton';
import { isMacCatalina } from '../../blue_modules/environment';
import { encodeUR } from '../../blue_modules/ur';
const fs = require('../../blue_modules/fs');
const BlueElectrum = require('../../blue_modules/BlueElectrum');
const ViewEditMultisigCosigners = () => {
const hasLoaded = useRef(false);
const { colors } = useTheme();
const { wallets, setWalletsWithNewOrder, setIsDrawerListBlurred } = useContext(BlueStorageContext);
const { wallets, setWalletsWithNewOrder, setIsDrawerListBlurred, isElectrumDisabled } = useContext(BlueStorageContext);
const { navigate, dispatch, goBack, addListener } = useNavigation();
const route = useRoute();
const openScannerButtonRef = useRef();
@ -58,7 +57,6 @@ const ViewEditMultisigCosigners = () => {
const tempWallet = useRef(new MultisigHDWallet());
const [wallet, setWallet] = useState();
const [isLoading, setIsLoading] = useState(true);
const [isElectrumDisabled, setIsElectrumDisabled] = useState(true);
const [isSaveButtonDisabled, setIsSaveButtonDisabled] = useState(true);
const [currentlyEditingCosignerNum, setCurrentlyEditingCosignerNum] = useState(false);
const [isProvideMnemonicsModalVisible, setIsProvideMnemonicsModalVisible] = useState(false);
@ -198,7 +196,6 @@ const ViewEditMultisigCosigners = () => {
useFocusEffect(
useCallback(() => {
// useFocusEffect is called on willAppear (example: when camera dismisses). we want to avoid this.
BlueElectrum.isDisabled().then(setIsElectrumDisabled);
if (hasLoaded.current) return;
setIsLoading(true);