This repository has been archived by the owner on Jan 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
App.js
315 lines (272 loc) · 9.22 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow strict-local
*/
import 'react-native-gesture-handler';
import SplashScreen from 'react-native-splash-screen';
import messaging from '@react-native-firebase/messaging';
import React, {useEffect, useReducer, useRef, useCallback, useState} from 'react';
import {NavigationContainer, DarkTheme, useTheme} from '@react-navigation/native';
import {createStackNavigator} from '@react-navigation/stack';
import {createDrawerNavigator} from '@react-navigation/drawer';
import Orientation from 'react-native-orientation-locker';
import Toast from 'react-native-toast-message';
import ConnectServerScreen from './screens/ConnectServerScreen';
import ServerSettingsScreen from './screens/ServerSettingsScreen';
import SettingsScreen from './screens/SettingsScreen';
import SessionContext from './session-context';
import {CameraScreen, DirectCameraScreen} from './screens/CameraScreen';
import EventScreen from './screens/EventScreen';
import DrawerMenu from './menu';
import {Button, View, Alert, Text, Platform} from 'react-native';
import {MenuProvider} from 'react-native-popup-menu';
import {
getAccountInfoList,
getActiveAccountInfoByList,
getTargetAccountByServerId,
removeAccountInfo,
getSelectedCameraList,
} from './lib/storage';
const Stack = createStackNavigator();
const Drawer = createDrawerNavigator();
import {checkServerCredentials, getAvailableDevices} from './lib/api';
import {unsaveNotificationToken} from './lib/notification';
import {handleNotification, isValidNotificationType} from './notification';
import {initialState, reducer} from './state';
function ServerRemoveButton({id, name, serverUuid, address, port, navigation, updateAccountList}) {
return (
<Button
title="Remove"
color="red"
onPress={() =>
Alert.alert(
'Server Removal Confirmation',
'Are you sure to remove "' + name + '" server?',
[
{text: 'Cancel', style: 'cancel'},
{
text: 'Confirm',
style: 'destructive',
onPress: async () => {
const url = 'https://' + address + (port ? ':' + port : '');
try {
await unsaveNotificationToken(url, id, serverUuid);
} catch (err) {
Toast.show({
type: 'error',
text1: 'Notification System',
text2: err.message,
});
}
await removeAccountInfo(id);
await updateAccountList();
navigation.goBack();
},
},
],
{cancelable: true},
)
}
/>
);
}
const App: () => React$Node = () => {
const navRef = useRef(null);
const [state, dispatch] = useReducer(reducer, initialState);
const [notification, setNotification] = useState(null);
const updateAccountList = async () => {
const list = await getAccountInfoList();
dispatch({type: 'update_account_list', payload: list});
return list;
};
const loginTargetAccount = useCallback((targetAccount, targetDevice) => {
if (!targetAccount) {
SplashScreen.hide();
return;
}
const [id, accountRecord] = targetAccount;
const {address, login, password, port} = accountRecord;
checkServerCredentials('https://' + address + (port ? ':' + port : ''), login, password).then((status) => {
if (status !== false) {
dispatch({
type: 'login',
payload: {id, ...accountRecord},
targetDevice,
});
}
});
}, []);
const navigateTargetDevice = useCallback((deviceList, targetDevice) => {
if (!targetDevice) {
SplashScreen.hide();
return;
}
SplashScreen.show();
let params = {};
if (deviceList.find(({id}) => parseInt(id) === parseInt(targetDevice))) {
params = {
screen: 'DeviceDirectPlay',
params: {
deviceId: targetDevice,
},
};
}
navRef.current?.navigate('DirectCamera', params);
}, []);
const onShowCamera = useCallback((deviceId) => {
SplashScreen.show();
navRef.current?.navigate('DirectCamera', {
screen: 'DeviceDirectPlay',
params: {
deviceId: deviceId,
},
});
}, []);
useEffect(() => {
Platform.OS === 'android' && SplashScreen.show();
const msg = messaging();
msg.onNotificationOpenedApp((message) => setNotification(message));
msg.setBackgroundMessageHandler(async (message) => {});
}, []);
useEffect(() => {
if (!notification) {
return;
}
const {serverId, deviceId, eventType} = notification.data;
if (state.activeAccount && state.activeAccount.serverUuid === serverId) {
if (isValidNotificationType(eventType)) {
navigateTargetDevice(state.deviceList, deviceId);
}
return;
}
dispatch({type: 'logout'});
getTargetAccountByServerId(state.accountList, serverId).then((targetAccount) => {
if (!targetAccount) {
return;
}
loginTargetAccount(targetAccount, isValidNotificationType(eventType) ? deviceId : null);
});
}, [notification]);
useEffect(() => {
if (!state.activeAccount) {
Orientation.lockToPortrait();
}
updateAccountList().then(async (accountList) => {
if (!state.activeAccount) {
const initialNotification = await messaging().getInitialNotification();
if (initialNotification) {
const {serverId, deviceId, eventType} = initialNotification.data;
loginTargetAccount(
await getTargetAccountByServerId(accountList, serverId),
isValidNotificationType(eventType) ? deviceId : null,
);
} else {
loginTargetAccount(await getActiveAccountInfoByList(accountList));
}
return;
}
Promise.all([getAvailableDevices(state.activeAccount), getSelectedCameraList(state.activeAccount.id)]).then(
([deviceList, selectedDeviceList]) => {
dispatch({
type: 'update_all_device_list',
deviceList,
selectedDeviceList,
});
navigateTargetDevice(deviceList, state.targetDevice);
},
);
});
return handleNotification(state, onShowCamera);
}, [state.activeAccount]);
return (
<SessionContext.Provider value={{state, dispatch, updateAccountList}}>
<MenuProvider>
<NavigationContainer ref={navRef} theme={DarkTheme}>
{state.activeAccount ? (
<Drawer.Navigator drawerContent={(props) => <DrawerMenu {...props} />}>
<Drawer.Screen
name="Camera"
options={{title: 'Screens'}}
component={CameraScreen}
listeners={{
focus: () => {
SplashScreen.hide();
Orientation.lockToLandscape();
},
}}
/>
<Drawer.Screen
name="DirectCamera"
component={DirectCameraScreen}
options={{title: 'Direct Camera'}}
listeners={{
focus: () => {
SplashScreen.hide();
Orientation.lockToLandscape();
},
}}
/>
<Drawer.Screen
name="Event"
component={EventScreen}
listeners={{
focus: () => {
SplashScreen.hide();
Orientation.lockToLandscape();
},
}}
/>
<Drawer.Screen
name="Settings"
component={SettingsScreen}
options={{title: 'Settings'}}
listeners={{
focus: () => {
SplashScreen.hide();
},
}}
/>
</Drawer.Navigator>
) : (
<Stack.Navigator>
<Stack.Screen
name="Home"
component={ConnectServerScreen}
options={{headerShown: false}}
listeners={{
focus: () => Orientation.lockToPortrait(),
}}
/>
<Stack.Screen
name="ServerSettings"
component={ServerSettingsScreen}
options={({route, navigation}) =>
route.params?.id
? {
title: 'Edit Server',
headerRight: () => (
<ServerRemoveButton
{...route.params}
navigation={navigation}
updateAccountList={updateAccountList}
/>
),
}
: {title: 'Add Server'}
}
listeners={{
focus: () => Orientation.lockToPortrait(),
}}
/>
</Stack.Navigator>
)}
</NavigationContainer>
<Toast ref={(ref) => Toast.setRef(ref)} />
</MenuProvider>
</SessionContext.Provider>
);
};
export default App;