-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
151 lines (132 loc) · 4.25 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
import React ,{useEffect, useState} from 'react';
import {SafeAreaView,TouchableOpacity, PermissionsAndroid,ActivityIndicator, Image, Dimensions, StyleSheet,ScrollView,View,Text,StatusBar,FlatList, Touchable} from 'react-native';
import RenderItem from './src/FlatListItem'
import Contacts from 'react-native-contacts';
import faker from 'faker';
import { RecyclerListView, DataProvider, LayoutProvider } from 'recyclerlistview';
const SCREEN_WIDTH = Dimensions.get('window').width;
const App = () => {
const fakeData = []
const [list , setList] = useState([])
const [permission, setPermission] = useState(true);
const [contacts, setContact] = useState([])
useEffect(() =>{
PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.READ_CONTACTS).then(response => {
if(response){
Contacts.getAll().then(contacts1 => {
let items = []
contacts1.forEach((item,index) =>{
item.phoneNumbers.map((x, index) => {
items.push({
type: 'NORMAL',
item:{
id:`${index}${x.id}`,
image: `https://randomuser.me/api/portraits/${faker.helpers.randomize(['women', 'men'])}/${faker.random.number(60)}.jpg`,
name:item.displayName,
mobile_number:x.number.replace(/-|\s/g,""),
}
})
})
})
sortdata(items.sort((a, b) => a.item.name.toLowerCase() > b.item.name.toLowerCase()));
})
}else{
PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.READ_CONTACTS).then((result) => {
if(result === 'granted'){
setPermission(true);
}else{
setPermission(false)
alert("Please Grant Contacts Permission")
}
}).catch((error) =>{
console.log(error)
})
}
}).catch((error) =>{
console.log(error)
})
},[permission])
const sortdata = (items) =>{
setdataprovider(items.filter((v,i,a)=>a.findIndex(t=>(t.item.mobile_number === v.item.mobile_number))===i));
}
const setdataprovider = (fakeData) =>{
setContact(new DataProvider((r1, r2) => r1 !== r2).cloneWithRows(fakeData))
}
const layoutProvider = new LayoutProvider((i) => {
return contacts.getDataForIndex(i).type;
}, (type, dim) => {
switch (type) {
case 'NORMAL':
dim.width = SCREEN_WIDTH;
dim.height = 60;
break;
default:
dim.width = 0;
dim.height = 0;
break;
};
})
const rowRenderer = (type, data) => {
const { image, name, mobile_number, id} = data.item;
return (
<TouchableOpacity style={styles.listItem} onPress={() => console.log('Pressed')}>
<Image style={styles.image} source={{ uri:image }} />
<View style={styles.body}>
<Text style={styles.name}>{name}{" "}</Text>
<Text style={styles.description}>{mobile_number}</Text>
</View>
</TouchableOpacity>
)
}
if(contacts.length === 0){
return(
<View style={{flex:1, alignItems:"center", justifyContent:"center"}}>
<Text style={{color:"black"}}>loading</Text>
</View>
)
}
return (
<View style={styles.container}>
<RecyclerListView
style={{flex: 1}}
rowRenderer={rowRenderer}
dataProvider={contacts}
layoutProvider={layoutProvider}
renderAheadOffset={500 }
/>
</View>
);
}
export default App;
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#FFF',
minHeight: 1,
minWidth: 1,
},
body: {
marginLeft: 10,
marginRight: 10,
maxWidth: SCREEN_WIDTH - (80 + 10 + 20),
},
image: {
height: 40,
width: 40,
borderRadius:40
},
name: {
fontSize: 16,
fontWeight: 'bold',
},
description: {
fontSize: 14,
opacity: 0.5,
},
listItem: {
flexDirection: 'row',
margin: 5,
borderRadius:10,
backgroundColor:"#F0F0F0"
},
});