Skip to content

Commit 61a6989

Browse files
author
akhenda
committed
fix(geolocation): fix location update bug
1 parent 9c92071 commit 61a6989

File tree

3 files changed

+53
-43
lines changed

3 files changed

+53
-43
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/containers/HomeScreen.js

Lines changed: 48 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
import React, { Component } from 'react';
22
import PropTypes from 'prop-types';
3-
import { View, PermissionsAndroid, Platform } from 'react-native';
43
import { connect } from 'react-redux';
54
import { Container } from 'native-base';
65
import Radar from 'react-native-radar';
76
import { Actions } from 'react-native-router-flux';
87
import RNAndroidLocationEnabler from 'react-native-android-location-enabler';
8+
import {
9+
View, PermissionsAndroid, Platform, StatusBar,
10+
} from 'react-native';
911

10-
import shallowCompare from 'src/utils/shallowCompare';
11-
import { updateLocation } from 'src/state/actions/app';
12-
import { getCategories, selectCategory, clearListings } from 'src/state/actions/listings';
1312
import SearchBar from 'src/components/SearchBar';
1413
import Salutation from 'src/components/Salutation';
14+
import shallowCompare from 'src/utils/shallowCompare';
15+
import { updateLocation } from 'src/state/actions/app';
1516
import CategoriesList from 'src/components/CategoriesList';
1617
import LoadingIndicator from 'src/components/LoadingIndicator';
1718
import AnimatedContentWrapper from 'src/components/AnimatedContentWrapper';
19+
import { getCategories, selectCategory, clearListings } from 'src/state/actions/listings';
1820

21+
import { colors } from 'src/theme';
1922
import styles from './styles/HomeScreenStyles';
2023

2124

@@ -32,47 +35,50 @@ class HomeScreen extends Component {
3235

3336
componentDidMount() {
3437
let granted;
35-
this.props.getCategories(this.props.token);
38+
const { user, loading, catLoading } = this.props;
3639

37-
const locationSetup = () => {
38-
// identify the user and request permissions
39-
const { user } = this.props;
40-
const description = user.name || user.username;
41-
42-
Radar.setUserId(String(user.id));
43-
Radar.setDescription(description);
44-
Radar.requestPermissions(false);
45-
46-
// track the user's location once in the foreground
47-
Radar.trackOnce().then((result) => {
48-
// do something with result.events, result.user.geofences
49-
this.props.updateLocation(result.location);
50-
}).catch(() => {
51-
// optionally, do something with err
52-
});
53-
54-
Radar.on('location', (result) => {
55-
this.props.updateLocation(result.location);
56-
});
57-
};
40+
this.props.getCategories(this.props.token);
5841

59-
if (Platform.OS === 'android') {
60-
RNAndroidLocationEnabler
61-
.promptForEnableLocationIfNeeded({ interval: 7000, fastInterval: 3000 })
62-
.then(async () => {
63-
// success => {alreadyEnabled: true, enabled: true, status: 'enabled'}
64-
granted = await PermissionsAndroid
65-
.request(PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION, {
66-
title: 'WAPI? App needs to access your location 📌',
67-
message: 'We need to access to your location '
68-
+ 'so that we can personalize your experience. 🙂',
69-
});
70-
if (granted && Object.keys(this.props.user) > 0) locationSetup();
71-
}).catch(() => {});
42+
if (!loading && !catLoading) {
43+
const locationSetup = () => {
44+
// identify the user and request permissions
45+
const description = user.name || user.username;
46+
47+
Radar.setUserId(String(user.id));
48+
Radar.setDescription(description);
49+
Radar.requestPermissions(false);
50+
51+
// track the user's location once in the foreground
52+
Radar.trackOnce().then((result) => {
53+
// do something with result.events, result.user.geofences
54+
this.props.updateLocation(result.location);
55+
}).catch(() => {
56+
// optionally, do something with err
57+
});
58+
59+
Radar.on('location', (result) => {
60+
this.props.updateLocation(result.location);
61+
});
62+
};
63+
64+
if (Platform.OS === 'android') {
65+
RNAndroidLocationEnabler
66+
.promptForEnableLocationIfNeeded({ interval: 7000, fastInterval: 3000 })
67+
.then(async () => {
68+
// success => {alreadyEnabled: true, enabled: true, status: 'enabled'}
69+
granted = await PermissionsAndroid
70+
.request(PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION, {
71+
title: 'WAPI? App needs to access your location 📌',
72+
message: 'We need to access to your location '
73+
+ 'so that we can personalize your experience. 🙂',
74+
});
75+
if (granted && Object.keys(this.props.user).length > 0) locationSetup();
76+
}).catch(() => {});
77+
}
78+
79+
if (Platform.OS === 'ios' && Object.keys(this.props.user).length > 0) locationSetup();
7280
}
7381

74-
if (Platform.OS === 'ios' && Object.keys(this.props.user) > 0) locationSetup();
75-
7682
this.props.clearListings();
7783
}
7884

@@ -116,6 +122,7 @@ class HomeScreen extends Component {
116122

117123
return (
118124
<Container style={styles.container}>
125+
<StatusBar translucent barStyle="light-content" backgroundColor={colors.statusBarTranslucent} />
119126
<AnimatedContentWrapper
120127
headerTitle="Discover"
121128
showToolbarRightButton={false}

src/state/reducers/app.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ const INITIAL_STATE = {
2525
shopping: false,
2626
volunteering: false,
2727
},
28-
currentLocation: null,
28+
currentLocation: {
29+
latitude: 0,
30+
longitude: 0,
31+
},
2932
favourites: [],
3033
};
3134

0 commit comments

Comments
 (0)