Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions example/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import {GiftedChat, Actions, Bubble} from 'react-native-gifted-chat';
import CustomActions from './CustomActions';
import CustomView from './CustomView';
import _ from 'lodash';

export default class Example extends React.Component {
constructor(props) {
Expand All @@ -27,6 +28,7 @@ export default class Example extends React.Component {
this.renderBubble = this.renderBubble.bind(this);
this.renderFooter = this.renderFooter.bind(this);
this.onLoadEarlier = this.onLoadEarlier.bind(this);
this.onLoadNewer = this.onLoadNewer.bind(this);

this._isAlright = null;
}
Expand All @@ -40,6 +42,10 @@ export default class Example extends React.Component {
});
}

componentDidMount() {
this.onLoadNewer();
}

componentWillUnmount() {
this._isMounted = false;
}
Expand All @@ -55,7 +61,7 @@ export default class Example extends React.Component {
if (this._isMounted === true) {
this.setState((previousState) => {
return {
messages: GiftedChat.prepend(previousState.messages, require('./data/old_messages.js')),
messages: require('./data/old_messages.js').concat(previousState.messages),
loadEarlier: false,
isLoadingEarlier: false,
};
Expand All @@ -64,6 +70,19 @@ export default class Example extends React.Component {
}, 1000); // simulating network
}

onLoadNewer() {
setTimeout(() => {
if (this._isMounted) {
this.setState((previousState) => {
return {
messages: _.cloneDeep(previousState.messages).concat(require('./data/new_messages')()),
};
});
}
}, 1000);
}


onSend(messages = []) {
this.setState((previousState) => {
return {
Expand All @@ -80,7 +99,7 @@ export default class Example extends React.Component {
if ((messages[0].image || messages[0].location) || !this._isAlright) {
this.setState((previousState) => {
return {
typingText: 'React Native is typing'
typingText: 'React Native is typing',
};
});
}
Expand Down Expand Up @@ -193,6 +212,7 @@ export default class Example extends React.Component {
onSend={this.onSend}
loadEarlier={this.state.loadEarlier}
onLoadEarlier={this.onLoadEarlier}
onLoadNewer={this.onLoadNewer}
isLoadingEarlier={this.state.isLoadingEarlier}

user={{
Expand Down
169 changes: 169 additions & 0 deletions example/data/new_messages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
module.exports = function () {
return [
{
_id: Math.round(Math.random() * 1000000),
text: 'New message',
createdAt: new Date(Date.UTC(2016, 7, 30, 17, 20, 0)),
user: {
_id: 1,
name: 'Developer',
},
sent: true,
received: true,
// location: {
// latitude: 48.864601,
// longitude: 2.398704
// },
},
{
_id: Math.round(Math.random() * 1000000),
text: 'New message',
createdAt: new Date(Date.UTC(2016, 7, 30, 17, 20, 0)),
user: {
_id: 1,
name: 'Developer',
},
sent: true,
received: true,
// location: {
// latitude: 48.864601,
// longitude: 2.398704
// },
},
{
_id: Math.round(Math.random() * 1000000),
text: 'New message',
createdAt: new Date(Date.UTC(2016, 7, 30, 17, 20, 0)),
user: {
_id: 1,
name: 'Developer',
},
sent: true,
received: true,
// location: {
// latitude: 48.864601,
// longitude: 2.398704
// },
},
{
_id: Math.round(Math.random() * 1000000),
text: 'New message',
createdAt: new Date(Date.UTC(2016, 7, 30, 17, 20, 0)),
user: {
_id: 1,
name: 'Developer',
},
sent: true,
received: true,
// location: {
// latitude: 48.864601,
// longitude: 2.398704
// },
},
{
_id: Math.round(Math.random() * 1000000),
text: 'New message',
createdAt: new Date(Date.UTC(2016, 7, 30, 17, 20, 0)),
user: {
_id: 1,
name: 'Developer',
},
sent: true,
received: true,
// location: {
// latitude: 48.864601,
// longitude: 2.398704
// },
},
{
_id: Math.round(Math.random() * 1000000),
text: 'New message',
createdAt: new Date(Date.UTC(2016, 7, 30, 17, 20, 0)),
user: {
_id: 1,
name: 'Developer',
},
sent: true,
received: true,
// location: {
// latitude: 48.864601,
// longitude: 2.398704
// },
},
{
_id: Math.round(Math.random() * 1000000),
text: 'New message',
createdAt: new Date(Date.UTC(2016, 7, 30, 17, 20, 0)),
user: {
_id: 1,
name: 'Developer',
},
sent: true,
received: true,
// location: {
// latitude: 48.864601,
// longitude: 2.398704
// },
},
{
_id: Math.round(Math.random() * 1000000),
text: 'New message',
createdAt: new Date(Date.UTC(2016, 7, 30, 17, 20, 0)),
user: {
_id: 1,
name: 'Developer',
},
sent: true,
received: true,
// location: {
// latitude: 48.864601,
// longitude: 2.398704
// },
},
{
_id: Math.round(Math.random() * 1000000),
text: 'New message',
createdAt: new Date(Date.UTC(2016, 7, 30, 17, 20, 0)),
user: {
_id: 1,
name: 'Developer',
},
sent: true,
received: true,
// location: {
// latitude: 48.864601,
// longitude: 2.398704
// },
},
{
_id: Math.round(Math.random() * 1000000),
text: 'New message',
createdAt: new Date(Date.UTC(2016, 7, 30, 17, 20, 0)),
user: {
_id: 1,
name: 'Developer',
},
sent: true,
received: true,
// location: {
// latitude: 48.864601,
// longitude: 2.398704
// },
},
{
_id: Math.round(Math.random() * 1000000),
text: 'New message',
createdAt: new Date(Date.UTC(2016, 7, 30, 17, 20, 0)),
user: {
_id: 1,
name: 'Developer',
},
sent: true,
received: true,
// location: {
// latitude: 48.864601,
// longitude: 2.398704
// },
},
];
}
1 change: 1 addition & 0 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"sync": "rm -rf ./node_modules/react-native-gifted-chat; sane '/usr/bin/rsync -v -a --exclude .git --exclude example --exclude __tests__ --exclude node_modules ../ ./node_modules/react-native-gifted-chat/' .. --glob='{**/*.json,**/*.js}'"
},
"dependencies": {
"lodash": "^4.17.4",
"prop-types": "15.5.10",
"react": "16.0.0-alpha.12",
"react-native": "0.46.3",
Expand Down
2 changes: 2 additions & 0 deletions src/GiftedChat.js
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,7 @@ GiftedChat.defaultProps = {
}),
loadEarlier: false,
onLoadEarlier: () => {},
onLoadNewer: () => {},
isLoadingEarlier: false,
renderLoading: null,
renderLoadEarlier: null,
Expand Down Expand Up @@ -603,6 +604,7 @@ GiftedChat.propTypes = {
isAnimated: PropTypes.bool,
loadEarlier: PropTypes.bool,
onLoadEarlier: PropTypes.func,
onLoadNewer: PropTypes.func,
isLoadingEarlier: PropTypes.bool,
renderLoading: PropTypes.func,
renderLoadEarlier: PropTypes.func,
Expand Down
21 changes: 19 additions & 2 deletions src/MessageContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ export default class MessageContainer extends React.Component {
this.renderRow = this.renderRow.bind(this);
this.renderFooter = this.renderFooter.bind(this);
this.renderLoadEarlier = this.renderLoadEarlier.bind(this);
this.onScroll = this.onScroll.bind(this);

this.state = {
messages: this.prepareMessages(props.messages),
isLoadingNewer: false,
};
}

Expand Down Expand Up @@ -112,6 +114,17 @@ export default class MessageContainer extends React.Component {
return <Message {...messageProps}/>;
}

async onScroll({nativeEvent}) {
const offset = nativeEvent.contentSize.height - nativeEvent.contentOffset.y;
const threshold = offset / nativeEvent.layoutMeasurement.height - 1;
if (!this.state.isLoadingNewer && threshold <= this.props.onTopReachedThreshold) {
await this.setState({isLoadingNewer: true});
await this.props.onLoadNewer();
await this.setState({isLoadingNewer: false});
//console.log('!!!!!!!!!!!!!!', offset, threshold);
}
}

keyExtractor = (item, index) => item._id;

render() {
Expand All @@ -131,6 +144,8 @@ export default class MessageContainer extends React.Component {
ListFooterComponent={this.renderLoadEarlier}
onEndReached={this.props.onTopReached}
onEndReachedThreshold={this.props.onTopReachedThreshold}
onScroll={this.onScroll}
scrollEventThrottle={500}
/>
</View>
);
Expand All @@ -142,8 +157,9 @@ MessageContainer.defaultProps = {
user: {},
renderFooter: null,
renderMessage: null,
onLoadEarlier: () => {
},
onLoadEarlier: () => {},
onLoadNewer: () => {},
onTopReachedThreshold: 0.5,
};

MessageContainer.propTypes = {
Expand All @@ -152,6 +168,7 @@ MessageContainer.propTypes = {
renderFooter: PropTypes.func,
renderMessage: PropTypes.func,
onLoadEarlier: PropTypes.func,
onLoadNewer: PropTypes.func,
flatListProps: PropTypes.object,
flatListKeyboardProps: PropTypes.object,
onTopReached: PropTypes.func,
Expand Down