2017-08-04 00:34:37 +00:00
|
|
|
import React from 'react';
|
2017-08-05 18:16:32 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2017-09-21 17:08:00 +00:00
|
|
|
import { Text, View, StyleSheet, Button, InteractionManager } from 'react-native';
|
2017-08-11 18:18:09 +00:00
|
|
|
import { ListView } from 'realm/react-native';
|
2017-08-13 01:35:09 +00:00
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import { bindActionCreators } from 'redux';
|
|
|
|
|
|
|
|
import * as actions from '../actions';
|
2017-08-17 06:28:41 +00:00
|
|
|
import { messagesRequest } from '../actions/messages';
|
2017-08-09 01:40:55 +00:00
|
|
|
import realm from '../lib/realm';
|
2017-08-09 20:18:00 +00:00
|
|
|
import RocketChat from '../lib/rocketchat';
|
2017-09-01 19:17:53 +00:00
|
|
|
import Message from '../containers/Message';
|
|
|
|
import MessageBox from '../containers/MessageBox';
|
|
|
|
import KeyboardView from '../presentation/KeyboardView';
|
2017-08-14 14:15:37 +00:00
|
|
|
|
2017-09-21 17:08:00 +00:00
|
|
|
const ds = new ListView.DataSource({ rowHasChanged: (r1, r2) => r1._id !== r2._id });
|
2017-08-04 00:34:37 +00:00
|
|
|
const styles = StyleSheet.create({
|
|
|
|
container: {
|
2017-08-13 20:35:30 +00:00
|
|
|
flex: 1,
|
|
|
|
backgroundColor: '#fff'
|
2017-08-04 00:34:37 +00:00
|
|
|
},
|
2017-08-07 00:34:35 +00:00
|
|
|
list: {
|
|
|
|
flex: 1,
|
|
|
|
transform: [{ scaleY: -1 }]
|
|
|
|
},
|
2017-08-04 00:34:37 +00:00
|
|
|
separator: {
|
|
|
|
height: 1,
|
|
|
|
backgroundColor: '#CED0CE'
|
2017-08-09 20:08:50 +00:00
|
|
|
},
|
|
|
|
bannerContainer: {
|
|
|
|
backgroundColor: 'orange'
|
|
|
|
},
|
|
|
|
bannerText: {
|
|
|
|
margin: 5,
|
|
|
|
textAlign: 'center',
|
|
|
|
color: '#a00'
|
2017-08-10 23:21:46 +00:00
|
|
|
},
|
|
|
|
header: {
|
|
|
|
transform: [{ scaleY: -1 }],
|
|
|
|
textAlign: 'center',
|
|
|
|
padding: 5,
|
|
|
|
color: '#ccc'
|
2017-08-04 00:34:37 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-09-25 13:15:28 +00:00
|
|
|
@connect(
|
|
|
|
state => ({
|
|
|
|
server: state.server.server,
|
|
|
|
Site_Url: state.settings.Site_Url,
|
|
|
|
Message_TimeFormat: state.settings.Message_TimeFormat,
|
|
|
|
loading: state.messages.isFetching
|
|
|
|
}),
|
|
|
|
dispatch => ({
|
|
|
|
actions: bindActionCreators(actions, dispatch),
|
|
|
|
getMessages: rid => dispatch(messagesRequest({ rid }))
|
|
|
|
})
|
|
|
|
)
|
2017-08-05 18:16:32 +00:00
|
|
|
export default class RoomView extends React.Component {
|
|
|
|
static propTypes = {
|
2017-09-21 17:08:00 +00:00
|
|
|
navigation: PropTypes.object.isRequired,
|
2017-08-22 01:24:41 +00:00
|
|
|
getMessages: PropTypes.func.isRequired,
|
2017-08-12 20:52:55 +00:00
|
|
|
rid: PropTypes.string,
|
|
|
|
sid: PropTypes.string,
|
2017-08-13 01:35:09 +00:00
|
|
|
name: PropTypes.string,
|
2017-08-13 23:45:47 +00:00
|
|
|
server: PropTypes.string,
|
2017-08-14 00:03:33 +00:00
|
|
|
Site_Url: PropTypes.string,
|
2017-08-22 01:24:41 +00:00
|
|
|
Message_TimeFormat: PropTypes.string,
|
|
|
|
loading: PropTypes.bool
|
2017-09-25 13:15:28 +00:00
|
|
|
};
|
2017-08-05 18:16:32 +00:00
|
|
|
|
2017-08-04 00:34:37 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2017-09-21 17:08:00 +00:00
|
|
|
|
|
|
|
this.sid = props.navigation.state.params.room.sid;
|
2017-09-25 13:15:28 +00:00
|
|
|
this.rid =
|
|
|
|
props.rid ||
|
|
|
|
props.navigation.state.params.room.rid ||
|
|
|
|
realm.objectForPrimaryKey('subscriptions', this.sid).rid;
|
|
|
|
|
|
|
|
this.data = realm
|
|
|
|
.objects('messages')
|
|
|
|
.filtered('_server.id = $0 AND rid = $1', this.props.server, this.rid)
|
|
|
|
.sorted('ts', true);
|
2017-08-07 18:42:02 +00:00
|
|
|
this.state = {
|
2017-09-21 17:08:00 +00:00
|
|
|
slow: false,
|
|
|
|
dataSource: [],
|
2017-08-10 16:49:15 +00:00
|
|
|
loaded: true,
|
2017-08-12 20:52:55 +00:00
|
|
|
joined: typeof props.rid === 'undefined'
|
2017-08-07 18:42:02 +00:00
|
|
|
};
|
2017-08-07 00:34:35 +00:00
|
|
|
}
|
2017-08-04 00:34:37 +00:00
|
|
|
|
2017-08-07 18:42:02 +00:00
|
|
|
componentWillMount() {
|
2017-09-21 17:08:00 +00:00
|
|
|
this.props.navigation.setParams({
|
2017-09-25 13:15:28 +00:00
|
|
|
title:
|
|
|
|
this.props.name ||
|
|
|
|
this.props.navigation.state.params.room.name ||
|
|
|
|
realm.objectForPrimaryKey('subscriptions', this.sid).name
|
2017-09-21 17:08:00 +00:00
|
|
|
});
|
|
|
|
this.timer = setTimeout(() => this.setState({ slow: true }), 5000);
|
2017-08-17 06:28:41 +00:00
|
|
|
this.props.getMessages(this.rid);
|
|
|
|
this.data.addListener(this.updateState);
|
2017-09-21 17:08:00 +00:00
|
|
|
this.state.dataSource = ds.cloneWithRows(this.data);
|
2017-08-11 18:18:09 +00:00
|
|
|
}
|
|
|
|
componentDidMount() {
|
2017-09-21 17:08:00 +00:00
|
|
|
InteractionManager.runAfterInteractions(() => RocketChat.readMessages(this.rid));
|
|
|
|
}
|
|
|
|
componentDidUpdate() {
|
|
|
|
return !this.props.loading && clearTimeout(this.timer);
|
2017-08-07 00:34:35 +00:00
|
|
|
}
|
|
|
|
componentWillUnmount() {
|
2017-09-21 17:08:00 +00:00
|
|
|
clearTimeout(this.timer);
|
|
|
|
this.data.removeAllListeners();
|
2017-08-04 00:34:37 +00:00
|
|
|
}
|
|
|
|
|
2017-08-10 23:21:46 +00:00
|
|
|
onEndReached = () => {
|
2017-08-13 02:15:00 +00:00
|
|
|
const rowCount = this.state.dataSource.getRowCount();
|
2017-09-25 13:15:28 +00:00
|
|
|
if (
|
|
|
|
rowCount &&
|
|
|
|
this.state.loaded &&
|
|
|
|
this.state.loadingMore !== true &&
|
|
|
|
this.state.end !== true
|
|
|
|
) {
|
2017-08-10 23:21:46 +00:00
|
|
|
this.setState({
|
2017-08-11 18:18:09 +00:00
|
|
|
// ...this.state,
|
2017-08-10 23:21:46 +00:00
|
|
|
loadingMore: true
|
|
|
|
});
|
2017-08-13 02:15:00 +00:00
|
|
|
|
|
|
|
const lastRowData = this.data[rowCount - 1];
|
|
|
|
RocketChat.loadMessagesForRoom(this.rid, lastRowData.ts, ({ end }) => {
|
2017-08-10 23:21:46 +00:00
|
|
|
this.setState({
|
2017-08-11 18:18:09 +00:00
|
|
|
// ...this.state,
|
2017-08-10 23:21:46 +00:00
|
|
|
loadingMore: false,
|
|
|
|
end
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2017-09-25 13:15:28 +00:00
|
|
|
};
|
2017-08-10 23:21:46 +00:00
|
|
|
|
2017-09-21 17:08:00 +00:00
|
|
|
updateState = () => {
|
2017-08-09 01:40:55 +00:00
|
|
|
this.setState({
|
2017-08-11 18:18:09 +00:00
|
|
|
dataSource: ds.cloneWithRows(this.data)
|
2017-08-09 01:40:55 +00:00
|
|
|
});
|
2017-09-21 17:08:00 +00:00
|
|
|
};
|
2017-08-04 00:34:37 +00:00
|
|
|
|
2017-08-09 20:18:00 +00:00
|
|
|
sendMessage = message => RocketChat.sendMessage(this.rid, message);
|
2017-08-09 01:40:55 +00:00
|
|
|
|
2017-09-21 17:08:00 +00:00
|
|
|
joinRoom = async() => {
|
|
|
|
await RocketChat.joinRoom(this.props.rid);
|
|
|
|
this.setState({
|
|
|
|
joined: true
|
|
|
|
});
|
2017-08-10 16:16:32 +00:00
|
|
|
};
|
|
|
|
|
2017-09-25 13:15:28 +00:00
|
|
|
renderBanner = () =>
|
|
|
|
(this.state.slow && this.props.loading ? (
|
2017-08-17 06:28:41 +00:00
|
|
|
<View style={styles.bannerContainer}>
|
|
|
|
<Text style={styles.bannerText}>Loading new messages...</Text>
|
|
|
|
</View>
|
2017-09-25 13:15:28 +00:00
|
|
|
) : null);
|
2017-08-10 16:16:32 +00:00
|
|
|
|
2017-08-09 01:40:55 +00:00
|
|
|
renderItem = ({ item }) => (
|
|
|
|
<Message
|
|
|
|
id={item._id}
|
|
|
|
item={item}
|
2017-08-13 23:45:47 +00:00
|
|
|
baseUrl={this.props.Site_Url}
|
2017-08-14 00:03:33 +00:00
|
|
|
Message_TimeFormat={this.props.Message_TimeFormat}
|
2017-08-09 01:40:55 +00:00
|
|
|
/>
|
|
|
|
);
|
2017-08-07 00:34:35 +00:00
|
|
|
|
2017-09-25 13:15:28 +00:00
|
|
|
renderSeparator = () => <View style={styles.separator} />;
|
2017-08-10 16:16:32 +00:00
|
|
|
|
|
|
|
renderFooter = () => {
|
|
|
|
if (!this.state.joined) {
|
2017-08-09 20:08:50 +00:00
|
|
|
return (
|
2017-08-10 16:16:32 +00:00
|
|
|
<View>
|
|
|
|
<Text>You are in preview mode.</Text>
|
|
|
|
<Button title='Join' onPress={this.joinRoom} />
|
2017-08-09 20:08:50 +00:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
2017-09-25 13:15:28 +00:00
|
|
|
return <MessageBox ref={box => (this.box = box)} onSubmit={this.sendMessage} rid={this.rid} />;
|
|
|
|
};
|
2017-08-09 20:08:50 +00:00
|
|
|
|
2017-08-10 23:21:46 +00:00
|
|
|
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>;
|
|
|
|
}
|
2017-09-25 13:15:28 +00:00
|
|
|
};
|
2017-08-10 23:21:46 +00:00
|
|
|
|
2017-08-04 00:34:37 +00:00
|
|
|
render() {
|
|
|
|
return (
|
2017-08-14 14:15:37 +00:00
|
|
|
<KeyboardView style={styles.container} keyboardVerticalOffset={64}>
|
2017-08-09 20:08:50 +00:00
|
|
|
{this.renderBanner()}
|
2017-08-11 18:18:09 +00:00
|
|
|
<ListView
|
2017-08-11 19:57:09 +00:00
|
|
|
enableEmptySections
|
2017-08-04 00:34:37 +00:00
|
|
|
style={styles.list}
|
2017-08-13 02:15:00 +00:00
|
|
|
onEndReachedThreshold={10}
|
|
|
|
renderFooter={this.renderHeader}
|
2017-08-11 18:18:09 +00:00
|
|
|
onEndReached={this.onEndReached}
|
|
|
|
dataSource={this.state.dataSource}
|
|
|
|
renderRow={item => this.renderItem({ item })}
|
2017-08-15 19:28:46 +00:00
|
|
|
initialListSize={10}
|
2017-08-04 00:34:37 +00:00
|
|
|
/>
|
2017-08-10 16:16:32 +00:00
|
|
|
{this.renderFooter()}
|
2017-08-14 14:15:37 +00:00
|
|
|
</KeyboardView>
|
2017-08-04 00:34:37 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|