ADD: Payment code in receive screen

This commit is contained in:
Marcos Rodriguez Velez 2024-06-08 11:04:26 -04:00 committed by Overtorment
parent fe943f822c
commit 6effb0da31

View file

@ -8,6 +8,7 @@ import {
Platform, Platform,
ScrollView, ScrollView,
StyleSheet, StyleSheet,
Text,
TextInput, TextInput,
View, View,
} from 'react-native'; } from 'react-native';
@ -32,6 +33,7 @@ import loc, { formatBalance } from '../../loc';
import { BitcoinUnit, Chain } from '../../models/bitcoinUnits'; import { BitcoinUnit, Chain } from '../../models/bitcoinUnits';
import { SuccessView } from '../send/success'; import { SuccessView } from '../send/success';
import { useStorage } from '../../hooks/context/useStorage'; import { useStorage } from '../../hooks/context/useStorage';
import { AddressTypeTabs, TABS } from '../../components/addresses/AddressTypeTabs';
import { HandOffActivityType } from '../../components/types'; import { HandOffActivityType } from '../../components/types';
const ReceiveDetails = () => { const ReceiveDetails = () => {
@ -47,6 +49,7 @@ const ReceiveDetails = () => {
const [showPendingBalance, setShowPendingBalance] = useState(false); const [showPendingBalance, setShowPendingBalance] = useState(false);
const [showConfirmedBalance, setShowConfirmedBalance] = useState(false); const [showConfirmedBalance, setShowConfirmedBalance] = useState(false);
const [showAddress, setShowAddress] = useState(false); const [showAddress, setShowAddress] = useState(false);
const [currentTab, setCurrentTab] = useState(TABS.EXTERNAL);
const { goBack, setParams } = useExtendedNavigation(); const { goBack, setParams } = useExtendedNavigation();
const { colors } = useTheme(); const { colors } = useTheme();
const [intervalMs, setIntervalMs] = useState(5000); const [intervalMs, setIntervalMs] = useState(5000);
@ -85,6 +88,9 @@ const ReceiveDetails = () => {
modalButton: { modalButton: {
backgroundColor: colors.modalButton, backgroundColor: colors.modalButton,
}, },
tip: {
backgroundColor: colors.ballOutgoingExpired,
},
}); });
useEffect(() => { useEffect(() => {
@ -174,7 +180,7 @@ const ReceiveDetails = () => {
const renderConfirmedBalance = () => { const renderConfirmedBalance = () => {
return ( return (
<ScrollView style={stylesHook.rootBackgroundColors} centerContent keyboardShouldPersistTaps="always"> <ScrollView style={stylesHook.rootBackgroundColor} centerContent keyboardShouldPersistTaps="always">
<View style={styles.scrollBody}> <View style={styles.scrollBody}>
{isCustom && ( {isCustom && (
<> <>
@ -221,6 +227,16 @@ const ReceiveDetails = () => {
return true; return true;
}; };
const setAddressBIP21Encoded = useCallback(
addr => {
const newBip21encoded = DeeplinkSchemaMatch.bip21encode(addr);
setParams({ address: addr });
setBip21encoded(newBip21encoded);
setShowAddress(true);
},
[setParams],
);
useEffect(() => { useEffect(() => {
BackHandler.addEventListener('hardwareBackPress', handleBackButton); BackHandler.addEventListener('hardwareBackPress', handleBackButton);
@ -262,7 +278,6 @@ const ReceiveDetails = () => {
title={loc.receive.details_setAmount} title={loc.receive.details_setAmount}
onPress={showCustomAmountModal} onPress={showCustomAmountModal}
/> />
<Button onPress={handleShareButtonPressed} title={loc.receive.details_share} />
</BlueCard> </BlueCard>
</View> </View>
{renderCustomAmountModal()} {renderCustomAmountModal()}
@ -273,7 +288,6 @@ const ReceiveDetails = () => {
const obtainWalletAddress = useCallback(async () => { const obtainWalletAddress = useCallback(async () => {
console.log('receive/details - componentDidMount'); console.log('receive/details - componentDidMount');
wallet.setUserHasSavedExport(true); wallet.setUserHasSavedExport(true);
await saveToDisk();
let newAddress; let newAddress;
if (address) { if (address) {
setAddressBIP21Encoded(address); setAddressBIP21Encoded(address);
@ -308,15 +322,7 @@ const ReceiveDetails = () => {
await Notifications.tryToObtainPermissions(receiveAddressButton); await Notifications.tryToObtainPermissions(receiveAddressButton);
Notifications.majorTomToGroundControl([newAddress], [], []); Notifications.majorTomToGroundControl([newAddress], [], []);
} }
// eslint-disable-next-line react-hooks/exhaustive-deps }, [wallet, saveToDisk, address, setAddressBIP21Encoded, isElectrumDisabled, sleep]);
}, []);
const setAddressBIP21Encoded = addr => {
const newBip21encoded = DeeplinkSchemaMatch.bip21encode(addr);
setParams({ address: addr });
setBip21encoded(newBip21encoded);
setShowAddress(true);
};
useFocusEffect( useFocusEffect(
useCallback(() => { useCallback(() => {
@ -330,8 +336,7 @@ const ReceiveDetails = () => {
return () => { return () => {
task.cancel(); task.cancel();
}; };
// eslint-disable-next-line react-hooks/exhaustive-deps }, [wallet, address, obtainWalletAddress, setAddressBIP21Encoded]),
}, [wallet]),
); );
const dismissCustomAmountModal = () => { const dismissCustomAmountModal = () => {
@ -424,14 +429,56 @@ const ReceiveDetails = () => {
} }
}; };
const renderTabContent = () => {
const qrValue = currentTab === TABS.EXTERNAL ? address : wallet.getBIP47PaymentCode();
if (currentTab === TABS.EXTERNAL) {
return (
<View style={styles.container}>
{!address && <Text>{loc.bip47.not_found}</Text>}
{address && renderReceiveDetails()}
</View>
);
} else {
return (
<View style={styles.container}>
{!qrValue && <Text>{loc.bip47.not_found}</Text>}
{qrValue && (
<>
<View style={[styles.tip, stylesHook.tip]}>
<Text style={{ color: colors.foregroundColor }}>{loc.receive.bip47_explanation}</Text>
</View>
<QRCodeComponent value={qrValue} />
<CopyTextToClipboard text={qrValue} truncated={false} />
</>
)}
</View>
);
}
};
return ( return (
<View style={[styles.root, stylesHook.root]}> <View style={[styles.root, stylesHook.root]}>
{wallet.isBIP47Enabled() && (
<View style={styles.tabsContainer}>
<AddressTypeTabs
currentTab={currentTab}
setCurrentTab={setCurrentTab}
customTabText={{ EXTERNAL: 'Address', INTERNAL: 'Payment Code' }}
/>
</View>
)}
{renderTabContent()}
<View style={styles.share}>
<BlueCard>
<Button onPress={handleShareButtonPressed} title={loc.receive.details_share} />
</BlueCard>
</View>
{address !== undefined && showAddress && ( {address !== undefined && showAddress && (
<HandOffComponent title={loc.send.details_address} type={HandOffActivityType.ReceiveOnchain} userInfo={{ address }} /> <HandOffComponent title={loc.send.details_address} type={HandOffActivityType.ReceiveOnchain} userInfo={{ address }} />
)} )}
{showConfirmedBalance ? renderConfirmedBalance() : null} {showConfirmedBalance ? renderConfirmedBalance() : null}
{showPendingBalance ? renderPendingBalance() : null} {showPendingBalance ? renderPendingBalance() : null}
{showAddress ? renderReceiveDetails() : null}
{!showAddress && !showPendingBalance && !showConfirmedBalance ? <BlueLoading /> : null} {!showAddress && !showPendingBalance && !showConfirmedBalance ? <BlueLoading /> : null}
</View> </View>
); );
@ -462,6 +509,9 @@ const styles = StyleSheet.create({
flexGrow: 1, flexGrow: 1,
justifyContent: 'space-between', justifyContent: 'space-between',
}, },
tabsContainer: {
height: 65,
},
scrollBody: { scrollBody: {
marginTop: 32, marginTop: 32,
flexGrow: 1, flexGrow: 1,
@ -500,6 +550,17 @@ const styles = StyleSheet.create({
marginHorizontal: 8, marginHorizontal: 8,
minHeight: 33, minHeight: 33,
}, },
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
tip: {
marginHorizontal: 16,
borderRadius: 12,
padding: 16,
marginVertical: 24,
},
}); });
export default ReceiveDetails; export default ReceiveDetails;