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

View File

@ -3,7 +3,7 @@ import { StyleSheet, Text, View } from "react-native";
import Button from "../Button";
type InitializationScreenProps = {
onPressFunc: () => void;
onPressFunc: (pin: string) => void;
};
const InitializationScreen: FC<InitializationScreenProps> = props => {
@ -13,7 +13,7 @@ const InitializationScreen: FC<InitializationScreenProps> = props => {
<View>
<View>
<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>