-
Notifications
You must be signed in to change notification settings - Fork 4
/
ProfileSetup.js
472 lines (408 loc) · 21 KB
/
ProfileSetup.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
import React, { Component } from 'react';
import { Text, View, ActivityIndicator, ScrollView, ToastAndroid, PermissionsAndroid, Alert } from 'react-native';
import { Header, Avatar, Icon, Input, Button } from 'react-native-elements';
import * as Colors from '../assets/Colors';
import ImagePicker from 'react-native-image-crop-picker';
import SegmentedControlTab from "react-native-segmented-control-tab";
import NetInfo from '@react-native-community/netinfo';
import Geolocation from '@react-native-community/geolocation';
import storage from '@react-native-firebase/storage';
import firestore from '@react-native-firebase/firestore';
import AsyncStorage from '@react-native-community/async-storage';
import { Actions } from 'react-native-router-flux';
export default class ProfileSetup extends Component {
constructor(props) {
super(props);
this.state = {
loading: false,
errorMessage: 'ERR_UNKNOWN',
pictureSelected: false,
response: null,
profileName: null,
profileAge: null,
profileProfession: null,
profileCompany: null,
profilePreferenceIndex: null,
profileGenderIndex: null,
profileCountry: null,
profileState: null,
profileCity: null
}
}
TAG = 'ProfileSetup:';
backHandler = null;
openImagePicker() {
ToastAndroid.show('Use 3:4 ratio for best results', ToastAndroid.LONG);
ImagePicker.openPicker({
enableRotationGesture: true,
cropping: true,
cropperActiveWidgetColor: Colors.primary,
cropperStatusBarColor: Colors.primary,
cropperToolbarColor: Colors.primary,
compressImageQuality: 0.7
}).then(response => {
if (response !== null) {
console.log(this.TAG, response)
this.setState({ response, pictureSelected: true });
Actions.push('avatarPreview', { avatar_url: response.path });
}
})
.catch(error => console.log(this.TAG, error));
}
handleAvatarLongPress() {
Alert.alert(
'Flambr',
'Remove profile picture? You can change it later',
[
{ text: 'NO' },
{ text: 'YES', onPress: () => this.setState({ pictureSelected: false, response: null }), style: 'cancel' }
]
);
}
async checkInternet() {
const status = await NetInfo.fetch();
if (!status.isConnected) {
this.setState({ errorMessage: 'ERR_NO_INTERNET' });
throw Error();
}
}
async checkProfileValues() {
if (this.state.profileName == null || this.state.profileAge == null || this.state.profileCompany == null || this.state.profileProfession == null || this.state.profileGenderIndex == null || this.state.profilePreferenceIndex == null) {
this.setState({ errorMessage: 'ERR_INVALID_VALUE' });
throw Error();
}
}
async uploadAvatar() {
console.log(this.TAG, 'Called uploadAvatar');
if (!this.state.pictureSelected) {
console.log(this.TAG, 'Avatar was empty');
ToastAndroid.show('No avatar selected. Change it later.', ToastAndroid.LONG);
return;
}
else {
//Get the phone number from local storage to create the reference for the avatar storage
var phoneNumber = await AsyncStorage.getItem('PHONE_NUMBER');
try {
await storage().ref(`avatar/${phoneNumber}`).putFile(this.state.response.path);
}
catch (error) {
console.log(this.TAG, error)
this.setState({ errorMessage: 'ERR_STORAGE' });
throw Error();
}
}
}
async getAvatarUrl() {
console.log(this.TAG, 'Called getAvatarUrl');
//Get the phone number for the avatar refernce
var phoneNumber = await AsyncStorage.getItem('PHONE_NUMBER');
try {
const avatarUrl = await storage().ref(`avatar/${phoneNumber}`).getDownloadURL();
await AsyncStorage.setItem('AVATAR_URL', avatarUrl);
}
catch (error) {
this.setState({ errorMessage: 'ERR_STORAGE' });
throw Error();
}
}
async getLocation() {
//Check for location access.
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
{
'title': 'Flambr',
'message': 'Flambr wants to access your location '
}
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log(this.TAG, 'Location permission granted');
Geolocation.getCurrentPosition(position => {
console.log(this.TAG, 'Got postion:', position);
var latitude = Number(position.coords.latitude);
var longitude = Number(position.coords.longitude);
var prox = `prox=${latitude},${longitude},250`;
//TODO: Replace API key and APP codes
fetch("https://reverse.geocoder.api.here.com/6.2/reversegeocode.json?" + prox + "&mode=retrieveAddresses&maxresults=1&gen=9&app_id=<Your App Id>&app_code=<You App Code>")
.then(resp => resp.json())
.then(resp => {
console.log(this.TAG, 'Response:', resp);
var profileCountry = resp.Response.View[0].Result[0].Location.Address.Country;
var profileState = resp.Response.View[0].Result[0].Location.Address.State;
var profileCity = resp.Response.View[0].Result[0].Location.Address.City;
this.setState({ profileCountry, profileState, profileCity });
console.log(this.TAG, this.state);
this.createFirestoreProfile();
});
}, error => {
this.setState({ errorMessage: 'ERR_LOCATION', loading: false });
ToastAndroid.show('Error while getting the location', ToastAndroid.LONG);
}, {
enableHighAccuracy: false, timeout: 10000
});
}
else {
//Permission was not granted. Throw an error.
this.setState({ errorMessage: 'ERR_LOCATION_PERMISSION_DENIED' });
throw Error();
}
}
async createFirestoreProfile() {
console.log(this.TAG, 'Called createFirestoreProfile. State:', this.state);
var { profileCountry, profileState, profileCity, profileName, profileAge, profileGenderIndex, profilePreferenceIndex, profileProfession, profileCompany } = this.state;
//Convert age to number for uploading
var numberProfileAge = parseInt(profileAge, 10);
let profileGender = null;
let profilePreference = null;
//Set value of gender
if (profileGenderIndex == 0)
profileGender = 'male';
else
profileGender = 'female';
//Set value of preference for this user
if (profilePreferenceIndex == 0) {
profilePreference = 'male';
}
else if (profilePreferenceIndex == 1) {
profilePreference = 'female';
}
else {
profilePreference = 'both'
}
let phoneNumber = null;
let avatarUrl = null;
let userDocRef = null;
try {
await AsyncStorage.setItem('USER_NAME', profileName);
await AsyncStorage.setItem('USER_GENDER', profileGender);
await AsyncStorage.setItem('USER_PREF', profilePreference);
await AsyncStorage.setItem('USER_COUNTRY', profileCountry);
//Also get the phone number and avatar url from local storage
phoneNumber = await AsyncStorage.getItem('PHONE_NUMBER');
avatarUrl = await AsyncStorage.getItem('AVATAR_URL');
//Lastly, create the user document reference that will be stored to local storage
const ref = `users/${profileCountry}/${profileState}-${profileCity}/${phoneNumber}`;
console.log(this.TAG, 'Created the user document reference', ref);
await AsyncStorage.setItem('USER_DOC_REF', ref);
userDocRef = ref;
}
catch (error) {
console.log(this.TAG, error);
this.setState({ errorMessage: 'ERR_ASYNC_STORAGE', loading: false });
throw Error();
}
console.log(this.TAG, userDocRef);
//First insert all the profiel values
firestore().doc(userDocRef).set({
name: profileName,
age: numberProfileAge,
gender: profileGender,
preference: profilePreference,
joining_date: firestore.Timestamp.now(),
avatar_url: avatarUrl,
path_to_document: userDocRef,
job: profileProfession,
company: profileCompany
}, { merge: false })
.then(success => {
console.log(this.TAG, success);
})
.catch(error => {
this.setState({ errorMessage: 'ERR_FIRESTORE', loading: false });
console.log(this.TAG, error);
});
//Now we insert the parial path for later use in signing in
const allUsersRef = `all_users/${phoneNumber}`;
const partialPath = `users/${profileCountry}/${profileState}-${profileCity}`;
firestore().doc(allUsersRef).set({
path_to_document: partialPath,
pin: this.props.pin
}, { merge: false })
.then(success => {
console.log(this.TAG, success);
})
.catch(error => {
this.setState({ errorMessage: 'ERR_FIRESTORE', loading: false });
console.log(this.TAG, error);
});
//After completing everything, move to main screen
Actions.push('bottomNavigator');
}
createProfile() {
console.log(this.TAG, 'Called createProfile');
this.setState({ loading: true, errorMessage: 'ERR_UNKNOWN' });
this.checkInternet()
.then(() => this.checkProfileValues())
.then(() => this.uploadAvatar())
.then(() => this.getAvatarUrl())
.then(() => this.getLocation())
.catch(error => {
console.log(this.TAG, error);
this.setState({ loading: false });
const { errorMessage } = this.state;
switch (errorMessage) {
case 'ERR_NO_INTERNET':
ToastAndroid.show('No internet', ToastAndroid.LONG);
break
case 'ERR_INVALID_VALUE':
ToastAndroid.show('Invalid values were entered', ToastAndroid.LONG);
break
case 'ERR_STORAGE':
ToastAndroid.show('Error occurred while uploading the avatar', ToastAndroid.LONG);
break
case 'ERR_LOCATION_PERMISSION_DENIED':
ToastAndroid.show('Please grant location permission', ToastAndroid.LONG);
break
case 'ERR_LOCATION':
ToastAndroid.show('Error while getting the location', ToastAndroid.LONG);
break
case 'ERR_ASYNC_STORAGE':
ToastAndroid.show('A fatal error occurred', ToastAndroid.LONG);
break
case 'ERR_FIRESTORE':
ToastAndroid.show('Error while communicating with the server', ToastAndroid.LONG);
break
default:
ToastAndroid.show('Some error occurred. Please try again', ToastAndroid.LONG);
break
}
});
}
render() {
if (this.state.loading) {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<ActivityIndicator size='large' color={Colors.primary} />
<Text style={{ marginTop: 20, marginBottom: 10, fontFamily: 'comfortaa.regular', fontSize: 20, color: 'black', paddingHorizontal: 40, textAlign: 'center' }}>Don't close the app</Text>
<Text style={{ marginTop: 10, fontFamily: 'comfortaa.regular', fontSize: 20, color: 'black', paddingHorizontal: 40, textAlign: 'center' }}>Uploading the avatar may take a few seconds</Text>
</View>
)
}
return (
<View style={{ flex: 1 }}>
<Header
placement='center'
leftComponent={{ text: 'Profile Setup', style: { fontFamily: 'comfortaa.bold', color: 'black', fontSize: 28 } }}
leftContainerStyle={{ paddingLeft: 10 }}
centerContainerStyle={{ flex: 0, height: 0, width: 0 }}
rightContainerStyle={{ flex: 0, height: 0, width: 0 }}
backgroundColor={Colors.headerGrey} />
<ScrollView style={{ paddingHorizontal: 40, flexDirection: 'column' }}>
{this.state.pictureSelected ?
<Avatar
rounded
showEditButton={true}
source={{ uri: this.state.response.path }}
onEditPress={() => this.openImagePicker()}
size='xlarge'
renderPlaceholderContent={<Icon name='person' type='material-icon' size={28} />}
containerStyle={{ marginTop: 40, marginBottom: 10, alignSelf: 'center' }}
onLongPress={() => this.handleAvatarLongPress()}
onPress={() => this.openImagePicker()}
/>
:
<Avatar
rounded
showEditButton={true}
onEditPress={() => this.openImagePicker()}
size='xlarge'
renderPlaceholderContent={<Icon name='person' type='material-icon' size={28} />}
containerStyle={{ marginTop: 40, marginBottom: 10, alignSelf: 'center' }}
onLongPress={() => this.handleAvatarLongPress()}
onPress={() => this.openImagePicker()}
/>
}
{this.state.pictureSelected ?
<View></View>
:
<Text style={{ fontFamily: 'comfortaa.regular', color: 'red', fontSize: 14, marginBottom: 30, alignSelf: 'center' }}>No profile picture selected</Text>
}
<Input
placeholder='Name'
inputStyle={{ fontFamily: 'comfortaa.regular', fontSize: 18, color: 'black' }}
label='Full Name'
labelStyle={{ fontFamily: 'Robota-Regular', fontSize: 14, color: Colors.greyLight }}
leftIcon={<Icon name='person' type='material-icon' size={18} color='black' />}
onChangeText={profileName => this.setState({ profileName })}
value={this.state.profileName}
containerStyle={{ marginBottom: 10 }}
autoCapitalize='words'
placeholderTextColor={Colors.greyLight}
inputContainerStyle={{ borderColor: 'black' }} />
<Input
placeholder='Age'
inputStyle={{ fontFamily: 'comfortaa.regular', fontSize: 18, color: 'black' }}
label='Age'
labelStyle={{ fontFamily: 'Robota-Regular', fontSize: 14, color: Colors.greyLight }}
leftIcon={<Icon name='person' type='material-icon' size={18} color='black' />}
onChangeText={profileAge => this.setState({ profileAge })}
value={this.state.profileAge}
containerStyle={{ marginTop: 10, marginBottom: 10 }}
keyboardType='numeric'
placeholderTextColor={Colors.greyLight}
inputContainerStyle={{ borderColor: 'black' }} />
<Text style={{ fontFamily: 'comfortaa.regular', fontSize: 24, color: 'black', marginTop: 10, marginBottom: 10 }}>I am a</Text>
<SegmentedControlTab
values={['Man', 'Woman']}
selectedIndex={this.state.profileGenderIndex}
onTabPress={profileGenderIndex => this.setState({ profileGenderIndex })}
borderRadius={30}
tabTextStyle={{ fontFamily: 'comfortaa.regular', fontSize: 18, color: Colors.primary }}
activeTabOpacity={0.5}
activeTabStyle={{ backgroundColor: Colors.primary }}
tabsContainerStyle={{ marginBottom: 10, marginTop: 10 }}
tabStyle={{ borderColor: Colors.primary }}
accessibilityLabels={['Man', 'Woman']}
/>
<Text style={{ fontFamily: 'comfortaa.regular', fontSize: 24, color: 'black', marginTop: 10, marginBottom: 10 }}>interested in</Text>
<SegmentedControlTab
values={['Men', 'Women', 'Anyone']}
selectedIndex={this.state.profilePreferenceIndex}
onTabPress={profilePreferenceIndex => this.setState({ profilePreferenceIndex })}
borderRadius={30}
tabTextStyle={{ fontFamily: 'comfortaa.regular', fontSize: 18, color: Colors.primary }}
activeTabOpacity={0.5}
activeTabStyle={{ backgroundColor: Colors.primary }}
tabsContainerStyle={{ marginBottom: 10, marginTop: 10 }}
tabStyle={{ borderColor: Colors.primary }}
accessibilityLabels={['Man', 'Woman']}
/>
<Text style={{ fontFamily: 'comfortaa.regular', fontSize: 24, color: 'black', marginTop: 10, marginBottom: 10 }}>Profession</Text>
<Input
placeholder='Profession'
inputStyle={{ fontFamily: 'comfortaa.regular', fontSize: 18, color: 'black' }}
label='Profession'
labelStyle={{ fontFamily: 'Robota-Regular', fontSize: 14, color: Colors.greyLight }}
leftIcon={<Icon name='work' type='material-icon' size={18} color='black' />}
onChangeText={profileProfession => this.setState({ profileProfession })}
value={this.state.profileProfession}
containerStyle={{ marginBottom: 10, marginTop: 10 }}
autoCapitalize='words'
placeholderTextColor={Colors.greyLight}
inputContainerStyle={{ borderColor: 'black' }} />
<Text style={{ fontFamily: 'comfortaa.regular', fontSize: 24, color: 'black', marginTop: 10, marginBottom: 10 }}>Workplace</Text>
<Input
placeholder='Company Name'
inputStyle={{ fontFamily: 'comfortaa.regular', fontSize: 18, color: 'black' }}
label='Workplace'
labelStyle={{ fontFamily: 'Robota-Regular', fontSize: 14, color: Colors.greyLight }}
leftIcon={<Icon name='home' type='material-icon' size={18} color='black' />}
onChangeText={profileCompany => this.setState({ profileCompany })}
value={this.state.profileCompany}
containerStyle={{ marginBottom: 10, marginTop: 10 }}
autoCapitalize='words'
placeholderTextColor={Colors.greyLight}
inputContainerStyle={{ borderColor: 'black' }} />
<Button
title='Create Profile'
onPress={() => this.createProfile()}
type='solid'
titleStyle={{ fontFamily: 'comfortaa.regular', color: 'white', fontSize: 20 }}
containerStyle={{ width: '100%' }}
buttonStyle={{ borderRadius: 60, marginVertical: 40, backgroundColor: Colors.primary }}
disabled={this.state.disabled}
disabledStyle={{ backgroundColor: '#d6d4d4' }} />
</ScrollView>
</View>
)
}
}