aboutsummaryrefslogtreecommitdiff
path: root/mobile/components/GreetingPage.js
blob: 4f681323bef15fd430e6c41d54b1f73f69da97c2 (plain)
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
import React, { Component } from 'react';
import { Text, View, Alert, StyleSheet, Button, AppRegistry, Navigator, TouchableHighlight, Platform} from 'react-native';
import { Ionicons } from '@expo/vector-icons';
import { createStackNavigator, createAppContainer } from 'react-navigation';
import { Location, Permissions, Constants } from 'expo';

export default class GreetingPage extends Component {
  state = {
    isLoading: true,
    errorMessage: null,
    locationResult: null,
  };

  // runs as soon as page is loaded
  componentDidMount() {
      // immediately attempt to get the location
      this._getLocationAsync();
  }

  _getLocationAsync = async () => {
    // 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.');
      });

    } else {
      // if the user pressed "Deny" or something on the promt
      throw new Error('Location permission not granted');
    }
  };

  moveToFormPage = () => {
    this.props.navigation.navigate('FormPage',
      {coordinates : [
          this.state.locationResult.longitude,
          this.state.locationResult.latitude]
      });
  };


  render() {
    //<View> acts like the way <div> 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 (
        <View style={styles.container}>
          <View style={styles.titleContainer}>
            <Text style={styles.title}>
              Trying to get location...
            </Text>
          </View>
        </View>
      )
    }

    return (

      <View style={styles.container}>

        <View style={styles.titleContainer}>
          <Text style={styles.title}>
            Welcome to Helpthehome!
          </Text>
        </View>

        <TouchableHighlight
          style={styles.first_button_container}
          onPress={() => { this.moveToFormPage() }}>

          <View style={styles.press_button}>

            <View padding={15}>
            <Text style={styles.current_location_title}>
              Use Current Location
            </Text>
            </View>

            <View marginTop = {5}>
              <Ionicons name="md-locate" size={40} color="white"/>
            </View>

          </View>

        </TouchableHighlight>

        <TouchableHighlight disabled style={styles.second_button_container}>
          <View style={styles.press_button}>

            <View padding={15}>
             <Text style={styles.current_location_title}>
              Drop Pin
             </Text>
            </View>

            <View>
              <Ionicons name="md-pin" size={40} color="white"/>
            </View>

          </View>
        </TouchableHighlight>

      </View>
    );
  }
}


const styles = StyleSheet.create( {
  container: {
    flex: 1,
    flexDirection: 'column',
    backgroundColor: '#40739e',
    alignItems: 'stretch'
  },
  titleContainer: {
    flex: 3,
    //position: 'absolute',
    marginTop: 205,
    //flexGrow: 1
  },
  title: {
    color: '#FFF',
    marginTop: 10,
    textAlign: 'center',
    opacity: 0.9,
    fontSize: 18,
    fontStyle: 'italic'
  },
  current_location_title: {
    color: '#FFF',
    textAlign: 'center',
    opacity: 0.9,
    fontSize: 20
  },
  first_button_container: {
    flex: 1,
    //position: 'absolute',
    //marginTop: 360,
    backgroundColor: '#c0392b',
    flexDirection: 'row',
    justifyContent: 'center',
    alignItems: 'stretch'
  },
  press_button: {
    flexDirection: 'row',
    justifyContent: 'space-between',
    // padding:5,
    alignItems: 'center'
  },
  second_button_container: {
    flex: 1,
    backgroundColor: '#535c68',
    flexDirection: 'row',
    justifyContent: 'center',
    alignItems: 'stretch'
  }
})