2017-08-10 16:25:50 +00:00
|
|
|
import ActionButton from 'react-native-action-button';
|
2017-08-12 20:52:55 +00:00
|
|
|
import { Navigation } from 'react-native-navigation';
|
2017-08-11 18:18:09 +00:00
|
|
|
import { ListView } from 'realm/react-native';
|
2017-08-10 16:25:50 +00:00
|
|
|
import Icon from 'react-native-vector-icons/Ionicons';
|
2017-08-03 18:23:43 +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 { Button, Text, View, StyleSheet, TouchableOpacity, Platform, TextInput } from 'react-native';
|
2017-08-09 16:19:17 +00:00
|
|
|
import Meteor from 'react-native-meteor';
|
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-09 01:40:55 +00:00
|
|
|
import RoomItem from '../components/RoomItem';
|
2017-08-11 18:18:09 +00:00
|
|
|
import debounce from '../utils/debounce';
|
2017-08-03 18:23:43 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
container: {
|
2017-08-09 16:19:17 +00:00
|
|
|
flex: 1,
|
2017-08-09 19:11:00 +00:00
|
|
|
alignItems: 'stretch',
|
2017-08-09 16:19:17 +00:00
|
|
|
justifyContent: 'center'
|
2017-08-03 18:23:43 +00:00
|
|
|
},
|
|
|
|
separator: {
|
|
|
|
height: 1,
|
2017-08-10 16:25:50 +00:00
|
|
|
backgroundColor: '#E7E7E7'
|
2017-08-09 16:19:17 +00:00
|
|
|
},
|
|
|
|
list: {
|
|
|
|
width: '100%'
|
|
|
|
},
|
2017-08-09 20:08:50 +00:00
|
|
|
emptyView: {
|
|
|
|
flexGrow: 1,
|
|
|
|
alignItems: 'stretch',
|
|
|
|
justifyContent: 'center'
|
|
|
|
},
|
2017-08-09 16:19:17 +00:00
|
|
|
emptyText: {
|
|
|
|
textAlign: 'center',
|
|
|
|
fontSize: 18,
|
|
|
|
color: '#ccc'
|
2017-08-09 19:11:00 +00:00
|
|
|
},
|
|
|
|
bannerContainer: {
|
|
|
|
backgroundColor: '#ddd'
|
|
|
|
},
|
|
|
|
bannerText: {
|
2017-08-09 20:08:50 +00:00
|
|
|
textAlign: 'center',
|
|
|
|
margin: 5
|
2017-08-10 16:25:50 +00:00
|
|
|
},
|
|
|
|
actionButtonIcon: {
|
|
|
|
fontSize: 20,
|
|
|
|
height: 22,
|
|
|
|
color: 'white'
|
2017-08-10 20:26:11 +00:00
|
|
|
},
|
|
|
|
searchBoxView: {
|
2017-08-10 23:26:59 +00:00
|
|
|
backgroundColor: '#eee'
|
2017-08-10 20:26:11 +00:00
|
|
|
},
|
|
|
|
searchBox: {
|
|
|
|
backgroundColor: '#fff',
|
|
|
|
margin: 5,
|
|
|
|
borderRadius: 5,
|
|
|
|
padding: 5,
|
|
|
|
paddingLeft: 10,
|
|
|
|
color: '#aaa'
|
2017-08-03 18:23:43 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-08-11 17:05:30 +00:00
|
|
|
let setInitialData;
|
2017-08-09 16:19:17 +00:00
|
|
|
|
|
|
|
Meteor.getData().on('loggingIn', () => {
|
|
|
|
setTimeout(() => {
|
|
|
|
if (Meteor._isLoggingIn === false && Meteor.userId() == null) {
|
|
|
|
console.log('loggingIn', Meteor.userId());
|
2017-08-12 20:52:55 +00:00
|
|
|
Navigation.showModal({
|
|
|
|
screen: 'Login',
|
|
|
|
animationType: 'slide-up'
|
|
|
|
});
|
2017-08-09 16:19:17 +00:00
|
|
|
}
|
|
|
|
}, 100);
|
|
|
|
});
|
|
|
|
|
|
|
|
Meteor.Accounts.onLogin(() => {
|
|
|
|
console.log('onLogin');
|
|
|
|
});
|
|
|
|
|
2017-08-11 18:18:09 +00:00
|
|
|
const ds = new ListView.DataSource({ rowHasChanged: (r1, r2) => r1 !== r2 });
|
|
|
|
class RoomsListItem extends React.PureComponent {
|
|
|
|
static propTypes = {
|
|
|
|
item: PropTypes.object.isRequired,
|
|
|
|
onPress: PropTypes.func.isRequired
|
|
|
|
}
|
|
|
|
_onPress = (...args) => {
|
|
|
|
this.props.onPress(...args);
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { item } = this.props;
|
|
|
|
return (
|
|
|
|
<TouchableOpacity key={item._id} onPress={() => this.props.onPress(item._id, item)}>
|
|
|
|
<RoomItem
|
|
|
|
id={item._id}
|
|
|
|
item={item}
|
|
|
|
/>
|
|
|
|
</TouchableOpacity>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2017-08-09 01:40:55 +00:00
|
|
|
export default class RoomsListView extends React.Component {
|
2017-08-05 18:16:32 +00:00
|
|
|
static propTypes = {
|
2017-08-12 20:52:55 +00:00
|
|
|
navigator: PropTypes.object.isRequired
|
2017-08-03 18:23:43 +00:00
|
|
|
}
|
|
|
|
|
2017-08-11 17:05:30 +00:00
|
|
|
static navigationOptions = (props) => {
|
2017-08-10 14:59:07 +00:00
|
|
|
const server = RocketChat.currentServer ? RocketChat.currentServer.replace(/^https?:\/\//, '') : '';
|
2017-08-10 15:11:37 +00:00
|
|
|
const textAlign = Platform.OS === 'ios' ? 'center' : 'left';
|
|
|
|
const marginLeft = Platform.OS === 'ios' ? 0 : 20;
|
2017-08-11 17:05:30 +00:00
|
|
|
const position = Platform.OS === 'ios' ? 'headerLeft' : 'headerRight';
|
|
|
|
|
2017-08-10 14:59:07 +00:00
|
|
|
return {
|
2017-08-10 15:11:37 +00:00
|
|
|
headerTitle: <View style={{ height: 10, width: 200, top: -10, marginLeft }}>
|
|
|
|
<Text style={{ textAlign, fontSize: 16, fontWeight: '600' }}>Channels</Text>
|
|
|
|
<Text style={{ textAlign, fontSize: 10 }}>{server}</Text>
|
|
|
|
</View>,
|
2017-08-11 17:05:30 +00:00
|
|
|
title: 'Channels',
|
2017-08-11 19:57:09 +00:00
|
|
|
[position]: <Button title='Servers' onPress={() => props.navigation.navigate('ListServerModal')} />
|
2017-08-10 14:59:07 +00:00
|
|
|
};
|
|
|
|
}
|
2017-08-09 16:19:17 +00:00
|
|
|
|
2017-08-03 18:23:43 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2017-08-11 18:18:09 +00:00
|
|
|
this.data = realm.objects('subscriptions').filtered('_server.id = $0', RocketChat.currentServer).sorted('_updatedAt', true);
|
2017-08-10 16:16:32 +00:00
|
|
|
this.state = {
|
2017-08-11 18:18:09 +00:00
|
|
|
dataSource: ds.cloneWithRows(this.data.sorted('_updatedAt', true).slice(0, 10)),
|
2017-08-10 16:16:32 +00:00
|
|
|
searching: false,
|
|
|
|
searchDataSource: [],
|
|
|
|
searchText: ''
|
|
|
|
};
|
2017-08-11 19:57:09 +00:00
|
|
|
this.data.addListener(this.updateState);
|
2017-08-12 20:52:55 +00:00
|
|
|
this.props.navigator.setOnNavigatorEvent(this.onNavigatorEvent.bind(this));
|
2017-08-09 16:19:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillMount() {
|
2017-08-11 17:05:30 +00:00
|
|
|
setInitialData = this.setInitialData;
|
2017-08-09 16:19:17 +00:00
|
|
|
|
2017-08-12 20:52:55 +00:00
|
|
|
if (RocketChat.currentServer) {
|
|
|
|
this.props.navigator.setSubTitle({
|
|
|
|
subtitle: RocketChat.currentServer
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
this.props.navigator.setButtons({
|
|
|
|
leftButtons: [{
|
|
|
|
id: 'servers',
|
|
|
|
title: 'Servers'
|
|
|
|
}],
|
|
|
|
// rightButtons: [], // see "Adding buttons to the navigator" below for format (optional)
|
|
|
|
animated: true
|
|
|
|
});
|
2017-08-09 16:19:17 +00:00
|
|
|
|
2017-08-11 19:57:09 +00:00
|
|
|
// this.setInitialData();
|
2017-08-09 18:01:54 +00:00
|
|
|
if (RocketChat.currentServer) {
|
2017-08-09 20:18:00 +00:00
|
|
|
RocketChat.connect();
|
2017-08-09 16:19:17 +00:00
|
|
|
} else {
|
2017-08-12 20:52:55 +00:00
|
|
|
Navigation.showModal({
|
|
|
|
screen: 'ListServer',
|
|
|
|
passProps: {},
|
|
|
|
navigatorStyle: {},
|
|
|
|
navigatorButtons: {},
|
|
|
|
animationType: 'none'
|
2017-08-11 17:05:30 +00:00
|
|
|
});
|
2017-08-09 16:19:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2017-08-11 17:05:30 +00:00
|
|
|
this.data.removeListener(this.updateState);
|
2017-08-09 16:19:17 +00:00
|
|
|
}
|
|
|
|
|
2017-08-12 20:52:55 +00:00
|
|
|
onNavigatorEvent = (event) => {
|
|
|
|
if (event.type === 'NavBarButtonPress') {
|
|
|
|
if (event.id === 'servers') {
|
|
|
|
Navigation.showModal({
|
|
|
|
screen: 'ListServer',
|
|
|
|
passProps: {},
|
|
|
|
navigatorStyle: {},
|
|
|
|
navigatorButtons: {},
|
|
|
|
animationType: 'slide-up'
|
|
|
|
// animationType: 'none'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-10 16:16:32 +00:00
|
|
|
onSearchChangeText = (text) => {
|
|
|
|
const searchText = text.trim();
|
|
|
|
this.setState({
|
|
|
|
searchText: text,
|
|
|
|
searching: searchText !== ''
|
|
|
|
});
|
2017-08-09 18:01:54 +00:00
|
|
|
|
2017-08-10 16:16:32 +00:00
|
|
|
if (searchText !== '') {
|
|
|
|
const dataSource = [];
|
|
|
|
const usernames = [];
|
|
|
|
realm.objects('subscriptions').filtered('_server.id = $0 AND name CONTAINS[c] $1', RocketChat.currentServer, searchText).forEach((sub) => {
|
|
|
|
dataSource.push(sub);
|
2017-08-09 18:01:54 +00:00
|
|
|
|
2017-08-10 16:16:32 +00:00
|
|
|
if (sub.t === 'd') {
|
|
|
|
usernames.push(sub.name);
|
2017-08-09 18:01:54 +00:00
|
|
|
}
|
2017-08-10 16:16:32 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (dataSource.length < 5) {
|
|
|
|
RocketChat.spotlight(searchText, usernames)
|
|
|
|
.then((results) => {
|
|
|
|
results.users.forEach((user) => {
|
|
|
|
dataSource.push({
|
|
|
|
...user,
|
|
|
|
name: user.username,
|
|
|
|
t: 'd',
|
|
|
|
search: true
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
results.rooms.forEach((room) => {
|
|
|
|
dataSource.push({
|
|
|
|
...room,
|
|
|
|
search: true
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
searchDataSource: dataSource
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-08-11 17:05:30 +00:00
|
|
|
setInitialData = () => {
|
|
|
|
if (this.data) {
|
|
|
|
this.data.removeListener(this.updateState);
|
|
|
|
}
|
2017-08-11 19:57:09 +00:00
|
|
|
this.data = realm.objects('subscriptions').filtered('_server.id = $0', RocketChat.currentServer).sorted('_updatedAt', true);
|
2017-08-11 17:05:30 +00:00
|
|
|
this.data.addListener(this.updateState);
|
|
|
|
|
2017-08-11 18:18:09 +00:00
|
|
|
this.updateState();
|
2017-08-11 17:05:30 +00:00
|
|
|
}
|
|
|
|
|
2017-08-11 18:18:09 +00:00
|
|
|
getSubscriptions = () => this.data.sorted('_updatedAt', true)
|
2017-08-09 18:01:54 +00:00
|
|
|
|
2017-08-11 18:18:09 +00:00
|
|
|
updateState = debounce(() => {
|
2017-08-10 16:16:32 +00:00
|
|
|
this.setState({
|
2017-08-11 19:57:09 +00:00
|
|
|
dataSource: ds.cloneWithRows(this.data.filtered('_server.id = $0', RocketChat.currentServer))
|
2017-08-10 16:16:32 +00:00
|
|
|
});
|
2017-08-11 18:18:09 +00:00
|
|
|
}, 500);
|
2017-08-03 18:23:43 +00:00
|
|
|
|
2017-08-10 16:49:15 +00:00
|
|
|
_onPressItem = (id, item = {}) => {
|
2017-08-12 20:52:55 +00:00
|
|
|
const navigateToRoom = (room) => {
|
|
|
|
this.props.navigator.push({
|
|
|
|
screen: 'Room',
|
|
|
|
passProps: room
|
|
|
|
});
|
|
|
|
};
|
2017-08-10 16:16:32 +00:00
|
|
|
|
|
|
|
const clearSearch = () => {
|
|
|
|
this.setState({
|
|
|
|
searchText: '',
|
|
|
|
searching: false,
|
|
|
|
searchDataSource: []
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// if user is using the search we need first to join/create room
|
|
|
|
if (item.search) {
|
|
|
|
if (item.t === 'd') {
|
|
|
|
RocketChat.createDirectMessage(item.username)
|
|
|
|
.then(room => realm.objects('subscriptions').filtered('_server.id = $0 AND rid = $1', RocketChat.currentServer, room.rid))
|
2017-08-12 20:52:55 +00:00
|
|
|
.then(subs => navigateToRoom({ sid: subs[0]._id }))
|
2017-08-10 16:16:32 +00:00
|
|
|
.then(() => clearSearch());
|
|
|
|
} else {
|
|
|
|
clearSearch();
|
2017-08-12 20:52:55 +00:00
|
|
|
navigateToRoom({ rid: item._id, name: item.name });
|
2017-08-10 16:16:32 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-12 20:52:55 +00:00
|
|
|
navigateToRoom({ sid: id });
|
2017-08-10 16:16:32 +00:00
|
|
|
clearSearch();
|
2017-08-05 18:16:32 +00:00
|
|
|
}
|
2017-08-12 20:52:55 +00:00
|
|
|
|
2017-08-10 16:25:50 +00:00
|
|
|
_createChannel = () => {
|
2017-08-12 20:52:55 +00:00
|
|
|
this.props.navigator.showModal({
|
|
|
|
screen: 'CreateChannel'
|
|
|
|
});
|
2017-08-10 16:25:50 +00:00
|
|
|
}
|
2017-08-12 20:52:55 +00:00
|
|
|
|
2017-08-09 19:11:00 +00:00
|
|
|
renderBanner = () => {
|
2017-08-09 20:08:50 +00:00
|
|
|
const status = Meteor.getData() && Meteor.getData().ddp && Meteor.getData().ddp.status;
|
|
|
|
|
|
|
|
if (status === 'disconnected') {
|
2017-08-09 19:11:00 +00:00
|
|
|
return (
|
2017-08-09 20:08:50 +00:00
|
|
|
<View style={[styles.bannerContainer, { backgroundColor: '#0d0' }]}>
|
|
|
|
<Text style={[styles.bannerText, { color: '#fff' }]}>Connecting...</Text>
|
2017-08-09 19:11:00 +00:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-08-09 20:08:50 +00:00
|
|
|
if (status === 'connected' && Meteor._isLoggingIn) {
|
2017-08-09 19:11:00 +00:00
|
|
|
return (
|
2017-08-09 20:08:50 +00:00
|
|
|
<View style={[styles.bannerContainer, { backgroundColor: 'orange' }]}>
|
|
|
|
<Text style={[styles.bannerText, { color: '#a00' }]}>Authenticating...</Text>
|
2017-08-09 19:11:00 +00:00
|
|
|
</View>
|
2017-08-09 20:18:00 +00:00
|
|
|
);
|
2017-08-09 19:11:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-05 18:16:32 +00:00
|
|
|
renderItem = ({ item }) => (
|
2017-08-11 18:18:09 +00:00
|
|
|
<RoomsListItem item={item} onPress={() => this._onPressItem(item._id, item)} />
|
2017-08-05 18:16:32 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
renderSeparator = () => (
|
|
|
|
<View style={styles.separator} />
|
|
|
|
);
|
2017-08-03 18:23:43 +00:00
|
|
|
|
2017-08-10 16:16:32 +00:00
|
|
|
renderSearchBar = () => (
|
2017-08-10 20:26:11 +00:00
|
|
|
<View style={styles.searchBoxView}>
|
|
|
|
<TextInput
|
|
|
|
style={styles.searchBox}
|
|
|
|
value={this.state.searchText}
|
|
|
|
onChangeText={this.onSearchChangeText}
|
|
|
|
returnKeyType='search'
|
|
|
|
placeholder='Search'
|
2017-08-10 20:31:14 +00:00
|
|
|
clearButtonMode='while-editing'
|
|
|
|
blurOnSubmit
|
2017-08-10 20:26:11 +00:00
|
|
|
/>
|
|
|
|
</View>
|
2017-08-10 16:16:32 +00:00
|
|
|
);
|
|
|
|
|
2017-08-11 18:18:09 +00:00
|
|
|
// if (!this.state.searching && !this.state.dataSource.length) {
|
|
|
|
// return (
|
|
|
|
// <View style={styles.emptyView}>
|
|
|
|
// <Text style={styles.emptyText}>No rooms</Text>
|
|
|
|
// </View>
|
|
|
|
// );
|
|
|
|
// }
|
|
|
|
renderList = () => (
|
|
|
|
// data={this.state.searching ? this.state.searchDataSource : this.state.dataSource}
|
|
|
|
// keyExtractor={item => item._id}
|
|
|
|
// ItemSeparatorComponent={this.renderSeparator}
|
|
|
|
// renderItem={this.renderItem}
|
|
|
|
<ListView
|
|
|
|
dataSource={this.state.dataSource}
|
|
|
|
style={styles.list}
|
|
|
|
renderRow={item => this.renderItem({ item })}
|
2017-08-12 20:52:55 +00:00
|
|
|
enableEmptySections
|
2017-08-11 18:18:09 +00:00
|
|
|
/>
|
|
|
|
)
|
2017-08-09 16:19:17 +00:00
|
|
|
|
2017-08-10 16:25:50 +00:00
|
|
|
renderCreateButtons() {
|
|
|
|
return (
|
|
|
|
<ActionButton buttonColor='rgba(231,76,60,1)'>
|
|
|
|
<ActionButton.Item buttonColor='#9b59b6' title='Create Channel' onPress={() => { this._createChannel(); }} >
|
|
|
|
<Icon name='md-chatbubbles' style={styles.actionButtonIcon} />
|
|
|
|
</ActionButton.Item>
|
|
|
|
</ActionButton>);
|
|
|
|
}
|
2017-08-09 16:19:17 +00:00
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<View style={styles.container}>
|
2017-08-09 19:11:00 +00:00
|
|
|
{this.renderBanner()}
|
2017-08-10 16:16:32 +00:00
|
|
|
{this.renderSearchBar()}
|
2017-08-09 16:19:17 +00:00
|
|
|
{this.renderList()}
|
2017-08-10 16:25:50 +00:00
|
|
|
{this.renderCreateButtons()}
|
2017-08-03 18:23:43 +00:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|