-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
126 lines (110 loc) · 3.7 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
122
123
124
125
126
import { StatusBar } from 'expo-status-bar';
import React, {useEffect, useState, useRef} from 'react';
import { StyleSheet, Text, View, AppState, Linking, Platform, TouchableWithoutFeedback } from 'react-native';
import Home from './src/screens/Home';
import DeviceList from './src/screens/DeviceList';
import { Stack, HStack, Center, Box, Progress, Heading, NativeBaseProvider, Icon } from 'native-base';
import {createNativeStackNavigator } from '@react-navigation/native-stack';
import { NavigationContainer } from '@react-navigation/native';
import { Root, Popup } from 'popup-ui'
import AsyncStorage from '@react-native-async-storage/async-storage';
import NewDevicePrompt from './src/screens/NewDevicePrompt';
const config = {
dependencies: {
'linear-gradient': require('expo-linear-gradient').LinearGradient
}
}
const nav_bar_options = {
title: "NVNT Patch",
headerStyle: {
backgroundColor: "#18181b"
},
headerTintColor: "#fff",
headerTitleAlign: 'center'
}
const nav_bar_options_no_back = {
title: "Connect New Device",
headerStyle: {
backgroundColor: "#18181b"
},
headerTintColor: "#fff",
headerTitleAlign: 'center',
headerLeft: () => {
return null;
},
headerBackVisible: false
}
const PERSISTENCE_KEY = 'APPLICATION_STATE';
const nav_stack = createNativeStackNavigator();
export default function App() {
const appState = useRef(AppState.currentState);
const [appStateVisible, setAppStateVisible] = useState(appState.current);
const [initialState, setInitialState] = useState();
const [isReady, setIsReady] = useState(false);
useEffect(() => {
if(!isReady || initialState == undefined) {
restoreState();
}
AppState.addEventListener('change', _handleAppStateChange);
return() => {
AppState.removeEventListener('change', _handleAppStateChange);
}
}, [isReady]);
async function restoreState() {
try {
const initialUrl = await Linking.getInitialURL();
if(Platform.OS !== 'web') {
const savedStateString = await AsyncStorage.getItem(PERSISTENCE_KEY);
var state = savedStateString ? JSON.parse(savedStateString): undefined;
if(state !== undefined) {
setInitialState(state);
} else if(state == undefined) {
state = {
bleConnected: false,
bleMAC: "null"
};
AsyncStorage.setItem(PERSISTENCE_KEY, JSON.stringify(state));
setInitialState(state);
}
console.log(state);
}
console.log("Restore state called!");
} finally {
setIsReady(true);
}
}
const _handleAppStateChange = nextAppState => {
if(appState.current.match(/inactive|background/) && nextAppState === 'active') {
console.log("App has come into the foreground!");
/*
Check BLE for updates
*/
}
appState.current = nextAppState;
setAppStateVisible(appState.current);
console.log("App State:", appState.current);
}
if(!isReady) {
return null; /* TO-DO: Local cache error page */
}
return (
<NativeBaseProvider config={config}>
<NavigationContainer>
<nav_stack.Navigator>
<nav_stack.Screen name="Home" component={Home} options={nav_bar_options} />
<nav_stack.Screen name="Connect" component={DeviceList} options={nav_bar_options_no_back} />
<nav_stack.Screen name="NewDevice" component={NewDevicePrompt} options={nav_bar_options_no_back} />
</nav_stack.Navigator>
</NavigationContainer>
<StatusBar style="light" />
</NativeBaseProvider>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});