fix step flow

This commit is contained in:
Ksenia Lebedeva 2024-09-12 09:58:19 +02:00
parent deca2bea34
commit 1a5e6c9a37
2 changed files with 16 additions and 5 deletions

View File

@ -18,15 +18,17 @@ enum Step {
const Main = () => { const Main = () => {
const isDarkMode = useColorScheme() === 'dark'; const isDarkMode = useColorScheme() === 'dark';
const [isModalVisible, setIsModalVisible] = useState<boolean>(false); const [isModalVisible, setIsModalVisible] = useState<boolean>(false);
const didMount = useRef(false);
const [step, setStep] = useState(Step.Discovery); const [step, setStep] = useState(Step.Discovery);
const [pin, setPin] = useState(null || String); const [pin, setPin] = useState(null || String);
const didMount = useRef(false);
const stepRef = useRef(step);
const keycardConnectHandler = async () => { const keycardConnectHandler = async () => {
try { try {
const appInfo = await Keycard.getApplicationInfo(); const appInfo = await Keycard.getApplicationInfo();
switch (step) { switch (stepRef.current) {
case Step.Discovery: case Step.Discovery:
if (appInfo["initialized?"]) { if (appInfo["initialized?"]) {
if (appInfo["has-master-key?"]) { if (appInfo["has-master-key?"]) {
@ -39,6 +41,7 @@ const Main = () => {
} }
break; break;
case Step.Initialization: case Step.Initialization:
await Keycard.init(pin);
setStep(Step.Loading); setStep(Step.Loading);
break; break;
case Step.Loading: case Step.Loading:
@ -59,9 +62,12 @@ const Main = () => {
console.log(err); console.log(err);
} }
await Keycard.stopNFC("");
setIsModalVisible(false); setIsModalVisible(false);
} }
useEffect(() => { useEffect(() => {
stepRef.current = step;
if (!didMount.current) { if (!didMount.current) {
didMount.current = true; didMount.current = true;
DeviceEventEmitter.addListener("keyCardOnConnected", keycardConnectHandler); DeviceEventEmitter.addListener("keyCardOnConnected", keycardConnectHandler);
@ -87,10 +93,15 @@ const Main = () => {
} }
} }
const initPin = async (p: string) => {
setPin(p);
return connectCard();
}
return ( return (
<SafeAreaView style={[backgroundStyle, styles.container]}> <SafeAreaView style={[backgroundStyle, styles.container]}>
{step == Step.Discovery && <DiscoveryScreen onPressFunc={connectCard}></DiscoveryScreen>} {step == Step.Discovery && <DiscoveryScreen onPressFunc={connectCard}></DiscoveryScreen>}
{step == Step.Initialization && <InitializationScreen onPressFunc={connectCard}></InitializationScreen>} {step == Step.Initialization && <InitializationScreen onPressFunc={initPin} ></InitializationScreen>}
<NFCModal isVisible={isModalVisible} onChangeFunc={setIsModalVisible}></NFCModal> <NFCModal isVisible={isModalVisible} onChangeFunc={setIsModalVisible}></NFCModal>
</SafeAreaView> </SafeAreaView>
); );

View File

@ -3,7 +3,7 @@ import { StyleSheet, Text, View } from "react-native";
import Button from "../Button"; import Button from "../Button";
type InitializationScreenProps = { type InitializationScreenProps = {
onPressFunc: () => void; onPressFunc: (pin: string) => void;
}; };
const InitializationScreen: FC<InitializationScreenProps> = props => { const InitializationScreen: FC<InitializationScreenProps> = props => {
@ -13,7 +13,7 @@ const InitializationScreen: FC<InitializationScreenProps> = props => {
<View> <View>
<View> <View>
<Text style={styles.heading}> Hello world</Text> <Text style={styles.heading}> Hello world</Text>
<Button label="Next" disabled={false} btnColor="#4A646C" btnWidth="100%" onChangeFunc={onPressFunc} btnJustifyContent='center'></Button> <Button label="Next" disabled={false} btnColor="#4A646C" btnWidth="100%" onChangeFunc={() => {onPressFunc("000000")}} btnJustifyContent='center'></Button>
</View> </View>
<View> <View>
</View> </View>