-
Notifications
You must be signed in to change notification settings - Fork 7
/
App.js
154 lines (141 loc) · 4.95 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
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, { useState, useEffect } from 'react';
import { StatusBar } from 'expo-status-bar'
import { getStatusBarHeight } from 'react-native-status-bar-height';
import { ThemeProvider } from 'react-native-elements'
import { View, Platform, StyleSheet } from 'react-native';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs'
import { NavigationContainer } from '@react-navigation/native';
import { SafeAreaProvider } from 'react-native-safe-area-context'
import { MaterialCommunityIcons, FontAwesome5 } from '@expo/vector-icons';
import AsyncStorage from '@react-native-community/async-storage'
import Browser from './App/Browser'
import Devices from './App/Devices'
import Add from './App/Add'
global.theme = {
colors: {
primary: '#009688',
secondary: '#333333',
success: '#3CB479',
error: '#F44336',
warning: '#BAA441',
info: '#949494',
lightGray: '#d1d1d1',
background: '#FAFAFA'
}
}
global.styles = StyleSheet.create({
button: {
borderRadius: 4,
paddingVertical: 6,
paddingHorizontal: 16
},
text: {
fontFamily: "Roboto",
},
})
//This ensures that there is a single color status bar on both IOS and Android and that the content starts below it.
const ColoredStatusBar = ({ backgroundColor, ...props }) => (
<View style={{ height: getStatusBarHeight(true), backgroundColor: backgroundColor }}>
<StatusBar translucent={Platform.OS === 'ios' ? true : false} backgroundColor={backgroundColor} {...props} />
</View>
);
const Tab = createBottomTabNavigator();
export default function App() {
const [selectedDevice, setSelectedDevice] = useState(-1)
const [devices, setDevices] = useState([])
const saveData = async () => {
try {
await AsyncStorage.setItem('devices', JSON.stringify(devices))
await AsyncStorage.setItem('selectedDevice', JSON.stringify(selectedDevice))
} catch (e) {
console.warn('Failed to save the data to the storage. Error: ' + e)
}
}
const readData = async () => {
try {
const devicesRead = await AsyncStorage.getItem('devices')
const selectedDeviceRead = await AsyncStorage.getItem('selectedDevice')
if (devicesRead !== null) {
setDevices(JSON.parse(devicesRead));
}
else {
console.warn("Error reading devices")
}
if (selectedDeviceRead !== null) {
setSelectedDevice(JSON.parse(selectedDeviceRead));
}
else {
console.warn("Error reading selected device")
}
} catch (e) {
console.warn('Failed to fetch the data from storage')
}
}
useEffect(() => {
readData();
}, [])
useEffect(() => {
saveData();
}, [devices, selectedDevice])
return (
<ThemeProvider theme={global.theme}>
<ColoredStatusBar backgroundColor={global.theme.colors.secondary} style='light' />
<SafeAreaProvider>
<NavigationContainer theme={global.theme}>
<Tab.Navigator
initialRouteName="Browser"
screenOptions={{
cardStyle: {
backgroundColor: global.theme.colors.secondary
}
}}
tabBarOptions={{
keyboardHidesTabBar: true,
activeTintColor: global.theme.colors.primary,
inactiveTintColor: global.theme.colors.background,
safeAreaInsets: { top: 0, bottom: 7, right: 0, left: 0 },
tabStyle: {
paddingTop: 7
},
style: {
position: 'absolute',
backgroundColor: global.theme.colors.secondary,
}
}}
>
<Tab.Screen
name="Browser"
options={{
tabBarLabel: 'Lights',
tabBarIcon: ({ color, size }) => (
<MaterialCommunityIcons name="lightbulb" size={size} color={color} />
),
}}>
{() => { return <Browser devices={devices} selectedDevice={selectedDevice} /> }}
</Tab.Screen>
<Tab.Screen
name="Devices"
options={{
tabBarLabel: 'Devices',
tabBarIcon: ({ color, size }) => (
<FontAwesome5 name="broadcast-tower" size={size * 0.85} color={color} />
),
}}>
{() => { return <Devices devices={devices} setDevices={setDevices} selectedDevice={selectedDevice} setSelectedDevice={setSelectedDevice} /> }}
</Tab.Screen>
<Tab.Screen
name="Add"
options={{
tabBarLabel: 'Add Device',
tabBarIcon: ({ color, size }) => (
<MaterialCommunityIcons name="plus" size={size * 1.4} color={color} />
),
}}>
{() => { return <Add devices={devices} setDevices={setDevices} /> }}
</Tab.Screen>
</Tab.Navigator>
</NavigationContainer>
</SafeAreaProvider>
</ThemeProvider>
);
}