|
1 | 1 | import { createNativeStackNavigator } from '@react-navigation/native-stack'; |
2 | | -import React, { useContext } from 'react'; |
| 2 | +import React, { useContext, useEffect, useState } from 'react'; |
| 3 | +import { Platform } from 'react-native'; |
| 4 | +import NfcManager from 'react-native-nfc-manager'; |
3 | 5 | import { AuthContext } from '../App'; |
4 | 6 | import AuthenticationScreen from '../Auth/AuthenticationScreen'; |
| 7 | +import NfcEnabledScreen from '../Nfc/NfcEnabledScreen'; |
5 | 8 | import NavBar from './NavBar'; |
6 | 9 |
|
7 | 10 | const Stack = createNativeStackNavigator(); |
| 11 | +type NfcState = 'enabled' | 'disabled' | 'unsupported'; |
8 | 12 |
|
9 | 13 | function RootStack() { |
| 14 | + const [nfcEnabled, setNfcEnabled] = useState<NfcState>('disabled'); |
| 15 | + |
| 16 | + useEffect(() => { |
| 17 | + const isEnabled = async () => { |
| 18 | + try { |
| 19 | + const isSupported = await NfcManager.isSupported(); |
| 20 | + if (!isSupported) { |
| 21 | + setNfcEnabled('unsupported'); |
| 22 | + return; |
| 23 | + } |
| 24 | + if (Platform.OS === 'android') { |
| 25 | + const enabled = await NfcManager.isEnabled(); |
| 26 | + setNfcEnabled(enabled ? 'enabled' : 'disabled'); |
| 27 | + } else if (Platform.OS === 'ios') { |
| 28 | + setNfcEnabled('enabled'); |
| 29 | + } else { |
| 30 | + // Platform.OS === "windows" | "macos" | "web" |
| 31 | + console.log('Not a valid platform for NFC'); |
| 32 | + } |
| 33 | + } catch (error) { |
| 34 | + console.error('Error fetching data:', error); |
| 35 | + } |
| 36 | + }; |
| 37 | + isEnabled(); |
| 38 | + }, []); |
| 39 | + |
10 | 40 | const auth = useContext(AuthContext); |
11 | 41 | return ( |
12 | 42 | <Stack.Navigator |
13 | 43 | screenOptions={{ |
14 | 44 | headerShown: false, |
15 | 45 | }} |
16 | 46 | > |
17 | | - {auth?.authenticated ? ( |
| 47 | + {nfcEnabled !== 'enabled' ? ( |
| 48 | + <Stack.Screen name="NfcEnabledScreen"> |
| 49 | + {() => <NfcEnabledScreen nfcEnabled={nfcEnabled} />} |
| 50 | + </Stack.Screen> |
| 51 | + ) : auth?.authenticated ? ( |
18 | 52 | <Stack.Screen name="Main" component={NavBar} /> |
19 | 53 | ) : ( |
20 | 54 | <Stack.Screen name="Authentication">{() => <AuthenticationScreen />}</Stack.Screen> |
|
0 commit comments