-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
121 lines (113 loc) · 3.53 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow strict-local
*/
import React from 'react';
// import {NavigationContainer} from '@react-navigation/native';
// import {createStackNavigator} from '@react-navigation/stack';
import { Auth as AmplifyAuth, Analytics } from 'aws-amplify';
// const Stack = createStackNavigator();
// import LoginScreen from './src/views/login';
// import HomeScreen from './src/views/home';
// import CreateNewPasswordScreen from './src/views/createNewPassword';
import Initializing from './src/views/Initializing';
import Auth from './src/views/Auth/Auth';
import MainNav from './src/views/Home/MainNav';
import { NativeModules } from 'react-native';
import {
NavigationContainer,
DefaultTheme,
DarkTheme,
} from '@react-navigation/native';
import AsyncStorage from '@react-native-community/async-storage';
const getData = async () => {
try {
const value = await AsyncStorage.getItem('@storage_Key_chm_device_token')
if(value !== null) {
// value previously stored
return value;
}
} catch (e) {
// error reading value
console.log('getData err', e);
}
};
import { useTheme } from 'react-native-paper';
const AppNavigator = () => {
const theme = useTheme();
const navigationTheme = theme.dark ? DarkTheme : DefaultTheme;
const [currentView, setCurrentView] = React.useState('initializing');
const updateAuth = (currentView2) => {
setCurrentView(currentView2);
};
React.useEffect(() => {
const checkAuth = async () => {
try {
const user = await AmplifyAuth.currentAuthenticatedUser();
if (user) {
console.log('user is signed in');
setCurrentView('home');
}
} catch (err) {
console.log('user is not signed in');
setCurrentView('auth');
}
};
const updateEndpoint = async () => {
try {
const user = await AmplifyAuth.currentAuthenticatedUser();
// getData().then((token) => {
NativeModules.RNPushNotification.getToken((token) => {
console.log('token', token);
Analytics.updateEndpoint({
address: token,
channelType: 'GCM',
userId: user.username,
optOut: 'NONE',
}).then((data) => console.log('updated endpoint', data));
});
} catch (err) {
console.log('update endpoint failed', err);
setCurrentView('auth');
}
};
if (currentView === 'initializing') {
setCurrentView('home');
} else if (currentView === 'auth') {
checkAuth();
} else if (currentView === 'home') {
updateEndpoint();
}
}, [currentView]);
return (
<NavigationContainer theme={navigationTheme}>
{currentView === 'initializing' && <Initializing />}
{currentView === 'auth' && <Auth updateAuth={updateAuth} />}
{currentView === 'home' && <MainNav updateAuth={updateAuth} />}
</NavigationContainer>
// <Stack.Navigator>
// {state.userToken == null ? (
// <>
// <Stack.Screen
// name="Login"
// component={LoginScreen}
// options={{
// title: 'Chmbox',
// animationTypeForReplace: 'pop',
// }}
// />
// <Stack.Screen
// name="CreateNewPassword"
// component={CreateNewPasswordScreen}
// />
// </>
// ) : (
// <Stack.Screen name="Home" component={HomeScreen} />
// )}
// </Stack.Navigator>
);
};
export default AppNavigator;