-
Notifications
You must be signed in to change notification settings - Fork 552
/
App.js
156 lines (140 loc) · 3.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
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
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow
*/
import React, { Component } from "react";
import {
PermissionsAndroid,
Platform,
SafeAreaView,
ScrollView,
StyleSheet,
Text,
View,
Image
} from "react-native";
import Contacts from "react-native-contacts";
import ListItem from "./components/ListItem";
import Avatar from "./components/Avatar";
import SearchBar from "./components/SearchBar";
type Props = {};
export default class App extends Component<Props> {
constructor(props) {
super(props);
this.search = this.search.bind(this);
this.state = {
contacts: []
};
}
async componentWillMount() {
if (Platform.OS === "android") {
PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.READ_CONTACTS, {
title: "Contacts",
message: "This app would like to view your contacts."
}).then(() => {
this.loadContacts();
});
} else {
this.loadContacts();
}
}
loadContacts() {
Contacts.getAll((err, contacts) => {
if (err === "denied") {
console.warn("Permission to access contacts was denied");
} else {
this.setState({ contacts });
}
});
}
search(text) {
const phoneNumberRegex = /\b[\+]?[(]?[0-9]{2,6}[)]?[-\s\.]?[-\s\/\.0-9]{3,15}\b/m;
const emailAddressRegex = /^(([^<>()[\].,;:\s@"]+(\.[^<>()[\].,;:\s@"]+)*)|(".+"))@(([^<>()[\].,;:\s@"]+\.)+[^<>()[\].,;:\s@"]{2,})$/i;
if (text === "" || text === null) {
this.loadContacts();
} else if (phoneNumberRegex.test(text)) {
Contacts.getContactsByPhoneNumber(text, (err, contacts) => {
this.setState({ contacts });
});
} else if (emailAddressRegex.test(text)) {
Contacts.getContactsByEmailAddress(text, (err, contacts) => {
this.setState({ contacts });
});
} else {
Contacts.getContactsMatchingString(text, (err, contacts) => {
this.setState({ contacts });
});
}
}
render() {
return (
<SafeAreaView style={styles.container}>
<View
style={{
paddingLeft: 100,
paddingRight: 100,
justifyContent: "center",
alignItems: "center"
}}
>
<Image
source={require("./logo.png")}
style={{
aspectRatio: 6,
resizeMode: "contain"
}}
/>
</View>
<SearchBar onChangeText={this.search} />
<ScrollView style={{ flex: 1 }}>
{this.state.contacts.map(contact => {
return (
<ListItem
leftElement={
<Avatar
img={
contact.hasThumbnail
? { uri: contact.thumbnailPath }
: undefined
}
placeholder={getAvatarInitials(
`${contact.givenName} ${contact.familyName}`
)}
width={40}
height={40}
/>
}
key={contact.recordID}
title={`${contact.givenName} ${contact.familyName}`}
description={`${contact.company}`}
onPress={() => Contacts.openExistingContact(contact, () => {})}
onDelete={() =>
Contacts.deleteContact(contact, () => {
this.loadContacts();
})
}
/>
);
})}
</ScrollView>
</SafeAreaView>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1
}
});
const getAvatarInitials = textString => {
if (!textString) return "";
const text = textString.trim();
const textSplit = text.split(" ");
if (textSplit.length <= 1) return text.charAt(0);
const initials =
textSplit[0].charAt(0) + textSplit[textSplit.length - 1].charAt(0);
return initials;
};