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-08-11 18:18:09 +00:00
|
|
|
import { Text, View, StyleSheet, Button } from 'react-native';
|
|
|
|
import { ListView } from 'realm/react-native';
|
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-08-11 18:18:09 +00:00
|
|
|
import debounce from '../utils/throttle';
|
2017-08-09 01:40:55 +00:00
|
|
|
import Message from '../components/Message';
|
2017-08-09 13:12:00 +00:00
|
|
|
import MessageBox from '../components/MessageBox';
|
2017-08-11 18:18:09 +00:00
|
|
|
// import KeyboardView from '../components/KeyboardView';
|
|
|
|
const ds = new ListView.DataSource({ rowHasChanged: (r1, r2) => r1 !== r2 });
|
2017-08-04 00:34:37 +00:00
|
|
|
const styles = StyleSheet.create({
|
|
|
|
container: {
|
|
|
|
flex: 1
|
|
|
|
},
|
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-08-05 18:16:32 +00:00
|
|
|
export default class RoomView extends React.Component {
|
|
|
|
static propTypes = {
|
2017-08-12 20:52:55 +00:00
|
|
|
navigator: PropTypes.object.isRequired,
|
|
|
|
rid: PropTypes.string,
|
|
|
|
sid: PropTypes.string,
|
|
|
|
name: PropTypes.string
|
2017-08-05 18:16:32 +00:00
|
|
|
}
|
|
|
|
|
2017-08-04 00:34:37 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2017-08-12 20:52:55 +00:00
|
|
|
this.rid = props.rid || realm.objectForPrimaryKey('subscriptions', props.sid).rid;
|
2017-08-04 00:34:37 +00:00
|
|
|
// this.rid = 'GENERAL';
|
2017-08-12 20:52:55 +00:00
|
|
|
|
2017-08-11 18:18:09 +00:00
|
|
|
this.data = realm.objects('messages').filtered('_server.id = $0 AND rid = $1', RocketChat.currentServer, this.rid).sorted('ts', true);
|
2017-08-07 18:42:02 +00:00
|
|
|
this.state = {
|
2017-08-11 18:18:09 +00:00
|
|
|
dataSource: ds.cloneWithRows(this.data.slice(0, 10)),
|
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-11 18:18:09 +00:00
|
|
|
// console.log(this.messages);
|
2017-08-07 18:42:02 +00:00
|
|
|
this.url = realm.objectForPrimaryKey('settings', 'Site_Url').value;
|
2017-08-12 20:52:55 +00:00
|
|
|
|
|
|
|
this.props.navigator.setTitle({
|
|
|
|
title: this.props.name || realm.objectForPrimaryKey('subscriptions', this.props.sid).name
|
|
|
|
});
|
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-08-10 16:49:15 +00:00
|
|
|
const late = setTimeout(() => this.setState({
|
|
|
|
loaded: false
|
|
|
|
}), 1000);
|
2017-08-10 23:21:46 +00:00
|
|
|
RocketChat.loadMessagesForRoom(this.rid, null, () => {
|
2017-08-10 16:49:15 +00:00
|
|
|
clearTimeout(late);
|
2017-08-09 20:08:50 +00:00
|
|
|
this.setState({
|
|
|
|
loaded: true
|
|
|
|
});
|
2017-08-11 18:18:09 +00:00
|
|
|
this.data.addListener(this.updateState);
|
2017-08-09 20:08:50 +00:00
|
|
|
});
|
2017-08-11 18:18:09 +00:00
|
|
|
this.updateState();
|
|
|
|
}
|
2017-08-12 20:52:55 +00:00
|
|
|
|
2017-08-11 18:18:09 +00:00
|
|
|
componentDidMount() {
|
|
|
|
return RocketChat.readMessages(this.rid);
|
2017-08-07 00:34:35 +00:00
|
|
|
}
|
2017-08-12 20:52:55 +00:00
|
|
|
|
2017-08-07 00:34:35 +00:00
|
|
|
componentWillUnmount() {
|
2017-08-11 17:05:30 +00:00
|
|
|
this.data.removeListener(this.updateState);
|
2017-08-04 00:34:37 +00:00
|
|
|
}
|
|
|
|
|
2017-08-10 23:21:46 +00:00
|
|
|
onEndReached = () => {
|
|
|
|
if (this.state.dataSource.length && this.state.loaded && this.state.loadingMore !== true && this.state.end !== true) {
|
|
|
|
this.setState({
|
2017-08-11 18:18:09 +00:00
|
|
|
// ...this.state,
|
2017-08-10 23:21:46 +00:00
|
|
|
loadingMore: true
|
|
|
|
});
|
|
|
|
RocketChat.loadMessagesForRoom(this.rid, this.state.dataSource[this.state.dataSource.length - 1].ts, ({ end }) => {
|
|
|
|
this.setState({
|
2017-08-11 18:18:09 +00:00
|
|
|
// ...this.state,
|
2017-08-10 23:21:46 +00:00
|
|
|
loadingMore: false,
|
|
|
|
end
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-11 18:18:09 +00:00
|
|
|
updateState = debounce(() => {
|
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-08-11 18:18:09 +00:00
|
|
|
// RocketChat.readMessages(this.rid);
|
|
|
|
// this.setState({
|
|
|
|
// messages: this.messages
|
|
|
|
// });
|
|
|
|
}, 100);
|
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-08-10 16:16:32 +00:00
|
|
|
joinRoom = () => {
|
2017-08-12 20:52:55 +00:00
|
|
|
RocketChat.joinRoom(this.props.rid)
|
2017-08-10 16:16:32 +00:00
|
|
|
.then(() => {
|
|
|
|
this.setState({
|
|
|
|
joined: true
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
renderBanner = () => {
|
|
|
|
if (this.state.loaded === false) {
|
|
|
|
return (
|
|
|
|
<View style={styles.bannerContainer}>
|
|
|
|
<Text style={styles.bannerText}>Loading new messages...</Text>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-08-09 01:40:55 +00:00
|
|
|
renderItem = ({ item }) => (
|
|
|
|
<Message
|
|
|
|
id={item._id}
|
|
|
|
item={item}
|
|
|
|
baseUrl={this.url}
|
|
|
|
/>
|
|
|
|
);
|
2017-08-07 00:34:35 +00:00
|
|
|
|
2017-08-10 16:16:32 +00:00
|
|
|
renderSeparator = () => (
|
|
|
|
<View style={styles.separator} />
|
|
|
|
);
|
|
|
|
|
|
|
|
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-08-10 16:16:32 +00:00
|
|
|
return (
|
|
|
|
<MessageBox
|
2017-08-11 18:18:09 +00:00
|
|
|
ref={box => this.box = box}
|
2017-08-10 16:16:32 +00:00
|
|
|
onSubmit={this.sendMessage}
|
2017-08-10 20:09:54 +00:00
|
|
|
rid={this.rid}
|
2017-08-10 16:16:32 +00:00
|
|
|
/>
|
|
|
|
);
|
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-08-04 00:34:37 +00:00
|
|
|
render() {
|
2017-08-11 18:18:09 +00:00
|
|
|
// data={this.state.dataSource}
|
|
|
|
// extraData={this.state}
|
|
|
|
// renderItem={this.renderItem}
|
|
|
|
// keyExtractor={item => item._id}
|
|
|
|
//
|
2017-08-04 00:34:37 +00:00
|
|
|
return (
|
2017-08-11 18:18:09 +00:00
|
|
|
<View style={styles.container}>
|
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-10 23:21:46 +00:00
|
|
|
onEndReachedThreshold={0.1}
|
|
|
|
ListFooterComponent={this.renderHeader()}
|
2017-08-11 18:18:09 +00:00
|
|
|
onEndReached={this.onEndReached}
|
|
|
|
dataSource={this.state.dataSource}
|
|
|
|
renderRow={item => this.renderItem({ item })}
|
2017-08-04 00:34:37 +00:00
|
|
|
/>
|
2017-08-10 16:16:32 +00:00
|
|
|
{this.renderFooter()}
|
2017-08-11 18:18:09 +00:00
|
|
|
</View>
|
2017-08-04 00:34:37 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|