-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.tsx
153 lines (131 loc) · 4.68 KB
/
App.tsx
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import React, { useEffect, useState, useCallback, useReducer } from "react";
import ScreenNavigator from "./routes/ScreenNavigator";
import * as Linking from "expo-linking";
import * as SplashScreen from "expo-splash-screen";
import AsyncStorage from "@react-native-async-storage/async-storage";
import { GlobalContextProvider } from "./shared/context/GlobalContext";
import { GlobalState, GlobalState_Actions, Regions, Store_Tokens } from "./shared/types/CustomTypes";
SplashScreen.preventAutoHideAsync();
export default function App() {
//Used for deep linking
// exp://192.168.0.21:19000/--/device/?appid=oai-test-devices&device_id=a-new-device&link=true
// dma://device/?appid=oai-test-devices&uid=A4RF3C&link=true
const initialState: GlobalState = {
ttn_auth_token: null,
ttn_allowed_chars: new RegExp("^[a-z0-9](?:[-]?[a-z0-9]){2,}$"),
application_server: Regions.EU1,
communication_server: Regions.AU1,
network_status: false,
};
const reducer = (state, action) => {
switch (action.type) {
case GlobalState_Actions.SET_AUTH_TOKEN:
return {
...state,
ttn_auth_token: action.payload,
};
case GlobalState_Actions.SET_TOKEN_VALID:
return {
...state,
ttn_isValid_token: action.payload,
};
case GlobalState_Actions.SET_APPLICATION_SERVER:
if (!action.payload) return state; //If null, maintain default values from initial state
return {
...state,
application_server: action.payload,
};
case GlobalState_Actions.SET_COMMUNICATION_SERVER:
if (!action.payload) return state;
return {
...state,
communication_server: action.payload,
};
case GlobalState_Actions.SET_NETWORK_STATUS:
return {
...state,
network_status: action.payload,
};
default:
return state;
}
};
const [state, dispatch] = useReducer(reducer, initialState);
const prefix = Linking.createURL("/");
const [data, setData] = useState(null);
const [appIsReady, setAppIsReady] = useState(false);
const linking = {
prefixes: [prefix],
config: {
screens: {
HomeScreen: "device",
},
},
};
useEffect(() => {
async function getInitialURL() {
const initialURL = await Linking.getInitialURL();
console.log("init", initialURL);
if (initialURL) setData(Linking.parse(initialURL));
}
async function prepare() {
//Prepare user settings
try {
await loadUserSettings();
} catch (e) {
console.warn(e);
} finally {
setAppIsReady(true);
}
}
const listener = Linking.addEventListener("url", handleDeepLink);
if (!data) {
getInitialURL();
}
prepare();
return () => {
listener.remove();
};
}, []);
useEffect(() => {
async function checkAppReady() {
if (appIsReady) {
console.log("app is ready");
await SplashScreen.hideAsync();
}
}
checkAppReady();
}, [appIsReady]);
const handleDeepLink = (event) => {
let data = Linking.parse(event.url);
setData(data);
};
const loadUserSettings = async () => {
try {
const server = await AsyncStorage.getItem(Store_Tokens.APPLICATION_SERVER);
dispatch({ type: GlobalState_Actions.SET_APPLICATION_SERVER, payload: server });
} catch (error) {
console.log(error);
}
try {
const server = await AsyncStorage.getItem(Store_Tokens.COMMUNICATION_SERVER);
dispatch({ type: GlobalState_Actions.SET_COMMUNICATION_SERVER, payload: server });
} catch (error) {
console.log(error);
}
try {
const authToken = await AsyncStorage.getItem(Store_Tokens.AUTH_TOKEN);
dispatch({ type: GlobalState_Actions.SET_AUTH_TOKEN, payload: authToken });
} catch (error) {
console.log(error);
}
};
if (!appIsReady) {
return null;
}
return (
<GlobalContextProvider reducer={[state, dispatch]}>
<ScreenNavigator linking={linking} />
</GlobalContextProvider>
);
}