-
Notifications
You must be signed in to change notification settings - Fork 6
/
App.js
217 lines (199 loc) · 6.4 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
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow strict-local
*/
import React from 'react';
import {
View,
StatusBar,
Image,
TouchableOpacity,
Linking
} from 'react-native';
import { NavigationContainer, useNavigation } from '@react-navigation/native';
import { createDrawerNavigator,
DrawerContentScrollView,
DrawerItemList,
DrawerItem
} from '@react-navigation/drawer';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import Icon from 'react-native-vector-icons/Ionicons';
import Search from './src/screens/search';
import Fav from './src/screens/fav';
import Profile from './src/screens/profile';
import commonStyles from './commonStyles';
// 20200501 JustCode: Import the camera and file system module
import Camera, { Constants } from "./src/components/camera";
import RNFS from 'react-native-fs';
const Drawer = createDrawerNavigator();
const DrawerNav = (props) => {
return (
<Drawer.Navigator
initialRouteName="TabNav"
drawerContent={
// 20200501 JustCode:
// Pass in the toggleCamera from parent component (App) method to DrawerContent
// Add profilePhoto props to hold the profile image
drawerProps => <DrawerContent {...drawerProps} toggleCamera={props.toggleCamera} profilePhoto={props.profilePhoto} />
}
>
<Drawer.Screen name="TabNav" component={TabNav} options={{title: 'Home'}} />
<Drawer.Screen name="Profile" component={Profile} options={{title: 'My Profile'}} />
</Drawer.Navigator>
);
}
const DrawerContent = (props) => {
return (
<>
<View style={commonStyles.drawerHeader}>
{/*
20200430 - JustCode:
Add a new Camera icon on top of the profile photo.
*/}
<View style={{width: 100, alignSelf: 'center' }}>
<Image source={props.profilePhoto} style={commonStyles.drawerProfilePhoto} />
<TouchableOpacity style={commonStyles.profileCamera}
onPress={() => {
// Call the toggleCamera passed by DrawerNav
props.toggleCamera && props.toggleCamera();
}}
>
<Icon name="ios-camera" size={50} color="#22222288" />
</TouchableOpacity>
</View>
</View>
<DrawerContentScrollView {...props}>
<DrawerItemList activeBackgroundColor={'transparent'} {...props} />
<DrawerItem
label="About"
onPress={() => Linking.openURL('https://www.justnice.net')}
/>
</DrawerContentScrollView>
</>
);
}
const Tab = createBottomTabNavigator();
const TabNav = () => {
return(
<Tab.Navigator
screenOptions={({ route }) => ({
tabBarIcon: ({ focused, color, size }) => {
let iconName = 'logo-react';
if (route.name === 'Search') {
iconName = 'ios-search';
} else if (route.name === 'Fav') {
iconName = focused ? 'ios-heart' : 'ios-heart-empty';
}
// You can return any component that you like here!
return <Icon name={iconName} size={size} color={color} />;
}
})}
tabBarOptions={{
activeTintColor: 'white',
inactiveTintColor: 'gray',
activeBackgroundColor: '#219bd9',
inactiveBackgroundColor: '#d6f9ff',
safeAreaInsets: {bottom: 0},
style: {height: 70},
tabStyle: {paddingBottom: 15}
}}
>
<Tab.Screen name="Search" component={Search} />
<Tab.Screen name="Fav" component={Fav} />
</Tab.Navigator>
);
}
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
showCamera: false, // Hide the camera by default
profilePhoto: require('./assets/icon.png') // Set the default profile photo to icon.png
};
}
// 20200502 JustCode
// Create a new constructor to check if there is any profile photo or not.
componentDidMount() {
// Check if there is any profile photo or not.
let path = RNFS.DocumentDirectoryPath + '/profilePic.png';
RNFS.exists(path)
.then(exist => {
console.log('File exist: ', exist);
if(exist) {
RNFS.readFile(path, 'base64')
.then(buffer => {
console.log('File read.');
this.setState({profilePhoto: {
uri: 'data:image/png;base64,' + buffer
}});
})
.catch(err => {
console.log('Unable to read profile photo. ', err);
})
}
})
.catch(err => {
console.log('Unable to access file system. ', err);
});
}
saveProfilePhoto(data) {
this.setState({showCamera: false});
let path = RNFS.DocumentDirectoryPath + '/profilePic.png';
// strip off the data: url prefix to get just the base64-encoded bytes
var imgData = data.replace(/^data:image\/\w+;base64,/, "");
// write the file
RNFS.writeFile(path, imgData, 'base64')
.then(_ => {
// Update the profilePhoto state so that the profile photo will update
// to the latest photo
this.setState({profilePhoto: {
uri: 'data:image/png;base64,' + imgData
}});
})
.catch((err) => {
console.log(err.message);
});
}
render() {
return (
<NavigationContainer>
<StatusBar barStyle="default" backgroundColor="#219bd9" />
{/*
20200501 JustCode:
Define a method called toggleCamera in the props of DrawerNav.
Define a profilePhoto prop to hold the user profile photo.
*/}
<DrawerNav {...this.props}
toggleCamera={() => {
this.setState({showCamera: !this.state.showCamera});
}}
profilePhoto={this.state.profilePhoto}
/>
{/*
20200501 JustCode:
Show the camera when user click on the camera button.
*/}
{
this.state.showCamera &&
<Camera
cameraType={Constants.Type.front}
flashMode={Constants.FlashMode.off}
autoFocus={Constants.AutoFocus.on}
whiteBalance={Constants.WhiteBalance.auto}
ratio={'1:1'}
quality={0.5}
imageWidth={800}
onCapture={data => this.saveProfilePhoto(data)}
onClose={_ => {
this.setState({showCamera: false});
}}
/>
}
</NavigationContainer>
);
}
}
export default App;