-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
166 lines (152 loc) · 4.37 KB
/
App.js
File metadata and controls
166 lines (152 loc) · 4.37 KB
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
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow strict-local
*/
import React, { useEffect, useState, useRef } from 'react';
import {
PermissionsAndroid, Alert, BackHandler,
StatusBar, View, ActivityIndicator, Dimensions, Animated, TouchableOpacity, Text
} from 'react-native';
import { WebView } from 'react-native-webview';
import nodejs from 'nodejs-mobile-react-native';
import RNExitApp from 'react-native-exit-app';
import RNFS from 'react-native-fs';
let { width, height } = Dimensions.get('window');
const App = () => {
const [serverState, setServerState] = useState(false);
const [backClickCount, setbackClickCount] = useState(0);
const animate = useRef(new Animated.Value(100)).current;
const animateExit = () => {
setbackClickCount(1);
Animated.sequence([
Animated.spring(
animate,
{
toValue: -.15 * height,
friction: 5,
duration: 300,
useNativeDriver: true,
}
),
Animated.timing(
animate,
{
toValue: 100,
duration: 300,
useNativeDriver: true,
}
),
]).start(() => {
setbackClickCount(0);
});
}
useEffect(() => {
// console.log(RNFS.DocumentDirectoryPath)
const handleBackButton = () => {
//backClickCount == 1 ? BackHandler.exitApp() : animateExit();
backClickCount == 1 ? RNExitApp.exitApp() : animateExit();
return true;
};
nodejs.start('main.js');
nodejs.channel.addListener('message', (msg: any) => {
if (msg === 'Server Started') {
setServerState(true);
}
console.log(msg);
});
nodejs.channel.send({ 'path': RNFS.ExternalDirectoryPath });
nodejs.channel.send('Start');
BackHandler.addEventListener('hardwareBackPress', handleBackButton);
return () => {
console.log('unmounted');
BackHandler.removeEventListener('hardwareBackPress', handleBackButton);
}
// const getPermissions = async () => {
// const writeGrant = await PermissionsAndroid.request(
// PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
// {
// title: 'Write to storage',
// message: 'This App needs access to your Storage. ',
// buttonNegative: 'Cancel',
// buttonPositive: 'OK'
// }
// );
// if (writeGrant === PermissionsAndroid.RESULTS.GRANTED) {
// console.log('has permissons');
// nodejs.start('main.js');
// nodejs.channel.addListener('message', (msg: any) => {
// if (msg === 'Server Started') {
// setServerState(true);
// }
// console.log(msg);
// });
// // nodejs.start('main.js');
// // nodejs.channel.addListener('message', (msg: any) => {
// // if (msg === 'Dev app listening on port 3000!') {
// // setHasPermissions(true);
// // }
// // });
// } else {
// Alert.alert('This App need storage permission for it to work.');
// }
// };
// getPermissions();
}, []);
if (!serverState) {
return (
<View style={{ alignItems: 'center', justifyContent: 'center', flex: 1 }}>
<ActivityIndicator size='large' />
</View>
);
}
return (
<>
<StatusBar barStyle="dark-content" />
<WebView
source={{ uri: 'http://127.0.0.1:9000/' }}
/>
<Animated.View style={[styles.animatedView, { transform: [{ translateY: animate }] }]}>
<Text style={styles.exitTitleText}>press back again to exit the app</Text>
<TouchableOpacity
activeOpacity={0.9}
// onPress={() => BackHandler.exitApp()}
onPress={() => RNExitApp.exitApp()}
>
<Text style={styles.exitText}>Exit</Text>
</TouchableOpacity>
</Animated.View>
</>
);
};
const styles = {
container: {
flex: 1,
justifyContent: "center",
alignItems: "center"
},
animatedView: {
width,
backgroundColor: "#0a5386",
elevation: 2,
position: "absolute",
bottom: 0,
padding: 10,
justifyContent: "center",
alignItems: "center",
flexDirection: "row",
},
exitTitleText: {
textAlign: "center",
color: "#ffffff",
marginRight: 10,
},
exitText: {
color: "#e5933a",
paddingHorizontal: 10,
paddingVertical: 3
}
};
export default App;