-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
164 lines (149 loc) · 3.62 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
import React, {Component} from 'react';
import {
createStackNavigator, createAppContainer
} from 'react-navigation';
import {
StyleSheet, ScrollView, View, Text ,
PermissionsAndroid
} from 'react-native';
import TodoList from './components/todo-list';
import AddTodo from './components/add-todo';
const defaultNavigationOptions = {
headerStyle: {
backgroundColor: '#1564bf',
},
headerTintColor: 'white',
headerTitleStyle: {
fontWeight: 'bold',
color: 'white',
},
}
class TodoDetails extends Component {
static navigationOptions = {
...defaultNavigationOptions,
title: 'Todo'
}
render() {
const todo = this.props.navigation.getParam('todo');
return (
<View>
<Text>
{todo.text}
</Text>
<Text>
Created at: {todo.location}
</Text>
</View>
)
}
}
class Home extends Component {
static navigationOptions = {
...defaultNavigationOptions,
title: 'Todo App',
};
constructor(props) {
super(props);
const todo1 = {
id: 1, text: 'fazer o app bonitao o/',
};
const todo2 = {
id: 2, text: 'fazer o app maaais bonitao',
};
const todo3 = {
id: 3, text: 'lanchar',
};
this.state = {
idCount: 3,
todos: [todo1, todo2, todo3],
}
this.requestMapsPermission();
}
async requestMapsPermission() {
try {
const isGranted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
{
'title': 'Todo app location access',
'message': 'We need your location to know here you created a todo'
}
)
this.setState({
geolocationPermissionGranted: isGranted === 'granted',
})
} catch (err) {
console.error(err);
}
}
async setTodoLocation(id, coords) {
const { latitude, longitude } = coords;
try {
const response = await fetch(
`https://nominatim.openstreetmap.org/reverse.php?format=json&lat=${latitude}&lon=${longitude}&zoom=18`
);
if (response.ok) {
const data = await response.json();
const address = `${data.address.suburb}, ${data.address.city}`;
const { todos } = this.state;
todos
.find(todo => todo.id === id)
.location = address;
this.setState({
todos
})
} else {
throw JSON.stringify(response.json());
}
} catch(e) {
console.error(e);
}
}
addTodo(text) {
const id = this.state.idCount + 1;
this.setState({
todos: [{ id, text }].concat(this.state.todos),
idCount: id
});
if (this.state.geolocationPermissionGranted) {
navigator.geolocation.getCurrentPosition((pos) => {
this.setTodoLocation(id, pos.coords)
}, null, { enableHighAccuracy: true })
}
}
render() {
return (
<View style={styles.container}>
<ScrollView
contentContainerStyle={styles.scrollView}
>
<AddTodo add={text => this.addTodo(text)}/>
<TodoList
todoList={this.state.todos}
navigation={this.props.navigation}
/>
</ScrollView>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#F5FCFF',
},
scrollView: {
width: '100%',
justifyContent: 'flex-start',
alignItems: 'flex-start',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
}
});
const AppNavigator = createStackNavigator({
Home: { screen: Home },
TodoDetails: { screen: TodoDetails }
})
export default createAppContainer(AppNavigator);