2017-08-10 16:25:50 +00:00
|
|
|
import ActionButton from 'react-native-action-button';
|
2017-08-11 18:18:09 +00:00
|
|
|
import { ListView } from 'realm/react-native';
|
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-09-01 19:42:50 +00:00
|
|
|
import Icon from 'react-native-vector-icons/Ionicons';
|
2017-11-14 22:46:56 +00:00
|
|
|
import { Platform, View, StyleSheet, TextInput, SafeAreaView } from 'react-native';
|
2017-08-13 01:35:09 +00:00
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import * as actions from '../actions';
|
2017-08-18 21:30:16 +00:00
|
|
|
import * as server from '../actions/connect';
|
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 RoomItem from '../presentation/RoomItem';
|
|
|
|
import Banner from '../containers/Banner';
|
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: {
|
2017-09-21 17:08:00 +00:00
|
|
|
width: '100%',
|
|
|
|
backgroundColor: '#FFFFFF'
|
2017-08-09 16:19:17 +00:00
|
|
|
},
|
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
|
|
|
},
|
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-11-13 12:49:19 +00:00
|
|
|
},
|
|
|
|
safeAreaView: {
|
2017-11-13 16:04:13 +00:00
|
|
|
flex: 1,
|
2017-11-13 12:49:19 +00:00
|
|
|
backgroundColor: '#fff'
|
2017-08-03 18:23:43 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-08-11 18:18:09 +00:00
|
|
|
const ds = new ListView.DataSource({ rowHasChanged: (r1, r2) => r1 !== r2 });
|
2017-08-13 01:35:09 +00:00
|
|
|
@connect(state => ({
|
2017-09-01 19:42:50 +00:00
|
|
|
server: state.server.server,
|
2017-08-18 21:30:16 +00:00
|
|
|
login: state.login,
|
|
|
|
Site_Url: state.settings.Site_Url,
|
2017-09-01 19:42:50 +00:00
|
|
|
canShowList: state.login.token || state.login.user.token
|
2017-11-08 20:23:46 +00:00
|
|
|
// Message_DateFormat: state.settings.Message_DateFormat
|
2017-08-13 01:35:09 +00:00
|
|
|
}), dispatch => ({
|
2017-08-17 01:06:31 +00:00
|
|
|
login: () => dispatch(actions.login()),
|
2017-08-18 21:30:16 +00:00
|
|
|
connect: () => dispatch(server.connectRequest())
|
2017-08-13 01:35:09 +00:00
|
|
|
}))
|
2017-08-14 18:02:53 +00:00
|
|
|
|
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-09-21 17:08:00 +00:00
|
|
|
navigation: PropTypes.object.isRequired,
|
2017-08-22 01:43:29 +00:00
|
|
|
Site_Url: PropTypes.string,
|
2017-11-08 20:23:46 +00:00
|
|
|
// Message_DateFormat: PropTypes.string,
|
2017-08-18 21:30:16 +00:00
|
|
|
server: PropTypes.string
|
2017-08-10 14:59:07 +00:00
|
|
|
}
|
2017-08-09 16:19:17 +00:00
|
|
|
|
2017-11-14 22:46:56 +00:00
|
|
|
static navigationOptions = ({ navigation }) => {
|
|
|
|
if (Platform.OS !== 'ios') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { params = {} } = navigation.state;
|
|
|
|
const headerRight = (
|
|
|
|
<Icon.Button
|
|
|
|
name='ios-create-outline'
|
|
|
|
color='blue'
|
|
|
|
size={26}
|
|
|
|
backgroundColor='transparent'
|
|
|
|
onPress={params.createChannel}
|
2017-11-18 20:17:24 +00:00
|
|
|
/>);
|
2017-11-14 22:46:56 +00:00
|
|
|
|
|
|
|
return { headerRight };
|
|
|
|
};
|
|
|
|
|
2017-08-03 18:23:43 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2017-09-21 17:08:00 +00:00
|
|
|
|
2017-08-10 16:16:32 +00:00
|
|
|
this.state = {
|
2017-11-20 22:18:00 +00:00
|
|
|
dataSource: ds.cloneWithRows([]),
|
2017-11-18 20:17:24 +00:00
|
|
|
searchText: ''
|
2017-08-10 16:16:32 +00:00
|
|
|
};
|
2017-09-21 17:08:00 +00:00
|
|
|
this.data = realm.objects('subscriptions').filtered('_server.id = $0', this.props.server).sorted('_updatedAt', true);
|
2017-08-21 00:11:46 +00:00
|
|
|
}
|
2017-09-21 17:08:00 +00:00
|
|
|
|
2017-11-20 22:18:00 +00:00
|
|
|
componentDidMount() {
|
2017-09-21 17:08:00 +00:00
|
|
|
this.data.addListener(this.updateState);
|
|
|
|
|
2017-11-14 22:46:56 +00:00
|
|
|
this.props.navigation.setParams({
|
|
|
|
createChannel: () => this._createChannel()
|
|
|
|
});
|
|
|
|
|
2017-11-20 22:18:00 +00:00
|
|
|
this.updateState();
|
2017-08-13 01:35:09 +00:00
|
|
|
}
|
2017-09-21 17:08:00 +00:00
|
|
|
|
2017-11-07 20:25:04 +00:00
|
|
|
componentWillReceiveProps(props) {
|
|
|
|
if (this.props.server !== props.server) {
|
|
|
|
this.data.removeListener(this.updateState);
|
|
|
|
this.data = realm.objects('subscriptions').filtered('_server.id = $0', props.server).sorted('_updatedAt', true);
|
|
|
|
this.data.addListener(this.updateState);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-09 16:19:17 +00:00
|
|
|
componentWillUnmount() {
|
2017-09-21 17:08:00 +00:00
|
|
|
this.data.removeAllListeners();
|
2017-08-12 20:52:55 +00:00
|
|
|
}
|
|
|
|
|
2017-08-10 16:16:32 +00:00
|
|
|
onSearchChangeText = (text) => {
|
|
|
|
const searchText = text.trim();
|
|
|
|
this.setState({
|
2017-11-18 20:17:24 +00:00
|
|
|
searchText: text
|
2017-08-10 16:16:32 +00:00
|
|
|
});
|
2017-08-15 19:28:46 +00:00
|
|
|
if (searchText === '') {
|
|
|
|
return this.setState({
|
2017-08-18 21:30:16 +00:00
|
|
|
dataSource: ds.cloneWithRows(this.data)
|
2017-08-15 19:28:46 +00:00
|
|
|
});
|
|
|
|
}
|
2017-08-09 18:01:54 +00:00
|
|
|
|
2017-08-18 21:30:16 +00:00
|
|
|
const data = this.data.filtered('name CONTAINS[c] $0', searchText).slice();
|
2017-08-09 18:01:54 +00:00
|
|
|
|
2017-08-15 19:28:46 +00:00
|
|
|
const usernames = [];
|
|
|
|
const dataSource = data.map((sub) => {
|
|
|
|
if (sub.t === 'd') {
|
|
|
|
usernames.push(sub.name);
|
|
|
|
}
|
|
|
|
return sub;
|
|
|
|
});
|
2017-08-10 16:16:32 +00:00
|
|
|
|
2017-08-15 19:28:46 +00:00
|
|
|
if (dataSource.length < 7) {
|
|
|
|
if (this.oldPromise) {
|
|
|
|
this.oldPromise();
|
|
|
|
}
|
|
|
|
Promise.race([
|
|
|
|
RocketChat.spotlight(searchText, usernames),
|
|
|
|
new Promise((resolve, reject) => this.oldPromise = reject)
|
|
|
|
])
|
|
|
|
.then((results) => {
|
|
|
|
results.users.forEach((user) => {
|
|
|
|
dataSource.push({
|
|
|
|
...user,
|
|
|
|
name: user.username,
|
|
|
|
t: 'd',
|
|
|
|
search: true
|
2017-08-10 16:16:32 +00:00
|
|
|
});
|
2017-08-15 19:28:46 +00:00
|
|
|
});
|
2017-08-10 16:16:32 +00:00
|
|
|
|
2017-08-15 19:28:46 +00:00
|
|
|
results.rooms.forEach((room) => {
|
|
|
|
dataSource.push({
|
|
|
|
...room,
|
|
|
|
search: true
|
2017-08-10 16:16:32 +00:00
|
|
|
});
|
2017-08-15 19:28:46 +00:00
|
|
|
});
|
2017-08-10 16:16:32 +00:00
|
|
|
|
2017-08-15 19:28:46 +00:00
|
|
|
this.setState({
|
|
|
|
dataSource: ds.cloneWithRows(dataSource)
|
2017-08-10 16:16:32 +00:00
|
|
|
});
|
2017-08-15 19:28:46 +00:00
|
|
|
}, () => console.log('spotlight stopped'))
|
|
|
|
.then(() => delete this.oldPromise);
|
2017-08-10 16:16:32 +00:00
|
|
|
}
|
2017-08-15 19:28:46 +00:00
|
|
|
this.setState({
|
|
|
|
dataSource: ds.cloneWithRows(dataSource)
|
|
|
|
});
|
2017-08-10 16:16:32 +00:00
|
|
|
}
|
2017-08-11 17:05:30 +00:00
|
|
|
|
2017-08-17 06:28:41 +00:00
|
|
|
updateState = () => {
|
2017-08-10 16:16:32 +00:00
|
|
|
this.setState({
|
2017-08-21 00:11:46 +00:00
|
|
|
dataSource: ds.cloneWithRows(this.data)
|
2017-08-10 16:16:32 +00:00
|
|
|
});
|
2017-08-17 06:28:41 +00:00
|
|
|
};
|
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) => {
|
2017-09-21 17:08:00 +00:00
|
|
|
this.props.navigation.navigate('Room', { room });
|
2017-08-12 20:52:55 +00:00
|
|
|
};
|
2017-08-10 16:16:32 +00:00
|
|
|
|
|
|
|
const clearSearch = () => {
|
|
|
|
this.setState({
|
2017-11-18 20:17:24 +00:00
|
|
|
searchText: ''
|
2017-08-10 16:16:32 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// if user is using the search we need first to join/create room
|
|
|
|
if (item.search) {
|
|
|
|
if (item.t === 'd') {
|
|
|
|
RocketChat.createDirectMessage(item.username)
|
2017-08-14 18:02:53 +00:00
|
|
|
.then(room => new Promise((resolve) => {
|
|
|
|
const data = realm.objects('subscriptions').filtered('_server.id = $0 AND rid = $1', this.props.server, room.rid);
|
|
|
|
|
|
|
|
if (data.length) {
|
|
|
|
return resolve(data[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
data.addListener(() => {
|
|
|
|
if (data.length) {
|
|
|
|
resolve(data[0]);
|
|
|
|
data.removeAllListeners();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}))
|
|
|
|
.then(sub => navigateToRoom({ sid: sub._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-11-19 02:29:46 +00:00
|
|
|
navigateToRoom({ sid: id, ...item });
|
2017-08-10 16:16:32 +00:00
|
|
|
clearSearch();
|
2017-08-05 18:16:32 +00:00
|
|
|
}
|
2017-09-21 17:08:00 +00:00
|
|
|
|
|
|
|
_createChannel() {
|
2017-09-25 13:15:28 +00:00
|
|
|
this.props.navigation.navigate('SelectUsers');
|
2017-09-01 19:42:50 +00:00
|
|
|
}
|
2017-09-21 17:08:00 +00:00
|
|
|
|
2017-08-10 16:16:32 +00:00
|
|
|
renderSearchBar = () => (
|
2017-08-10 20:26:11 +00:00
|
|
|
<View style={styles.searchBoxView}>
|
|
|
|
<TextInput
|
2017-08-18 21:30:16 +00:00
|
|
|
underlineColorAndroid='transparent'
|
2017-08-10 20:26:11 +00:00
|
|
|
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-18 21:30:16 +00:00
|
|
|
renderItem = item => (
|
|
|
|
<RoomItem
|
2017-09-21 17:08:00 +00:00
|
|
|
unread={item.unread}
|
2017-08-21 16:16:23 +00:00
|
|
|
name={item.name}
|
2017-11-08 20:23:46 +00:00
|
|
|
_updatedAt={item._updatedAt}
|
2017-09-21 17:08:00 +00:00
|
|
|
key={item._id}
|
2017-08-21 16:16:23 +00:00
|
|
|
type={item.t}
|
2017-08-21 00:11:46 +00:00
|
|
|
baseUrl={this.props.Site_Url}
|
2017-11-13 12:49:19 +00:00
|
|
|
dateFormat='MM-DD-YYYY HH:mm:ss'
|
2017-08-18 21:30:16 +00:00
|
|
|
onPress={() => this._onPressItem(item._id, item)}
|
|
|
|
/>
|
|
|
|
)
|
2017-09-21 17:08:00 +00:00
|
|
|
|
2017-08-11 18:18:09 +00:00
|
|
|
renderList = () => (
|
|
|
|
<ListView
|
|
|
|
dataSource={this.state.dataSource}
|
|
|
|
style={styles.list}
|
2017-08-18 21:30:16 +00:00
|
|
|
renderRow={this.renderItem}
|
2017-08-13 03:19:14 +00:00
|
|
|
renderHeader={this.renderSearchBar}
|
2017-11-14 22:46:56 +00:00
|
|
|
contentOffset={{ x: 0, y: 38 }}
|
2017-08-12 20:52:55 +00:00
|
|
|
enableEmptySections
|
2017-08-14 18:02:53 +00:00
|
|
|
keyboardShouldPersistTaps='always'
|
2017-08-11 18:18:09 +00:00
|
|
|
/>
|
|
|
|
)
|
2017-09-21 17:08:00 +00:00
|
|
|
|
2017-08-18 21:30:16 +00:00
|
|
|
renderCreateButtons = () => (
|
2017-09-01 19:42:50 +00:00
|
|
|
<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>
|
2017-09-21 17:08:00 +00:00
|
|
|
</ActionButton>
|
|
|
|
);
|
|
|
|
|
|
|
|
render = () => (
|
2017-08-21 00:11:46 +00:00
|
|
|
<View style={styles.container}>
|
|
|
|
<Banner />
|
2017-11-13 12:49:19 +00:00
|
|
|
<SafeAreaView style={styles.safeAreaView}>
|
|
|
|
{this.renderList()}
|
2017-11-14 22:46:56 +00:00
|
|
|
{Platform.OS === 'android' && this.renderCreateButtons()}
|
2017-11-13 12:49:19 +00:00
|
|
|
</SafeAreaView>
|
2017-08-21 00:11:46 +00:00
|
|
|
</View>)
|
2017-08-03 18:23:43 +00:00
|
|
|
}
|