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';
|
2018-08-31 18:13:30 +00:00
|
|
|
import { Platform, View, FlatList, BackHandler, ActivityIndicator, SafeAreaView, Text, Image, Dimensions, ScrollView, Keyboard } from 'react-native';
|
2017-08-13 01:35:09 +00:00
|
|
|
import { connect } from 'react-redux';
|
2018-08-31 16:46:33 +00:00
|
|
|
import { isEqual } from 'lodash';
|
2018-05-18 17:55:08 +00:00
|
|
|
|
2018-08-31 18:13:30 +00:00
|
|
|
import SearchBox from '../../containers/SearchBox';
|
2017-12-27 15:22:06 +00:00
|
|
|
import database from '../../lib/realm';
|
2017-12-08 19:13:21 +00:00
|
|
|
import RocketChat from '../../lib/rocketchat';
|
|
|
|
import RoomItem from '../../presentation/RoomItem';
|
|
|
|
import styles from './styles';
|
2018-04-26 17:33:43 +00:00
|
|
|
import LoggedView from '../View';
|
2018-05-18 17:55:08 +00:00
|
|
|
import log from '../../utils/log';
|
2018-06-01 17:38:13 +00:00
|
|
|
import I18n from '../../i18n';
|
2018-08-31 16:46:33 +00:00
|
|
|
import SortDropdown from './SortDropdown';
|
|
|
|
import ServerDropdown from './ServerDropdown';
|
|
|
|
import Touch from '../../utils/touch';
|
|
|
|
import { toggleSortDropdown } from '../../actions/rooms';
|
|
|
|
|
|
|
|
const ROW_HEIGHT = 70;
|
2018-09-05 18:15:03 +00:00
|
|
|
const SCROLL_OFFSET = 56;
|
2018-08-31 16:46:33 +00:00
|
|
|
|
|
|
|
const isAndroid = () => Platform.OS === 'android';
|
|
|
|
const getItemLayout = (data, index) => ({ length: ROW_HEIGHT, offset: ROW_HEIGHT * index, index });
|
|
|
|
const leftButtons = [{
|
|
|
|
id: 'settings',
|
|
|
|
icon: { uri: 'settings', scale: Dimensions.get('window').scale },
|
|
|
|
testID: 'rooms-list-view-sidebar'
|
|
|
|
}];
|
|
|
|
const rightButtons = [{
|
2018-08-31 18:13:30 +00:00
|
|
|
id: 'newMessage',
|
2018-08-31 16:46:33 +00:00
|
|
|
icon: { uri: 'new_channel', scale: Dimensions.get('window').scale },
|
|
|
|
testID: 'rooms-list-view-create-channel'
|
|
|
|
}];
|
|
|
|
|
|
|
|
if (Platform.OS === 'android') {
|
|
|
|
rightButtons.push({
|
|
|
|
id: 'search',
|
|
|
|
icon: { uri: 'search', scale: Dimensions.get('window').scale }
|
|
|
|
});
|
|
|
|
}
|
2017-08-03 18:23:43 +00:00
|
|
|
|
2018-09-04 14:29:20 +00:00
|
|
|
@connect(state => ({
|
|
|
|
userId: state.login.user && state.login.user.id,
|
|
|
|
server: state.server.server,
|
2018-09-11 16:32:52 +00:00
|
|
|
baseUrl: state.settings.baseUrl || state.server ? state.server.server : '',
|
2018-09-04 14:29:20 +00:00
|
|
|
searchText: state.rooms.searchText,
|
|
|
|
loadingServer: state.server.loading,
|
|
|
|
showServerDropdown: state.rooms.showServerDropdown,
|
|
|
|
showSortDropdown: state.rooms.showSortDropdown,
|
|
|
|
sortBy: state.sortPreferences.sortBy,
|
|
|
|
groupByType: state.sortPreferences.groupByType,
|
|
|
|
showFavorites: state.sortPreferences.showFavorites,
|
2018-09-11 16:32:52 +00:00
|
|
|
showUnread: state.sortPreferences.showUnread,
|
|
|
|
useRealName: state.settings.UI_Use_Real_Name
|
2018-09-04 14:29:20 +00:00
|
|
|
}), dispatch => ({
|
2018-08-31 16:46:33 +00:00
|
|
|
toggleSortDropdown: () => dispatch(toggleSortDropdown())
|
2017-08-13 01:35:09 +00:00
|
|
|
}))
|
2018-07-10 13:40:32 +00:00
|
|
|
/** @extends React.Component */
|
2018-04-26 17:33:43 +00:00
|
|
|
export default class RoomsListView extends LoggedView {
|
2018-08-31 16:46:33 +00:00
|
|
|
static navigatorButtons = {
|
|
|
|
leftButtons, rightButtons
|
|
|
|
}
|
|
|
|
|
|
|
|
static navigatorStyle = {
|
|
|
|
navBarCustomView: 'RoomsListHeaderView',
|
2018-09-05 18:15:03 +00:00
|
|
|
navBarComponentAlignment: 'fill',
|
2018-08-31 16:46:33 +00:00
|
|
|
navBarBackgroundColor: isAndroid() ? '#2F343D' : undefined,
|
|
|
|
navBarTextColor: isAndroid() ? '#FFF' : undefined,
|
|
|
|
navBarButtonColor: isAndroid() ? '#FFF' : undefined
|
|
|
|
}
|
|
|
|
|
2017-08-05 18:16:32 +00:00
|
|
|
static propTypes = {
|
2018-07-10 13:40:32 +00:00
|
|
|
navigator: PropTypes.object,
|
|
|
|
userId: PropTypes.string,
|
2018-09-11 16:32:52 +00:00
|
|
|
baseUrl: PropTypes.string,
|
2017-12-08 19:13:21 +00:00
|
|
|
server: PropTypes.string,
|
2018-08-01 19:35:06 +00:00
|
|
|
searchText: PropTypes.string,
|
2018-08-31 16:46:33 +00:00
|
|
|
loadingServer: PropTypes.bool,
|
|
|
|
showServerDropdown: PropTypes.bool,
|
|
|
|
showSortDropdown: PropTypes.bool,
|
2018-09-04 14:29:20 +00:00
|
|
|
sortBy: PropTypes.string,
|
|
|
|
groupByType: PropTypes.bool,
|
|
|
|
showFavorites: PropTypes.bool,
|
|
|
|
showUnread: PropTypes.bool,
|
2018-09-11 16:32:52 +00:00
|
|
|
toggleSortDropdown: PropTypes.func,
|
|
|
|
useRealName: PropTypes.bool
|
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) {
|
2018-04-26 17:33:43 +00:00
|
|
|
super('RoomsListView', props);
|
2017-09-21 17:08:00 +00:00
|
|
|
|
2018-09-11 16:32:52 +00:00
|
|
|
this.data = [];
|
2017-08-10 16:16:32 +00:00
|
|
|
this.state = {
|
2018-05-24 20:17:00 +00:00
|
|
|
search: [],
|
2018-08-31 16:46:33 +00:00
|
|
|
loading: true,
|
|
|
|
chats: [],
|
|
|
|
unread: [],
|
|
|
|
favorites: [],
|
|
|
|
channels: [],
|
|
|
|
privateGroup: [],
|
|
|
|
direct: [],
|
|
|
|
livechat: []
|
2017-08-10 16:16:32 +00:00
|
|
|
};
|
2018-07-10 13:40:32 +00:00
|
|
|
props.navigator.setOnNavigatorEvent(this.onNavigatorEvent.bind(this));
|
2017-08-21 00:11:46 +00:00
|
|
|
}
|
2017-09-21 17:08:00 +00:00
|
|
|
|
2018-08-31 16:46:33 +00:00
|
|
|
componentWillMount() {
|
2018-07-10 13:40:32 +00:00
|
|
|
this.initDefaultHeader();
|
2017-08-13 01:35:09 +00:00
|
|
|
}
|
2017-09-21 17:08:00 +00:00
|
|
|
|
2018-07-18 20:34:59 +00:00
|
|
|
componentDidMount() {
|
|
|
|
this.getSubscriptions();
|
|
|
|
}
|
|
|
|
|
2018-08-31 16:46:33 +00:00
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
if (nextProps.server && this.props.loadingServer !== nextProps.loadingServer) {
|
|
|
|
if (nextProps.loadingServer) {
|
|
|
|
this.setState({ loading: true });
|
|
|
|
} else {
|
|
|
|
this.getSubscriptions();
|
|
|
|
}
|
|
|
|
} else if (this.props.searchText !== nextProps.searchText) {
|
|
|
|
this.search(nextProps.searchText);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
|
|
|
return !(isEqual(this.props, nextProps) && isEqual(this.state, nextState));
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
if (!(
|
2018-09-04 14:29:20 +00:00
|
|
|
(prevProps.sortBy === this.props.sortBy) &&
|
|
|
|
(prevProps.groupByType === this.props.groupByType) &&
|
|
|
|
(prevProps.showFavorites === this.props.showFavorites) &&
|
|
|
|
(prevProps.showUnread === this.props.showUnread)
|
2018-08-31 16:46:33 +00:00
|
|
|
)) {
|
2018-07-18 20:34:59 +00:00
|
|
|
this.getSubscriptions();
|
2017-11-07 20:25:04 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-02 21:31:44 +00:00
|
|
|
|
2017-08-09 16:19:17 +00:00
|
|
|
componentWillUnmount() {
|
2018-08-31 16:46:33 +00:00
|
|
|
this.removeListener(this.data);
|
|
|
|
this.removeListener(this.unread);
|
|
|
|
this.removeListener(this.favorites);
|
|
|
|
this.removeListener(this.channels);
|
|
|
|
this.removeListener(this.privateGroup);
|
|
|
|
this.removeListener(this.direct);
|
|
|
|
this.removeListener(this.livechat);
|
|
|
|
|
2018-08-01 19:35:06 +00:00
|
|
|
if (this.timeout) {
|
|
|
|
clearTimeout(this.timeout);
|
|
|
|
}
|
2018-07-10 13:40:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onNavigatorEvent(event) {
|
|
|
|
const { navigator } = this.props;
|
|
|
|
if (event.type === 'NavBarButtonPress') {
|
2018-08-31 18:13:30 +00:00
|
|
|
if (event.id === 'newMessage') {
|
|
|
|
this.props.navigator.showModal({
|
|
|
|
screen: 'NewMessageView',
|
|
|
|
title: I18n.t('New_Message'),
|
2018-07-10 13:40:32 +00:00
|
|
|
passProps: {
|
2018-08-31 18:13:30 +00:00
|
|
|
onPressItem: this._onPressItem
|
2018-07-10 13:40:32 +00:00
|
|
|
}
|
|
|
|
});
|
2018-08-31 16:46:33 +00:00
|
|
|
} else if (event.id === 'settings') {
|
2018-07-10 13:40:32 +00:00
|
|
|
navigator.toggleDrawer({
|
2018-08-31 16:46:33 +00:00
|
|
|
side: 'left'
|
2018-07-10 13:40:32 +00:00
|
|
|
});
|
|
|
|
} else if (event.id === 'search') {
|
|
|
|
this.initSearchingAndroid();
|
2018-08-31 16:46:33 +00:00
|
|
|
} else if (event.id === 'cancelSearch' || event.id === 'back') {
|
2018-07-10 13:40:32 +00:00
|
|
|
this.cancelSearchingAndroid();
|
|
|
|
}
|
|
|
|
} else if (event.type === 'ScreenChangedEvent' && event.id === 'didAppear') {
|
2018-08-31 16:46:33 +00:00
|
|
|
navigator.setDrawerEnabled({
|
2018-07-10 13:40:32 +00:00
|
|
|
side: 'left',
|
|
|
|
enabled: true
|
|
|
|
});
|
|
|
|
}
|
2017-08-12 20:52:55 +00:00
|
|
|
}
|
|
|
|
|
2018-07-18 20:34:59 +00:00
|
|
|
getSubscriptions = () => {
|
|
|
|
if (this.props.server && this.hasActiveDB()) {
|
2018-09-04 14:29:20 +00:00
|
|
|
if (this.props.sortBy === 'alphabetical') {
|
2018-08-31 16:46:33 +00:00
|
|
|
this.data = database.objects('subscriptions').filtered('archived != true && open == true').sorted('name', false);
|
|
|
|
} else {
|
|
|
|
this.data = database.objects('subscriptions').filtered('archived != true && open == true').sorted('roomUpdatedAt', true);
|
|
|
|
}
|
|
|
|
|
|
|
|
let chats = [];
|
|
|
|
let unread = [];
|
|
|
|
let favorites = [];
|
|
|
|
let channels = [];
|
|
|
|
let privateGroup = [];
|
|
|
|
let direct = [];
|
|
|
|
let livechat = [];
|
|
|
|
|
|
|
|
// unread
|
2018-09-04 14:29:20 +00:00
|
|
|
if (this.props.showUnread) {
|
2018-08-31 16:46:33 +00:00
|
|
|
this.unread = this.data.filtered('archived != true && open == true').sorted('name', false).filtered('(unread > 0 || alert == true)');
|
|
|
|
unread = this.unread.slice();
|
|
|
|
setTimeout(() => {
|
|
|
|
this.unread.addListener(() => this.setState({ unread: this.unread.slice() }));
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.removeListener(unread);
|
|
|
|
}
|
|
|
|
// favorites
|
2018-09-04 14:29:20 +00:00
|
|
|
if (this.props.showFavorites) {
|
2018-08-31 16:46:33 +00:00
|
|
|
this.favorites = this.data.filtered('f == true');
|
|
|
|
favorites = this.favorites.slice();
|
|
|
|
setTimeout(() => {
|
|
|
|
this.favorites.addListener(() => this.setState({ favorites: this.favorites.slice() }));
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.removeListener(favorites);
|
|
|
|
}
|
|
|
|
// type
|
2018-09-04 14:29:20 +00:00
|
|
|
if (this.props.groupByType) {
|
2018-08-31 16:46:33 +00:00
|
|
|
// channels
|
|
|
|
this.channels = this.data.filtered('t == $0', 'c');
|
|
|
|
channels = this.channels.slice();
|
|
|
|
// private
|
|
|
|
this.privateGroup = this.data.filtered('t == $0', 'p');
|
|
|
|
privateGroup = this.privateGroup.slice();
|
|
|
|
// direct
|
|
|
|
this.direct = this.data.filtered('t == $0', 'd');
|
|
|
|
direct = this.direct.slice();
|
|
|
|
// livechat
|
|
|
|
this.livechat = this.data.filtered('t == $0', 'l');
|
|
|
|
livechat = this.livechat.slice();
|
|
|
|
setTimeout(() => {
|
|
|
|
this.channels.addListener(() => this.setState({ channels: this.channels.slice() }));
|
|
|
|
this.privateGroup.addListener(() => this.setState({ privateGroup: this.privateGroup.slice() }));
|
|
|
|
this.direct.addListener(() => this.setState({ direct: this.direct.slice() }));
|
|
|
|
this.livechat.addListener(() => this.setState({ livechat: this.livechat.slice() }));
|
|
|
|
});
|
|
|
|
this.removeListener(this.chats);
|
|
|
|
} else {
|
|
|
|
// chats
|
|
|
|
this.chats = this.data.filtered('(unread == 0 && alert == false)');
|
|
|
|
chats = this.chats.slice();
|
|
|
|
setTimeout(() => {
|
|
|
|
this.chats.addListener(() => this.setState({ chats: this.chats.slice() }));
|
|
|
|
});
|
|
|
|
this.removeListener(this.channels);
|
|
|
|
this.removeListener(this.privateGroup);
|
|
|
|
this.removeListener(this.direct);
|
|
|
|
this.removeListener(this.livechat);
|
|
|
|
}
|
|
|
|
|
|
|
|
// setState
|
|
|
|
this.setState({
|
|
|
|
chats, unread, favorites, channels, privateGroup, direct, livechat
|
|
|
|
});
|
2018-07-18 20:34:59 +00:00
|
|
|
}
|
2018-08-01 19:35:06 +00:00
|
|
|
this.timeout = setTimeout(() => {
|
|
|
|
this.setState({ loading: false });
|
|
|
|
}, 200);
|
2018-07-18 20:34:59 +00:00
|
|
|
}
|
|
|
|
|
2018-08-31 16:46:33 +00:00
|
|
|
removeListener = (data) => {
|
|
|
|
if (data && data.removeAllListeners) {
|
|
|
|
data.removeAllListeners();
|
2018-07-10 13:40:32 +00:00
|
|
|
}
|
2018-08-31 16:46:33 +00:00
|
|
|
}
|
2018-07-10 13:40:32 +00:00
|
|
|
|
2018-08-31 16:46:33 +00:00
|
|
|
initDefaultHeader = () => {
|
|
|
|
const { navigator } = this.props;
|
|
|
|
navigator.setButtons({ leftButtons, rightButtons });
|
|
|
|
navigator.setStyle({
|
|
|
|
navBarCustomView: 'RoomsListHeaderView',
|
2018-09-05 18:15:03 +00:00
|
|
|
navBarComponentAlignment: 'fill',
|
2018-08-31 16:46:33 +00:00
|
|
|
navBarBackgroundColor: isAndroid() ? '#2F343D' : undefined,
|
|
|
|
navBarTextColor: isAndroid() ? '#FFF' : undefined,
|
|
|
|
navBarButtonColor: isAndroid() ? '#FFF' : undefined
|
2018-07-10 13:40:32 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
initSearchingAndroid = () => {
|
|
|
|
const { navigator } = this.props;
|
|
|
|
navigator.setButtons({
|
|
|
|
leftButtons: [{
|
|
|
|
id: 'cancelSearch',
|
2018-08-31 16:46:33 +00:00
|
|
|
icon: { uri: 'back', scale: Dimensions.get('window').scale }
|
2018-07-10 13:40:32 +00:00
|
|
|
}],
|
|
|
|
rightButtons: []
|
|
|
|
});
|
|
|
|
navigator.setStyle({
|
|
|
|
navBarCustomView: 'RoomsListSearchView',
|
|
|
|
navBarComponentAlignment: 'fill'
|
|
|
|
});
|
|
|
|
BackHandler.addEventListener('hardwareBackPress', this.handleBackPress);
|
|
|
|
}
|
|
|
|
|
2018-07-18 20:34:59 +00:00
|
|
|
// this is necessary during development (enables Cmd + r)
|
|
|
|
hasActiveDB = () => database && database.databases && database.databases.activeDB;
|
|
|
|
|
2018-07-10 13:40:32 +00:00
|
|
|
cancelSearchingAndroid = () => {
|
|
|
|
if (Platform.OS === 'android') {
|
|
|
|
this.setState({ search: [] });
|
|
|
|
this.initDefaultHeader();
|
2018-08-31 16:46:33 +00:00
|
|
|
Keyboard.dismiss();
|
2018-07-10 13:40:32 +00:00
|
|
|
BackHandler.removeEventListener('hardwareBackPress', this.handleBackPress);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleBackPress = () => {
|
|
|
|
this.cancelSearchingAndroid();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-08-31 16:46:33 +00:00
|
|
|
_isUnread = item => item.unread > 0 || item.alert
|
2018-02-16 18:34:25 +00:00
|
|
|
|
2018-08-31 18:13:30 +00:00
|
|
|
search = async(text) => {
|
|
|
|
const result = await RocketChat.search({ text });
|
|
|
|
this.setState({
|
|
|
|
search: result
|
|
|
|
});
|
2018-03-02 21:31:44 +00:00
|
|
|
}
|
2017-08-10 16:16:32 +00:00
|
|
|
|
2018-05-24 20:17:00 +00:00
|
|
|
goRoom = (rid, name) => {
|
2018-07-10 13:40:32 +00:00
|
|
|
this.props.navigator.push({
|
|
|
|
screen: 'RoomView',
|
|
|
|
title: name,
|
2018-08-31 18:13:30 +00:00
|
|
|
backButtonTitle: '',
|
2018-09-19 14:18:32 +00:00
|
|
|
passProps: { rid }
|
2018-05-24 20:17:00 +00:00
|
|
|
});
|
2018-07-10 13:40:32 +00:00
|
|
|
this.cancelSearchingAndroid();
|
2018-05-24 20:17:00 +00:00
|
|
|
}
|
|
|
|
|
2018-03-02 21:31:44 +00:00
|
|
|
_onPressItem = async(item = {}) => {
|
|
|
|
if (!item.search) {
|
2018-05-24 20:17:00 +00:00
|
|
|
const { rid, name } = item;
|
|
|
|
return this.goRoom(rid, name);
|
2017-08-10 16:16:32 +00:00
|
|
|
}
|
2018-03-02 21:31:44 +00:00
|
|
|
if (item.t === 'd') {
|
2018-05-07 20:43:26 +00:00
|
|
|
// if user is using the search we need first to join/create room
|
|
|
|
try {
|
2018-05-24 20:17:00 +00:00
|
|
|
const { username } = item;
|
|
|
|
const sub = await RocketChat.createDirectMessage(username);
|
|
|
|
const { rid } = sub;
|
|
|
|
return this.goRoom(rid, username);
|
2018-05-18 17:55:08 +00:00
|
|
|
} catch (e) {
|
|
|
|
log('RoomsListView._onPressItem', e);
|
2018-05-07 20:43:26 +00:00
|
|
|
}
|
2018-05-24 20:17:00 +00:00
|
|
|
} else {
|
|
|
|
const { rid, name } = item;
|
|
|
|
return this.goRoom(rid, name);
|
2018-03-02 21:31:44 +00:00
|
|
|
}
|
2017-08-05 18:16:32 +00:00
|
|
|
}
|
2017-09-21 17:08:00 +00:00
|
|
|
|
2018-09-05 18:15:03 +00:00
|
|
|
toggleSort = () => {
|
|
|
|
if (Platform.OS === 'ios') {
|
|
|
|
this.scroll.scrollTo({ x: 0, y: SCROLL_OFFSET, animated: true });
|
|
|
|
} else {
|
|
|
|
this.scroll.scrollTo({ x: 0, y: 0, animated: true });
|
|
|
|
}
|
|
|
|
setTimeout(() => {
|
|
|
|
this.props.toggleSortDropdown();
|
|
|
|
}, 100);
|
|
|
|
}
|
2018-08-31 16:46:33 +00:00
|
|
|
|
|
|
|
renderHeader = () => {
|
|
|
|
if (this.state.search.length > 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return this.renderSort();
|
|
|
|
}
|
|
|
|
|
|
|
|
renderSort = () => (
|
|
|
|
<Touch
|
|
|
|
onPress={this.toggleSort}
|
|
|
|
style={styles.dropdownContainerHeader}
|
|
|
|
>
|
|
|
|
<View style={styles.sortItemContainer}>
|
2018-09-04 14:29:20 +00:00
|
|
|
<Text style={styles.sortToggleText}>{I18n.t('Sorting_by', { key: I18n.t(this.props.sortBy === 'alphabetical' ? 'name' : 'activity') })}</Text>
|
2018-08-31 16:46:33 +00:00
|
|
|
<Image style={styles.sortIcon} source={{ uri: 'group_type' }} />
|
|
|
|
</View>
|
|
|
|
</Touch>
|
|
|
|
)
|
|
|
|
|
|
|
|
renderSearchBar = () => {
|
|
|
|
if (Platform.OS === 'ios') {
|
2018-08-31 18:13:30 +00:00
|
|
|
return <SearchBox onChangeText={text => this.search(text)} testID='rooms-list-view-search' />;
|
2018-08-31 16:46:33 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-10 16:16:32 +00:00
|
|
|
|
2018-02-19 21:15:31 +00:00
|
|
|
renderItem = ({ item }) => {
|
2018-07-10 13:40:32 +00:00
|
|
|
const id = item.rid.replace(this.props.userId, '').trim();
|
2018-09-11 16:32:52 +00:00
|
|
|
const { useRealName } = this.props;
|
2018-02-16 18:34:25 +00:00
|
|
|
return (<RoomItem
|
2017-11-22 16:40:59 +00:00
|
|
|
alert={item.alert}
|
2017-09-21 17:08:00 +00:00
|
|
|
unread={item.unread}
|
2017-11-22 16:40:59 +00:00
|
|
|
userMentions={item.userMentions}
|
|
|
|
favorite={item.f}
|
2018-02-16 18:34:25 +00:00
|
|
|
lastMessage={item.lastMessage}
|
2018-09-11 16:32:52 +00:00
|
|
|
name={(useRealName && item.fname) || item.name}
|
2017-11-28 11:01:18 +00:00
|
|
|
_updatedAt={item.roomUpdatedAt}
|
2017-09-21 17:08:00 +00:00
|
|
|
key={item._id}
|
2018-02-16 18:34:25 +00:00
|
|
|
id={id}
|
2017-08-21 16:16:23 +00:00
|
|
|
type={item.t}
|
2018-09-11 16:32:52 +00:00
|
|
|
baseUrl={this.props.baseUrl}
|
2017-11-28 17:47:56 +00:00
|
|
|
onPress={() => this._onPressItem(item)}
|
2018-05-23 13:39:18 +00:00
|
|
|
testID={`rooms-list-view-item-${ item.name }`}
|
2018-08-31 16:46:33 +00:00
|
|
|
height={ROW_HEIGHT}
|
2018-02-16 18:34:25 +00:00
|
|
|
/>);
|
|
|
|
}
|
2017-09-21 17:08:00 +00:00
|
|
|
|
2018-08-31 16:46:33 +00:00
|
|
|
renderSeparator = () => <View style={styles.separator} />;
|
|
|
|
|
|
|
|
renderSection = (data, header) => {
|
2018-09-11 16:32:52 +00:00
|
|
|
if (header === 'Unread' && !this.props.showUnread) {
|
|
|
|
return null;
|
|
|
|
} else if (header === 'Favorites' && !this.props.showFavorites) {
|
|
|
|
return null;
|
|
|
|
} else if (['Channels', 'Direct_Messages', 'Private_Groups', 'Livechat'].includes(header) && !this.props.groupByType) {
|
|
|
|
return null;
|
|
|
|
} else if (header === 'Chats' && this.props.groupByType) {
|
|
|
|
return null;
|
|
|
|
}
|
2018-08-31 16:46:33 +00:00
|
|
|
if (data.length > 0) {
|
|
|
|
return (
|
|
|
|
<FlatList
|
|
|
|
data={data}
|
|
|
|
extraData={data}
|
|
|
|
keyExtractor={item => item.rid}
|
|
|
|
style={styles.list}
|
|
|
|
renderItem={this.renderItem}
|
|
|
|
ItemSeparatorComponent={this.renderSeparator}
|
|
|
|
ListHeaderComponent={() => (
|
|
|
|
<View style={styles.groupTitleContainer}>
|
|
|
|
<Text style={styles.groupTitle}>{I18n.t(header)}</Text>
|
|
|
|
</View>
|
|
|
|
)}
|
|
|
|
getItemLayout={getItemLayout}
|
|
|
|
enableEmptySections
|
|
|
|
removeClippedSubviews
|
|
|
|
keyboardShouldPersistTaps='always'
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-08-01 19:35:06 +00:00
|
|
|
renderList = () => {
|
2018-08-31 16:46:33 +00:00
|
|
|
const {
|
|
|
|
search, chats, unread, favorites, channels, direct, privateGroup, livechat
|
|
|
|
} = this.state;
|
|
|
|
|
|
|
|
if (search.length > 0) {
|
|
|
|
return (
|
|
|
|
<FlatList
|
|
|
|
data={search}
|
|
|
|
extraData={search}
|
|
|
|
keyExtractor={item => item.rid}
|
|
|
|
style={styles.list}
|
|
|
|
renderItem={this.renderItem}
|
|
|
|
ItemSeparatorComponent={this.renderSeparator}
|
|
|
|
getItemLayout={getItemLayout}
|
|
|
|
enableEmptySections
|
|
|
|
removeClippedSubviews
|
|
|
|
keyboardShouldPersistTaps='always'
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<View style={styles.container}>
|
|
|
|
{this.renderSection(unread, 'Unread')}
|
|
|
|
{this.renderSection(favorites, 'Favorites')}
|
|
|
|
{this.renderSection(channels, 'Channels')}
|
|
|
|
{this.renderSection(direct, 'Direct_Messages')}
|
|
|
|
{this.renderSection(privateGroup, 'Private_Groups')}
|
|
|
|
{this.renderSection(livechat, 'Livechat')}
|
|
|
|
{this.renderSection(chats, 'Chats')}
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderScroll = () => {
|
2018-08-01 19:35:06 +00:00
|
|
|
if (this.state.loading) {
|
|
|
|
return <ActivityIndicator style={styles.loading} />;
|
|
|
|
}
|
2018-08-31 16:46:33 +00:00
|
|
|
|
2018-08-01 19:35:06 +00:00
|
|
|
return (
|
2018-08-31 16:46:33 +00:00
|
|
|
<ScrollView
|
2018-09-05 18:15:03 +00:00
|
|
|
ref={ref => this.scroll = ref}
|
|
|
|
contentOffset={Platform.OS === 'ios' ? { x: 0, y: SCROLL_OFFSET } : {}}
|
2018-08-01 19:35:06 +00:00
|
|
|
keyboardShouldPersistTaps='always'
|
|
|
|
testID='rooms-list-view-list'
|
2018-08-31 16:46:33 +00:00
|
|
|
>
|
|
|
|
{this.renderSearchBar()}
|
|
|
|
{this.renderHeader()}
|
|
|
|
{this.renderList()}
|
|
|
|
</ScrollView>
|
2018-08-01 19:35:06 +00:00
|
|
|
);
|
|
|
|
}
|
2017-09-21 17:08:00 +00:00
|
|
|
|
2018-08-31 16:46:33 +00:00
|
|
|
render = () => {
|
|
|
|
const {
|
2018-09-04 14:29:20 +00:00
|
|
|
sortBy, groupByType, showFavorites, showUnread, showServerDropdown, showSortDropdown
|
2018-08-31 16:46:33 +00:00
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<SafeAreaView style={styles.container} testID='rooms-list-view'>
|
|
|
|
{this.renderScroll()}
|
|
|
|
{showSortDropdown ?
|
|
|
|
<SortDropdown
|
|
|
|
close={this.toggleSort}
|
2018-09-04 14:29:20 +00:00
|
|
|
sortBy={sortBy}
|
|
|
|
groupByType={groupByType}
|
|
|
|
showFavorites={showFavorites}
|
|
|
|
showUnread={showUnread}
|
2018-08-31 16:46:33 +00:00
|
|
|
/> :
|
|
|
|
null}
|
|
|
|
{showServerDropdown ? <ServerDropdown navigator={this.props.navigator} /> : null}
|
|
|
|
</SafeAreaView>
|
|
|
|
);
|
|
|
|
}
|
2017-08-03 18:23:43 +00:00
|
|
|
}
|