-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.js
executable file
·112 lines (93 loc) · 2.94 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
import React, {useState, useEffect} from 'react';
import { StyleSheet, FlatList, Text, View, Button, Linking } from 'react-native';
import * as Permissions from 'expo-permissions';
import * as Contacts from 'expo-contacts';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
function ContactsScreen({ navigation }) {
const [contacts, setContacts] = useState([]);
const [permissions, setPermissions] = useState(false);
const getPermissions = async () => {
const { status } = await Permissions.askAsync(Permissions.CONTACTS);
setPermissions(true);
};
const showContacts = async () => {
const contactList = await Contacts.getContactsAsync();
setContacts(contactList.data);
};
useEffect( () => {
getPermissions();
}, []);
const call = contact => {
let phoneNumber = contact.phoneNumbers[0].number.replace(/[\(\)\-\s+]/g, '');
console.log(contact.phoneNumbers)
let link = `tel:${phoneNumber}`;
Linking.canOpenURL(link).then(supported=> Linking.openURL(link)).catch(console.error);
}
return (
<View style={styles.container}>
<Button
onPress={showContacts}
title="Show Contacts"
/>
<Button
onPress={() => navigation.navigate('Location')}
title="Show Location"
/>
<View style={styles.section}>
<Button
onPress={() => navigation.navigate('Contacts')}
title="Return to Home"
/>
<FlatList
data={contacts}
keyExtractor={(item)=>item.id}
renderItem={({item})=>{
console.log(item);
return <Button style={styles.person} title={item.name + ''} onPress={()=> call(item) } />}
}/>
</View>
</View>
);
}
const styles = StyleSheet.create({
person: {
marginTop:'3em',
},
section: {
margin: 15,
flex: 1,
alignItems: 'flex-start',
justifyContent: 'flex-start',
},
container: {
alignItems: 'center',
backgroundColor: '#fff',
flex: 1,
justifyContent: 'center',
marginTop: 35,
},
});
function LocationScreen({ navigation }) {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Here lies the location data, if I had any.</Text>
<Button
onPress={() => navigation.navigate('Contacts')}
title="Return to Home"
/>
</View>
);
}
const Stack = createStackNavigator();
function App() {
return (
<NavigationContainer>
<Stack.Navigator initialRouteName="Contacts">
<Stack.Screen name="Contacts" component={ContactsScreen} />
<Stack.Screen name="Location" component={LocationScreen} />
</Stack.Navigator>
</NavigationContainer>
);
}
export default App;