BlueWallet/components/DynamicQRCode.tsx
2026-05-13 14:49:39 -05:00

275 lines
7.5 KiB
TypeScript

import React, { Component } from 'react';
import { Dimensions, StyleSheet, Text, TouchableOpacity, View } from 'react-native';
import { encodeUR } from '../blue_modules/ur';
import { BlueCurrentTheme } from '../components/themes';
import loc from '../loc';
import QRCode from './QRCode';
import { BlueSpacing20 } from './BlueSpacing';
const { height, width } = Dimensions.get('window');
interface DynamicQRCodeProps {
value: string;
walletID?: string;
capacity?: number;
hideControls?: boolean;
}
interface DynamicQRCodeState {
index: number;
total: number;
qrCodeHeight: number;
intervalHandler: ReturnType<typeof setInterval> | number | null;
displayQRCode: boolean;
hideControls?: boolean;
}
export class DynamicQRCode extends Component<DynamicQRCodeProps, DynamicQRCodeState> {
constructor(props: DynamicQRCodeProps) {
super(props);
const qrCodeHeight = height > width ? width - 40 : width / 3;
const qrCodeMaxHeight = 370;
this.state = {
index: 0,
total: 0,
qrCodeHeight: Math.min(qrCodeHeight, qrCodeMaxHeight),
intervalHandler: null,
displayQRCode: true,
};
}
fragments: string[] = [];
componentWillUnmount() {
this.stopAutoMove();
}
componentDidMount() {
const { value, capacity = 175, hideControls = true, walletID } = this.props;
try {
this.fragments = encodeUR(value, capacity, walletID ?? null);
this.setState(
{
total: this.fragments.length,
hideControls,
displayQRCode: true,
},
() => {
this.startAutoMove();
},
);
} catch (e) {
console.log(e);
this.setState({ displayQRCode: false, hideControls });
}
}
moveToNextFragment = () => {
const { index, total } = this.state;
if (index === total - 1) {
this.setState({
index: 0,
});
} else {
this.setState(state => ({
index: state.index + 1,
}));
}
};
forceUseBBQR = () => {
const { value, capacity = 175, hideControls = true, walletID } = this.props;
console.log({ value, capacity, walletID });
try {
this.fragments = encodeUR(value, capacity, walletID ?? null, 'BBQR');
this.setState({
total: this.fragments.length,
displayQRCode: true,
});
} catch (e) {
console.log(e);
this.setState({ displayQRCode: false, hideControls });
}
};
forceUseURv2 = () => {
const { value, capacity = 175, hideControls = true, walletID } = this.props;
console.log({ value, capacity, walletID });
try {
this.fragments = encodeUR(value, capacity, walletID ?? null, 'URv2');
this.setState({
total: this.fragments.length,
displayQRCode: true,
});
} catch (e) {
console.log(e);
this.setState({ displayQRCode: false, hideControls });
}
};
startAutoMove = () => {
if (!this.state.intervalHandler)
this.setState(() => ({
intervalHandler: setInterval(this.moveToNextFragment, 500),
}));
};
stopAutoMove = () => {
clearInterval(this.state.intervalHandler as number);
this.setState(() => ({
intervalHandler: null,
}));
};
moveToPreviousFragment = () => {
const { index, total } = this.state;
if (index > 0) {
this.setState(state => ({
index: state.index - 1,
}));
} else {
this.setState(state => ({
index: total - 1,
}));
}
};
onError = () => {
console.log('Data is too large for QR Code.');
this.setState({ displayQRCode: false });
};
render() {
const currentFragment = this.fragments[this.state.index];
if (!currentFragment && this.state.displayQRCode) {
return (
<View>
<Text>{loc.send.dynamic_init}</Text>
</View>
);
}
return (
<View style={animatedQRCodeStyle.container}>
<TouchableOpacity
accessibilityRole="button"
testID="DynamicCode"
onPress={() => {
this.setState(prevState => ({ hideControls: !prevState.hideControls }));
}}
>
{this.state.displayQRCode && (
<View style={animatedQRCodeStyle.qrcodeContainer}>
<QRCode
isLogoRendered={false}
value={currentFragment.toUpperCase()}
size={this.state.qrCodeHeight}
isMenuAvailable={false}
ecl="L"
onError={this.onError}
/>
</View>
)}
</TouchableOpacity>
{!this.state.hideControls && (
<View style={animatedQRCodeStyle.container}>
<BlueSpacing20 />
<View>
<Text style={animatedQRCodeStyle.text}>
{loc.formatString(loc._.of, { number: this.state.index + 1, total: this.state.total })}
</Text>
</View>
<BlueSpacing20 />
<View style={animatedQRCodeStyle.controller}>
<TouchableOpacity
accessibilityRole="button"
style={[animatedQRCodeStyle.button, animatedQRCodeStyle.buttonPrev]}
onPress={this.moveToPreviousFragment}
>
<Text style={animatedQRCodeStyle.text}>{loc.send.dynamic_prev}</Text>
</TouchableOpacity>
<TouchableOpacity
accessibilityRole="button"
style={[animatedQRCodeStyle.button, animatedQRCodeStyle.buttonStopStart]}
onPress={this.state.intervalHandler ? this.stopAutoMove : this.startAutoMove}
>
<Text style={animatedQRCodeStyle.text}>{this.state.intervalHandler ? loc.send.dynamic_stop : loc.send.dynamic_start}</Text>
</TouchableOpacity>
<TouchableOpacity
accessibilityRole="button"
style={[animatedQRCodeStyle.button, animatedQRCodeStyle.buttonNext]}
onPress={this.moveToNextFragment}
>
<Text style={animatedQRCodeStyle.text}>{loc.send.dynamic_next}</Text>
</TouchableOpacity>
</View>
<View style={animatedQRCodeStyle.controller2}>
<TouchableOpacity accessibilityRole="button" style={animatedQRCodeStyle.buttonUseFormat} onPress={this.forceUseBBQR}>
<Text style={animatedQRCodeStyle.text}>Force use BBQR</Text>
</TouchableOpacity>
<TouchableOpacity accessibilityRole="button" style={animatedQRCodeStyle.buttonUseFormat} onPress={this.forceUseURv2}>
<Text style={animatedQRCodeStyle.text}>Force use URv2</Text>
</TouchableOpacity>
</View>
</View>
)}
</View>
);
}
}
const animatedQRCodeStyle = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
alignItems: 'center',
},
qrcodeContainer: {
alignItems: 'center',
justifyContent: 'center',
},
controller: {
width: '90%',
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
borderRadius: 25,
height: 45,
paddingHorizontal: 18,
},
controller2: {
flexDirection: 'column',
},
button: {
alignItems: 'center',
height: 45,
justifyContent: 'center',
},
buttonPrev: {
width: '25%',
alignItems: 'flex-start',
},
buttonUseFormat: {
alignItems: 'center',
paddingTop: 25,
},
buttonStopStart: {
width: '50%',
},
buttonNext: {
width: '25%',
alignItems: 'flex-end',
},
text: {
fontSize: 14,
color: BlueCurrentTheme.colors.foregroundColor,
fontWeight: 'bold',
},
});