From 4be82ee680c4dbc5ee4389ebd01ad18753ea53cf Mon Sep 17 00:00:00 2001 From: Kumar Damani Date: Sat, 9 Mar 2019 19:05:36 -0500 Subject: fixed having to press current location button arbitrarily many times before proceeding to next screen --- mobile/components/GreetingPage.js | 181 +++++++++++++++++++++----------------- 1 file changed, 98 insertions(+), 83 deletions(-) (limited to 'mobile') diff --git a/mobile/components/GreetingPage.js b/mobile/components/GreetingPage.js index e8da92e..9301fbc 100644 --- a/mobile/components/GreetingPage.js +++ b/mobile/components/GreetingPage.js @@ -5,96 +5,111 @@ import { createStackNavigator, createAppContainer } from 'react-navigation'; import { Location, Permissions, Constants } from 'expo'; export default class GreetingPage extends Component { - //Every component needs a render() function state = { - location: null, + isLoading: true, errorMessage: null, + locationResult: null, }; - componentWillMount() { - if (Platform.OS === 'android' && !Constants.isDevice) { - this.setState({ - errorMessage: 'This will not work on an android device', - }); - } else { + // runs as soon as page is loaded + componentDidMount() { + // immediately attempt to get the location this._getLocationAsync(); - } } _getLocationAsync = async () => { - let { status } = await Permissions.askAsync(Permissions.LOCATION); - if (status !== 'granted') { - this.setState({ - errorMessage: 'Permission to access location was denied', + // ask location services to allow location access to THIS app. + // Note: location services itself must still be enabled for this! + const {status} = await Permissions.askAsync(Permissions.LOCATION); + if (status === 'granted') { + // try to actually get the location asynchronously - a Promise + Location.getCurrentPositionAsync({enableHighAccuracy: true}).then((position) => { + this.setState({locationResult: position.coords}); + // since we are guaranteed to have the location here, its safe to load the full view + this.setState({isLoading: false}); + }).catch((e) => { + alert(e + ' Please make sure your location (GPS) is turned on.'); }); - } - let location = await Location.getCurrentPositionAsync({}); - this.setState({ location }); + } else { + // if the user pressed "Deny" or something on the promt + throw new Error('Location permission not granted'); + } }; moveToFormPage = () => { - this._getLocationAsync; - if (this.state.location === null) { - Alert.alert("There was a problem accessing your location"); - return; - } - const coordinates = [this.state.location.coords.longitude, this.state.location.coords.latitude]; - this.props.navigation.navigate('FormPage', {coordinates : coordinates}); - } + this.props.navigation.navigate('FormPage', + {coordinates : [ + this.state.locationResult.longitude, + this.state.locationResult.latitude] + }); + }; render() { + // acts like the way
does in JavaScript + //style={...} stores properties about where you want the component to be placed + //and how you want it to look. It's similar to adding CSS styles to HTML elements + //when creating web pages + + // initialy show a loading screen when trying to get location on load + if (this.state.isLoading) { + return ( + + + + Trying to get location... + + + + ) + } + return ( - // acts like the way
does in JavaScript - //style={...} stores properties about where you want the component to be placed - //and how you want it to look. It's similar to adding CSS styles to HTML elements - //when creating web pages - - - Ensure that location services are enabled! - - + + + Welcome to Helpthehome! + + - { this.moveToFormPage() - }}> + { this.moveToFormPage() }}> - + - - - Use Current Location - - + + + Use Current Location + + - - - + + + - + - + - - + + - - - Drop Pin - - + + + Drop Pin + + - - - + + + - - + + ); @@ -110,42 +125,42 @@ const styles = StyleSheet.create( { alignItems: 'stretch' }, titleContainer: { - flex: 3, + flex: 3, //position: 'absolute', marginTop: 205, //flexGrow: 1 }, title: { - color: '#FFF', - marginTop: 10, - textAlign: 'center', - opacity: 0.9, - fontSize: 18, + color: '#FFF', + marginTop: 10, + textAlign: 'center', + opacity: 0.9, + fontSize: 18, fontStyle: 'italic' }, current_location_title: { - color: '#FFF', - marginTop: 15, - textAlign: 'center', - opacity: 0.9, - fontSize: 20 + color: '#FFF', + marginTop: 15, + textAlign: 'center', + opacity: 0.9, + fontSize: 20 }, first_button_container: { - flex: 1, - //position: 'absolute', - //marginTop: 360, - backgroundColor: '#c0392b', - flexDirection: 'row', - justifyContent: 'center', - alignItems: 'stretch' + flex: 1, + //position: 'absolute', + //marginTop: 360, + backgroundColor: '#c0392b', + flexDirection: 'row', + justifyContent: 'center', + alignItems: 'stretch' }, press_button: { - flexDirection: 'column', - justifyContent: 'center', - alignItems: 'center' + flexDirection: 'column', + justifyContent: 'center', + alignItems: 'center' }, second_button_container: { - flex: 1, - backgroundColor: '#535c68' + flex: 1, + backgroundColor: '#535c68' } }) -- cgit v1.2.3