-
Notifications
You must be signed in to change notification settings - Fork 692
/
ConversationListScreen.js
196 lines (172 loc) · 5.39 KB
/
ConversationListScreen.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import _ from 'lodash';
import PropTypes from 'prop-types';
import React, {Component, PureComponent} from 'react';
import {StyleSheet, Alert, FlatList} from 'react-native';
import {gestureHandlerRootHOC} from 'react-native-gesture-handler';
import {ThemeManager, Colors, ListItem, Text, Avatar, AvatarHelper, Drawer, Button} from 'react-native-ui-lib'; //eslint-disable-line
import conversations from '../../data/conversations';
const collectionsIcon = require('../../assets/icons/collections.png');
const starIcon = require('../../assets/icons/star.png');
const shareIcon = require('../../assets/icons/share.png');
const batchSize = 10;
class ConversationListScreen extends Component {
constructor(props) {
super(props);
this.lastIndex = undefined;
this.refArray = [];
this.batchCounter = 0;
this.state = {
items: this.createItems({min: 0, max: batchSize})
};
}
createItems(batch) {
const data = conversations.slice(batch.min, batch.max);
const map = _.map(data, (item, index) => {
const initials = AvatarHelper.getInitials(item.name);
const avatarBadgeProps = {backgroundColor: Number(index) < 3 ? Colors.green30 : undefined};
const buttonPress = () => Alert.alert('Badge button press');
const listOnPress = () => Alert.alert(`Pressed on contact #${index + 1}`);
const imageSource = item.thumbnail ? {uri: item.thumbnail} : null;
const rightButtons = [
{
text: 'More',
icon: shareIcon,
background: Colors.dark60,
onPress: () => Alert.alert(`More button pressed for item #${item.name}`)
},
{
text: 'Archive',
icon: collectionsIcon,
background: Colors.blue30,
onPress: () => Alert.alert(`Archive button pressed for item #${item.name}`)
},
];
const leftButton = {
text: 'Read',
icon: starIcon,
background: Colors.green30,
onPress: () => Alert.alert(`Read button pressed for item #${item.name}`)
};
return {
...item,
initials,
avatarBadgeProps,
buttonPress,
listOnPress,
imageSource,
rightButtons,
leftButton
};
});
return map;
}
getNewItems() {
this.batchCounter++;
const newItems = this.createItems({
min: batchSize * this.batchCounter,
max: batchSize + (batchSize * this.batchCounter)
});
const items = _.concat(this.state.items, newItems);
this.setState({items});
}
closeLast(index) {
if (this.lastIndex !== undefined && this.lastIndex !== index) {
this.closeDrawer(this.lastIndex);
}
this.lastIndex = index;
}
closeDrawer(index) {
this.refArray[index].closeDrawer();
}
addRef = (ref, index) => {
this.refArray[index] = ref;
}
onEndReached = () => {
this.getNewItems();
}
onSwipeableWillOpen = (props) => {
this.closeLast(props.index);
}
renderItem = ({item, index}) => {
return <ContactItem item={item} index={index} addRef={this.addRef} onSwipeableWillOpen={this.onSwipeableWillOpen}/>
}
keyExtractor = (item, index) => `${item.name}-${index}`;
render() {
return (
<FlatList
data={this.state.items}
renderItem={this.renderItem}
keyExtractor={this.keyExtractor}
onEndReached={this.onEndReached}
/>
);
}
}
class ContactItem extends PureComponent {
static propTypes = {
item: PropTypes.object,
index: PropTypes.number,
addRef: PropTypes.func,
onSwipeableWillOpen: PropTypes.func
}
render() {
const {item, index, addRef, onSwipeableWillOpen} = this.props;
return (
<Drawer
migrate
leftItem={item.leftButton}
rightItems={item.rightButtons}
// itemsMinWidth={80}
ref={r => addRef(r, index)}
index={index} // sent for the 'closeLast' functionality
onSwipeableWillOpen={onSwipeableWillOpen} // sent for the 'closeLast' functionality
>
<ListItem
height={75.8}
onPress={item.listOnPress}
>
<ListItem.Part left>
<Avatar
size={54}
imageSource={item.imageSource}
label={item.initials}
badgeProps={item.avatarBadgeProps}
containerStyle={styles.avatar}
/>
</ListItem.Part>
<ListItem.Part middle column containerStyle={styles.border}>
<ListItem.Part containerStyle={styles.middle}>
<Text style={styles.text} text70 color={Colors.dark10} numberOfLines={1}>{item.name}</Text>
<Text style={styles.subtitle} text90 color={Colors.dark50}>{item.timestamp}</Text>
</ListItem.Part>
<ListItem.Part>
<Text style={styles.text} text80 color={Colors.dark40} numberOfLines={1}>{item.text}</Text>
{item.count > 0 && <Button size={'small'} label={item.count} onPress={item.buttonPress}/>}
</ListItem.Part>
</ListItem.Part>
</ListItem>
</Drawer>
);
}
}
const styles = StyleSheet.create({
border: {
borderBottomWidth: StyleSheet.hairlineWidth,
borderColor: ThemeManager.dividerColor,
paddingRight: 17
},
avatar: {
marginHorizontal: 18
},
middle: {
marginBottom: 3
},
text: {
flex: 1,
marginRight: 10
},
subtitle: {
marginTop: 2
}
});
export default gestureHandlerRootHOC(ConversationListScreen);