Load more messages. Closes #15

This commit is contained in:
Rodrigo Nascimento 2017-08-10 20:21:46 -03:00
parent c7ed4a5fcd
commit 0541a13233
2 changed files with 47 additions and 4 deletions

View File

@ -104,10 +104,14 @@ const RocketChat = {
}); });
}, },
loadMessagesForRoom(rid, cb) { loadMessagesForRoom(rid, end, cb) {
Meteor.call('loadHistory', rid, null, 50, (err, data) => { Meteor.call('loadHistory', rid, end, 20, (err, data) => {
if (err) { if (err) {
console.error(err); console.error(err);
if (cb) {
cb({ end: true });
}
return;
} }
realm.write(() => { realm.write(() => {
@ -119,7 +123,11 @@ const RocketChat = {
}); });
if (cb) { if (cb) {
cb(); if (data.messages.length < 20) {
cb({ end: true });
} else {
cb({ end: false });
}
} }
}); });

View File

@ -27,6 +27,12 @@ const styles = StyleSheet.create({
margin: 5, margin: 5,
textAlign: 'center', textAlign: 'center',
color: '#a00' color: '#a00'
},
header: {
transform: [{ scaleY: -1 }],
textAlign: 'center',
padding: 5,
color: '#ccc'
} }
}); });
@ -57,7 +63,7 @@ export default class RoomView extends React.Component {
const late = setTimeout(() => this.setState({ const late = setTimeout(() => this.setState({
loaded: false loaded: false
}), 1000); }), 1000);
RocketChat.loadMessagesForRoom(this.rid, () => { RocketChat.loadMessagesForRoom(this.rid, null, () => {
clearTimeout(late); clearTimeout(late);
this.setState({ this.setState({
loaded: true loaded: true
@ -70,6 +76,22 @@ export default class RoomView extends React.Component {
realm.removeListener('change', this.updateState); realm.removeListener('change', this.updateState);
} }
onEndReached = () => {
if (this.state.dataSource.length && this.state.loaded && this.state.loadingMore !== true && this.state.end !== true) {
this.setState({
...this.state,
loadingMore: true
});
RocketChat.loadMessagesForRoom(this.rid, this.state.dataSource[this.state.dataSource.length - 1].ts, ({ end }) => {
this.setState({
...this.state,
loadingMore: false,
end
});
});
}
}
getMessages = () => realm.objects('messages').filtered('_server.id = $0 AND rid = $1', RocketChat.currentServer, this.rid).sorted('ts', true) getMessages = () => realm.objects('messages').filtered('_server.id = $0 AND rid = $1', RocketChat.currentServer, this.rid).sorted('ts', true)
updateState = () => { updateState = () => {
@ -129,6 +151,16 @@ export default class RoomView extends React.Component {
); );
} }
renderHeader = () => {
if (this.state.loadingMore) {
return <Text style={styles.header}>Loading more messages...</Text>;
}
if (this.state.end) {
return <Text style={styles.header}>Start of conversation</Text>;
}
}
render() { render() {
return ( return (
<KeyboardView style={styles.container} keyboardVerticalOffset={64}> <KeyboardView style={styles.container} keyboardVerticalOffset={64}>
@ -140,6 +172,9 @@ export default class RoomView extends React.Component {
extraData={this.state} extraData={this.state}
renderItem={this.renderItem} renderItem={this.renderItem}
keyExtractor={item => item._id} keyExtractor={item => item._id}
onEndReached={this.onEndReached}
onEndReachedThreshold={0.1}
ListFooterComponent={this.renderHeader()}
/> />
{this.renderFooter()} {this.renderFooter()}
</KeyboardView> </KeyboardView>