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-09-25 19:28:42 +00:00
|
|
|
import {
|
2019-04-17 18:57:46 +00:00
|
|
|
View, FlatList, BackHandler, ActivityIndicator, Text, ScrollView, Keyboard, LayoutAnimation, InteractionManager
|
2018-09-25 19:28:42 +00:00
|
|
|
} from 'react-native';
|
2019-01-31 16:08:38 +00:00
|
|
|
import { connect } from 'react-redux';
|
2018-08-31 16:46:33 +00:00
|
|
|
import { isEqual } from 'lodash';
|
2019-03-12 16:23:06 +00:00
|
|
|
import { SafeAreaView, NavigationEvents } from 'react-navigation';
|
|
|
|
import Orientation from 'react-native-orientation-locker';
|
2018-05-18 17:55:08 +00:00
|
|
|
|
2019-04-04 18:08:40 +00:00
|
|
|
import database, { safeAddListener } from '../../lib/realm';
|
2017-12-08 19:13:21 +00:00
|
|
|
import RocketChat from '../../lib/rocketchat';
|
2019-03-29 19:36:07 +00:00
|
|
|
import RoomItem, { ROW_HEIGHT } from '../../presentation/RoomItem';
|
2017-12-08 19:13:21 +00:00
|
|
|
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';
|
2019-02-07 16:13:21 +00:00
|
|
|
import {
|
|
|
|
toggleSortDropdown as toggleSortDropdownAction,
|
|
|
|
openSearchHeader as openSearchHeaderAction,
|
2019-03-12 16:23:06 +00:00
|
|
|
closeSearchHeader as closeSearchHeaderAction
|
|
|
|
// roomsRequest as roomsRequestAction
|
2019-02-07 16:13:21 +00:00
|
|
|
} from '../../actions/rooms';
|
2018-11-27 19:40:53 +00:00
|
|
|
import { appStart as appStartAction } from '../../actions';
|
2018-12-21 10:55:35 +00:00
|
|
|
import debounce from '../../utils/debounce';
|
2019-01-29 19:52:56 +00:00
|
|
|
import { isIOS, isAndroid } from '../../utils/deviceInfo';
|
2019-03-12 16:23:06 +00:00
|
|
|
import RoomsListHeaderView from './Header';
|
|
|
|
import { DrawerButton, CustomHeaderButtons, Item } from '../../containers/HeaderButton';
|
|
|
|
import StatusBar from '../../containers/StatusBar';
|
2019-04-17 18:57:46 +00:00
|
|
|
import ListHeader from './ListHeader';
|
2018-08-31 16:46:33 +00:00
|
|
|
|
2018-09-05 18:15:03 +00:00
|
|
|
const SCROLL_OFFSET = 56;
|
2018-08-31 16:46:33 +00:00
|
|
|
|
2019-03-29 19:36:07 +00:00
|
|
|
const shouldUpdateProps = ['searchText', 'loadingServer', 'showServerDropdown', 'showSortDropdown', 'sortBy', 'groupByType', 'showFavorites', 'showUnread', 'useRealName', 'StoreLastMessage', 'appState'];
|
2018-08-31 16:46:33 +00:00
|
|
|
const getItemLayout = (data, index) => ({ length: ROW_HEIGHT, offset: ROW_HEIGHT * index, index });
|
2018-09-26 13:56:36 +00:00
|
|
|
const keyExtractor = item => item.rid;
|
|
|
|
|
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,
|
2018-12-05 20:52:08 +00:00
|
|
|
useRealName: state.settings.UI_Use_Real_Name,
|
2019-03-29 19:36:07 +00:00
|
|
|
appState: state.app.ready && state.app.foreground ? 'foreground' : 'background',
|
|
|
|
StoreLastMessage: state.settings.Store_Last_Message
|
2018-09-04 14:29:20 +00:00
|
|
|
}), dispatch => ({
|
2018-10-23 21:39:48 +00:00
|
|
|
toggleSortDropdown: () => dispatch(toggleSortDropdownAction()),
|
|
|
|
openSearchHeader: () => dispatch(openSearchHeaderAction()),
|
2018-11-27 19:40:53 +00:00
|
|
|
closeSearchHeader: () => dispatch(closeSearchHeaderAction()),
|
2019-03-12 16:23:06 +00:00
|
|
|
appStart: () => dispatch(appStartAction())
|
|
|
|
// roomsRequest: () => dispatch(roomsRequestAction())
|
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 {
|
2019-03-12 16:23:06 +00:00
|
|
|
static navigationOptions = ({ navigation }) => {
|
|
|
|
const searching = navigation.getParam('searching');
|
|
|
|
const cancelSearchingAndroid = navigation.getParam('cancelSearchingAndroid');
|
|
|
|
const onPressItem = navigation.getParam('onPressItem', () => {});
|
|
|
|
const initSearchingAndroid = navigation.getParam('initSearchingAndroid', () => {});
|
|
|
|
|
2018-10-23 21:39:48 +00:00
|
|
|
return {
|
2019-03-12 16:23:06 +00:00
|
|
|
headerLeft: (
|
|
|
|
searching
|
|
|
|
? (
|
|
|
|
<CustomHeaderButtons left>
|
|
|
|
<Item title='cancel' iconName='cross' onPress={cancelSearchingAndroid} />
|
|
|
|
</CustomHeaderButtons>
|
|
|
|
)
|
|
|
|
: <DrawerButton navigation={navigation} testID='rooms-list-view-sidebar' />
|
|
|
|
),
|
|
|
|
headerTitle: <RoomsListHeaderView />,
|
|
|
|
headerRight: (
|
|
|
|
searching
|
|
|
|
? null
|
|
|
|
: (
|
|
|
|
<CustomHeaderButtons>
|
|
|
|
{isAndroid ? <Item title='search' iconName='magnifier' onPress={initSearchingAndroid} /> : null}
|
|
|
|
<Item title='new' iconName='edit-rounded' onPress={() => navigation.navigate('NewMessageView', { onPressItem })} testID='rooms-list-view-create-channel' />
|
|
|
|
</CustomHeaderButtons>
|
|
|
|
)
|
|
|
|
)
|
2018-10-23 21:39:48 +00:00
|
|
|
};
|
2018-08-31 16:46:33 +00:00
|
|
|
}
|
|
|
|
|
2017-08-05 18:16:32 +00:00
|
|
|
static propTypes = {
|
2019-03-12 16:23:06 +00:00
|
|
|
navigation: PropTypes.object,
|
2018-07-10 13:40:32 +00:00
|
|
|
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-10-02 12:33:21 +00:00
|
|
|
useRealName: PropTypes.bool,
|
2019-03-29 19:36:07 +00:00
|
|
|
StoreLastMessage: PropTypes.bool,
|
2019-03-12 16:23:06 +00:00
|
|
|
// appState: PropTypes.string,
|
2018-10-23 21:39:48 +00:00
|
|
|
toggleSortDropdown: PropTypes.func,
|
|
|
|
openSearchHeader: PropTypes.func,
|
2018-11-27 19:40:53 +00:00
|
|
|
closeSearchHeader: PropTypes.func,
|
2019-03-12 16:23:06 +00:00
|
|
|
appStart: PropTypes.func
|
|
|
|
// roomsRequest: PropTypes.func
|
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);
|
2019-04-17 18:57:46 +00:00
|
|
|
console.time(`${ this.constructor.name } init`);
|
|
|
|
console.time(`${ this.constructor.name } mount`);
|
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-11-27 19:40:53 +00:00
|
|
|
searching: false,
|
2018-05-24 20:17:00 +00:00
|
|
|
search: [],
|
2018-08-31 16:46:33 +00:00
|
|
|
loading: true,
|
|
|
|
chats: [],
|
|
|
|
unread: [],
|
|
|
|
favorites: [],
|
2019-04-08 12:35:28 +00:00
|
|
|
discussions: [],
|
2018-08-31 16:46:33 +00:00
|
|
|
channels: [],
|
|
|
|
privateGroup: [],
|
|
|
|
direct: [],
|
|
|
|
livechat: []
|
2017-08-10 16:16:32 +00:00
|
|
|
};
|
2019-03-12 16:23:06 +00:00
|
|
|
Orientation.unlockAllOrientations();
|
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();
|
2019-03-12 16:23:06 +00:00
|
|
|
const { navigation } = this.props;
|
|
|
|
navigation.setParams({
|
|
|
|
onPressItem: this._onPressItem, initSearchingAndroid: this.initSearchingAndroid, cancelSearchingAndroid: this.cancelSearchingAndroid
|
|
|
|
});
|
2019-04-17 18:57:46 +00:00
|
|
|
console.timeEnd(`${ this.constructor.name } mount`);
|
2018-07-18 20:34:59 +00:00
|
|
|
}
|
|
|
|
|
2018-08-31 16:46:33 +00:00
|
|
|
componentWillReceiveProps(nextProps) {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { loadingServer, searchText } = this.props;
|
|
|
|
|
|
|
|
if (nextProps.server && loadingServer !== nextProps.loadingServer) {
|
2018-08-31 16:46:33 +00:00
|
|
|
if (nextProps.loadingServer) {
|
2018-10-23 21:39:48 +00:00
|
|
|
this.internalSetState({ loading: true });
|
2018-08-31 16:46:33 +00:00
|
|
|
} else {
|
|
|
|
this.getSubscriptions();
|
|
|
|
}
|
2018-09-25 19:28:42 +00:00
|
|
|
} else if (searchText !== nextProps.searchText) {
|
2018-08-31 16:46:33 +00:00
|
|
|
this.search(nextProps.searchText);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
2018-12-21 10:55:35 +00:00
|
|
|
// eslint-disable-next-line react/destructuring-assignment
|
|
|
|
const propsUpdated = shouldUpdateProps.some(key => nextProps[key] !== this.props[key]);
|
|
|
|
if (propsUpdated) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { loading, searching } = this.state;
|
|
|
|
if (nextState.loading !== loading) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (nextState.searching !== searching) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { search } = this.state;
|
|
|
|
if (!isEqual(nextState.search, search)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2018-08-31 16:46:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate(prevProps) {
|
2018-09-25 19:28:42 +00:00
|
|
|
const {
|
2019-03-12 16:23:06 +00:00
|
|
|
sortBy, groupByType, showFavorites, showUnread
|
2018-09-25 19:28:42 +00:00
|
|
|
} = this.props;
|
|
|
|
|
2018-08-31 16:46:33 +00:00
|
|
|
if (!(
|
2018-09-25 19:28:42 +00:00
|
|
|
(prevProps.sortBy === sortBy)
|
|
|
|
&& (prevProps.groupByType === groupByType)
|
|
|
|
&& (prevProps.showFavorites === showFavorites)
|
|
|
|
&& (prevProps.showUnread === 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
|
|
|
}
|
2019-03-12 16:23:06 +00:00
|
|
|
// removed for now... we may not need it anymore
|
|
|
|
// else if (appState === 'foreground' && appState !== prevProps.appState) {
|
|
|
|
// // roomsRequest();
|
|
|
|
// }
|
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() {
|
2019-04-17 18:57:46 +00:00
|
|
|
if (this.data && this.data.removeAllListeners) {
|
|
|
|
this.data.removeAllListeners();
|
|
|
|
}
|
|
|
|
if (this.getSubscriptions && this.getSubscriptions.stop) {
|
|
|
|
this.getSubscriptions.stop();
|
|
|
|
}
|
|
|
|
if (this.updateStateInteraction && this.updateStateInteraction.cancel) {
|
|
|
|
this.updateStateInteraction.cancel();
|
|
|
|
}
|
|
|
|
console.countReset(`${ this.constructor.name }.render calls`);
|
2017-08-12 20:52:55 +00:00
|
|
|
}
|
|
|
|
|
2019-04-17 18:57:46 +00:00
|
|
|
// eslint-disable-next-line react/sort-comp
|
2018-10-23 21:39:48 +00:00
|
|
|
internalSetState = (...args) => {
|
2019-03-27 20:06:57 +00:00
|
|
|
const { navigation } = this.props;
|
|
|
|
if (isIOS && navigation.isFocused()) {
|
2018-10-29 13:54:23 +00:00
|
|
|
LayoutAnimation.easeInEaseOut();
|
|
|
|
}
|
2018-10-23 21:39:48 +00:00
|
|
|
this.setState(...args);
|
|
|
|
}
|
|
|
|
|
2019-04-17 18:57:46 +00:00
|
|
|
getSubscriptions = debounce(() => {
|
|
|
|
if (this.data && this.data.removeAllListeners) {
|
|
|
|
this.data.removeAllListeners();
|
|
|
|
}
|
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
const {
|
|
|
|
server, sortBy, showUnread, showFavorites, groupByType
|
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
if (server && this.hasActiveDB()) {
|
2019-04-17 17:01:03 +00:00
|
|
|
this.data = database.objects('subscriptions').filtered('archived != true && open == true && t != $0', 'l');
|
2018-09-25 19:28:42 +00:00
|
|
|
if (sortBy === 'alphabetical') {
|
2019-04-17 17:01:03 +00:00
|
|
|
this.data = this.data.sorted('name', false);
|
2018-08-31 16:46:33 +00:00
|
|
|
} else {
|
2019-04-17 17:01:03 +00:00
|
|
|
this.data = this.data.sorted('roomUpdatedAt', true);
|
2018-08-31 16:46:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// unread
|
2018-09-25 19:28:42 +00:00
|
|
|
if (showUnread) {
|
2019-04-17 17:01:03 +00:00
|
|
|
this.unread = this.data.filtered('(unread > 0 || alert == true)');
|
2018-08-31 16:46:33 +00:00
|
|
|
} else {
|
2019-04-17 18:57:46 +00:00
|
|
|
this.unread = [];
|
2018-08-31 16:46:33 +00:00
|
|
|
}
|
|
|
|
// favorites
|
2018-09-25 19:28:42 +00:00
|
|
|
if (showFavorites) {
|
2018-08-31 16:46:33 +00:00
|
|
|
this.favorites = this.data.filtered('f == true');
|
|
|
|
} else {
|
2019-04-17 18:57:46 +00:00
|
|
|
this.favorites = [];
|
2018-08-31 16:46:33 +00:00
|
|
|
}
|
|
|
|
// type
|
2018-09-25 19:28:42 +00:00
|
|
|
if (groupByType) {
|
2019-04-08 12:35:28 +00:00
|
|
|
this.discussions = this.data.filtered('prid != null');
|
|
|
|
this.channels = this.data.filtered('t == $0 AND prid == null', 'c');
|
|
|
|
this.privateGroup = this.data.filtered('t == $0 AND prid == null', 'p');
|
|
|
|
this.direct = this.data.filtered('t == $0 AND prid == null', 'd');
|
|
|
|
this.livechat = this.data.filtered('t == $0 AND prid == null', 'l');
|
2019-04-17 18:57:46 +00:00
|
|
|
} else if (showUnread) {
|
|
|
|
this.chats = this.data.filtered('(unread == 0 && alert == false)');
|
2018-08-31 16:46:33 +00:00
|
|
|
} else {
|
2019-04-17 18:57:46 +00:00
|
|
|
this.chats = this.data;
|
2018-08-31 16:46:33 +00:00
|
|
|
}
|
2019-04-17 18:57:46 +00:00
|
|
|
safeAddListener(this.data, this.updateState);
|
|
|
|
}
|
|
|
|
}, 300);
|
2018-08-31 16:46:33 +00:00
|
|
|
|
2019-04-17 18:57:46 +00:00
|
|
|
// eslint-disable-next-line react/sort-comp
|
|
|
|
updateState = debounce(() => {
|
|
|
|
this.updateStateInteraction = InteractionManager.runAfterInteractions(() => {
|
2018-10-23 21:39:48 +00:00
|
|
|
this.internalSetState({
|
2019-05-03 13:33:38 +00:00
|
|
|
chats: this.chats ? this.chats.slice() : [],
|
|
|
|
unread: this.unread ? this.unread.slice() : [],
|
|
|
|
favorites: this.favorites ? this.favorites.slice() : [],
|
|
|
|
discussions: this.discussions ? this.discussions.slice() : [],
|
|
|
|
channels: this.channels ? this.channels.slice() : [],
|
|
|
|
privateGroup: this.privateGroup ? this.privateGroup.slice() : [],
|
|
|
|
direct: this.direct ? this.direct.slice() : [],
|
|
|
|
livechat: this.livechat ? this.livechat.slice() : [],
|
2019-04-17 18:57:46 +00:00
|
|
|
loading: false
|
2018-08-31 16:46:33 +00:00
|
|
|
});
|
2019-04-24 18:36:29 +00:00
|
|
|
this.forceUpdate();
|
2019-04-17 18:57:46 +00:00
|
|
|
});
|
|
|
|
}, 300);
|
2018-10-18 17:56:49 +00:00
|
|
|
|
2018-07-10 13:40:32 +00:00
|
|
|
initSearchingAndroid = () => {
|
2019-03-12 16:23:06 +00:00
|
|
|
const { openSearchHeader, navigation } = this.props;
|
2018-11-27 19:40:53 +00:00
|
|
|
this.setState({ searching: true });
|
2019-03-12 16:23:06 +00:00
|
|
|
navigation.setParams({ searching: true });
|
2018-10-23 21:39:48 +00:00
|
|
|
openSearchHeader();
|
2018-07-10 13:40:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cancelSearchingAndroid = () => {
|
2019-01-29 19:52:56 +00:00
|
|
|
if (isAndroid) {
|
2019-03-12 16:23:06 +00:00
|
|
|
const { closeSearchHeader, navigation } = this.props;
|
2018-11-27 19:40:53 +00:00
|
|
|
this.setState({ searching: false });
|
2019-03-12 16:23:06 +00:00
|
|
|
navigation.setParams({ searching: false });
|
2018-10-23 21:39:48 +00:00
|
|
|
closeSearchHeader();
|
|
|
|
this.internalSetState({ search: [] });
|
2018-08-31 16:46:33 +00:00
|
|
|
Keyboard.dismiss();
|
2018-07-10 13:40:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-23 21:39:48 +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
|
|
|
handleBackPress = () => {
|
2018-11-27 19:40:53 +00:00
|
|
|
const { searching } = this.state;
|
|
|
|
const { appStart } = this.props;
|
|
|
|
if (searching) {
|
|
|
|
this.cancelSearchingAndroid();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
appStart('background');
|
|
|
|
return false;
|
2018-07-10 13:40:32 +00:00
|
|
|
}
|
|
|
|
|
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 });
|
2018-10-23 21:39:48 +00:00
|
|
|
this.internalSetState({
|
2018-08-31 18:13:30 +00:00
|
|
|
search: result
|
|
|
|
});
|
2018-03-02 21:31:44 +00:00
|
|
|
}
|
2017-08-10 16:16:32 +00:00
|
|
|
|
2019-04-08 12:35:28 +00:00
|
|
|
getRoomTitle = (item) => {
|
|
|
|
const { useRealName } = this.props;
|
|
|
|
return ((item.prid || useRealName) && item.fname) || item.name;
|
|
|
|
}
|
|
|
|
|
|
|
|
goRoom = (item) => {
|
2018-11-05 19:02:54 +00:00
|
|
|
this.cancelSearchingAndroid();
|
2019-03-12 16:23:06 +00:00
|
|
|
const { navigation } = this.props;
|
2019-04-08 12:35:28 +00:00
|
|
|
navigation.navigate('RoomView', {
|
|
|
|
rid: item.rid, name: this.getRoomTitle(item), t: item.t, prid: item.prid
|
|
|
|
});
|
2018-05-24 20:17:00 +00:00
|
|
|
}
|
|
|
|
|
2018-03-02 21:31:44 +00:00
|
|
|
_onPressItem = async(item = {}) => {
|
|
|
|
if (!item.search) {
|
2019-04-08 12:35:28 +00:00
|
|
|
return this.goRoom(item);
|
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;
|
2018-12-05 20:52:08 +00:00
|
|
|
const result = await RocketChat.createDirectMessage(username);
|
|
|
|
if (result.success) {
|
2018-12-21 10:55:35 +00:00
|
|
|
return this.goRoom({ rid: result.room._id, name: username, t: 'd' });
|
2018-12-05 20:52:08 +00:00
|
|
|
}
|
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 {
|
2019-04-08 12:35:28 +00:00
|
|
|
return this.goRoom(item);
|
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 = () => {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { toggleSortDropdown } = this.props;
|
|
|
|
|
2019-01-29 19:52:56 +00:00
|
|
|
const offset = isAndroid ? 0 : SCROLL_OFFSET;
|
2018-12-21 10:55:35 +00:00
|
|
|
if (this.scroll.scrollTo) {
|
|
|
|
this.scroll.scrollTo({ x: 0, y: offset, animated: true });
|
|
|
|
} else if (this.scroll.scrollToOffset) {
|
|
|
|
this.scroll.scrollToOffset({ offset });
|
2018-09-05 18:15:03 +00:00
|
|
|
}
|
|
|
|
setTimeout(() => {
|
2018-09-25 19:28:42 +00:00
|
|
|
toggleSortDropdown();
|
2018-09-05 18:15:03 +00:00
|
|
|
}, 100);
|
|
|
|
}
|
2018-08-31 16:46:33 +00:00
|
|
|
|
2018-09-26 13:56:36 +00:00
|
|
|
getScrollRef = ref => this.scroll = ref
|
|
|
|
|
2019-04-17 18:57:46 +00:00
|
|
|
renderListHeader = () => {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { search } = this.state;
|
|
|
|
const { sortBy } = this.props;
|
|
|
|
return (
|
2019-04-17 18:57:46 +00:00
|
|
|
<ListHeader
|
|
|
|
searchLength={search.length}
|
|
|
|
sortBy={sortBy}
|
|
|
|
onChangeSearchText={this.search}
|
|
|
|
toggleSort={this.toggleSort}
|
|
|
|
/>
|
2018-09-25 19:28:42 +00:00
|
|
|
);
|
|
|
|
}
|
2018-08-31 16:46:33 +00:00
|
|
|
|
2018-02-19 21:15:31 +00:00
|
|
|
renderItem = ({ item }) => {
|
2019-03-29 19:36:07 +00:00
|
|
|
const {
|
2019-04-08 12:35:28 +00:00
|
|
|
userId, baseUrl, StoreLastMessage
|
2019-03-29 19:36:07 +00:00
|
|
|
} = this.props;
|
2018-09-25 19:28:42 +00:00
|
|
|
const id = item.rid.replace(userId, '').trim();
|
|
|
|
|
2019-04-24 18:36:29 +00:00
|
|
|
if (item.search || (item.isValid && item.isValid())) {
|
|
|
|
return (
|
|
|
|
<RoomItem
|
|
|
|
alert={item.alert}
|
|
|
|
unread={item.unread}
|
|
|
|
userMentions={item.userMentions}
|
|
|
|
favorite={item.f}
|
2019-04-26 12:14:30 +00:00
|
|
|
lastMessage={item.lastMessage ? JSON.parse(JSON.stringify(item.lastMessage)) : null}
|
2019-04-24 18:36:29 +00:00
|
|
|
name={this.getRoomTitle(item)}
|
|
|
|
_updatedAt={item.roomUpdatedAt}
|
|
|
|
key={item._id}
|
|
|
|
id={id}
|
|
|
|
type={item.t}
|
|
|
|
baseUrl={baseUrl}
|
|
|
|
prid={item.prid}
|
|
|
|
showLastMessage={StoreLastMessage}
|
|
|
|
onPress={() => this._onPressItem(item)}
|
|
|
|
testID={`rooms-list-view-item-${ item.name }`}
|
|
|
|
height={ROW_HEIGHT}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return null;
|
2018-02-16 18:34:25 +00:00
|
|
|
}
|
2017-09-21 17:08:00 +00:00
|
|
|
|
2018-12-21 10:55:35 +00:00
|
|
|
renderSectionHeader = header => (
|
|
|
|
<View style={styles.groupTitleContainer}>
|
|
|
|
<Text style={styles.groupTitle}>{I18n.t(header)}</Text>
|
|
|
|
</View>
|
|
|
|
)
|
2018-08-31 16:46:33 +00:00
|
|
|
|
|
|
|
renderSection = (data, header) => {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { showUnread, showFavorites, groupByType } = this.props;
|
|
|
|
|
|
|
|
if (header === 'Unread' && !showUnread) {
|
2018-09-11 16:32:52 +00:00
|
|
|
return null;
|
2018-09-25 19:28:42 +00:00
|
|
|
} else if (header === 'Favorites' && !showFavorites) {
|
2018-09-11 16:32:52 +00:00
|
|
|
return null;
|
2019-04-08 12:35:28 +00:00
|
|
|
} else if (['Discussions', 'Channels', 'Direct_Messages', 'Private_Groups', 'Livechat'].includes(header) && !groupByType) {
|
2018-09-11 16:32:52 +00:00
|
|
|
return null;
|
2018-09-25 19:28:42 +00:00
|
|
|
} else if (header === 'Chats' && groupByType) {
|
2018-09-11 16:32:52 +00:00
|
|
|
return null;
|
|
|
|
}
|
2019-04-24 18:36:29 +00:00
|
|
|
if (data && data.length > 0) {
|
2018-08-31 16:46:33 +00:00
|
|
|
return (
|
|
|
|
<FlatList
|
|
|
|
data={data}
|
2018-09-26 13:56:36 +00:00
|
|
|
keyExtractor={keyExtractor}
|
2018-08-31 16:46:33 +00:00
|
|
|
style={styles.list}
|
|
|
|
renderItem={this.renderItem}
|
2018-12-06 12:53:20 +00:00
|
|
|
ListHeaderComponent={() => this.renderSectionHeader(header)}
|
2018-08-31 16:46:33 +00:00
|
|
|
getItemLayout={getItemLayout}
|
|
|
|
enableEmptySections
|
|
|
|
removeClippedSubviews
|
|
|
|
keyboardShouldPersistTaps='always'
|
2018-12-21 10:55:35 +00:00
|
|
|
initialNumToRender={12}
|
|
|
|
windowSize={7}
|
2018-08-31 16:46:33 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-08-01 19:35:06 +00:00
|
|
|
renderList = () => {
|
2018-08-31 16:46:33 +00:00
|
|
|
const {
|
2019-04-08 12:35:28 +00:00
|
|
|
search, chats, unread, favorites, discussions, channels, direct, privateGroup, livechat
|
2018-08-31 16:46:33 +00:00
|
|
|
} = this.state;
|
|
|
|
|
|
|
|
if (search.length > 0) {
|
|
|
|
return (
|
|
|
|
<FlatList
|
|
|
|
data={search}
|
|
|
|
extraData={search}
|
2018-09-26 13:56:36 +00:00
|
|
|
keyExtractor={keyExtractor}
|
2018-08-31 16:46:33 +00:00
|
|
|
style={styles.list}
|
|
|
|
renderItem={this.renderItem}
|
|
|
|
getItemLayout={getItemLayout}
|
|
|
|
enableEmptySections
|
|
|
|
removeClippedSubviews
|
|
|
|
keyboardShouldPersistTaps='always'
|
2018-12-21 10:55:35 +00:00
|
|
|
initialNumToRender={12}
|
|
|
|
windowSize={7}
|
2018-08-31 16:46:33 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<View style={styles.container}>
|
|
|
|
{this.renderSection(unread, 'Unread')}
|
|
|
|
{this.renderSection(favorites, 'Favorites')}
|
2019-04-08 12:35:28 +00:00
|
|
|
{this.renderSection(discussions, 'Discussions')}
|
2018-08-31 16:46:33 +00:00
|
|
|
{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-09-25 19:28:42 +00:00
|
|
|
const { loading } = this.state;
|
|
|
|
|
|
|
|
if (loading) {
|
2018-08-01 19:35:06 +00:00
|
|
|
return <ActivityIndicator style={styles.loading} />;
|
|
|
|
}
|
2018-08-31 16:46:33 +00:00
|
|
|
|
2018-12-21 10:55:35 +00:00
|
|
|
const { showUnread, showFavorites, groupByType } = this.props;
|
|
|
|
if (!(showUnread || showFavorites || groupByType)) {
|
|
|
|
const { chats, search } = this.state;
|
|
|
|
return (
|
|
|
|
<FlatList
|
|
|
|
ref={this.getScrollRef}
|
|
|
|
data={search.length ? search : chats}
|
2019-01-29 19:52:56 +00:00
|
|
|
contentOffset={isIOS ? { x: 0, y: SCROLL_OFFSET } : {}}
|
2018-12-21 10:55:35 +00:00
|
|
|
keyExtractor={keyExtractor}
|
|
|
|
style={styles.list}
|
|
|
|
renderItem={this.renderItem}
|
|
|
|
ListHeaderComponent={this.renderListHeader}
|
|
|
|
getItemLayout={getItemLayout}
|
|
|
|
removeClippedSubviews
|
|
|
|
keyboardShouldPersistTaps='always'
|
2019-04-08 12:35:28 +00:00
|
|
|
initialNumToRender={9}
|
|
|
|
windowSize={9}
|
2018-12-21 10:55:35 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-08-01 19:35:06 +00:00
|
|
|
return (
|
2018-08-31 16:46:33 +00:00
|
|
|
<ScrollView
|
2018-09-26 13:56:36 +00:00
|
|
|
ref={this.getScrollRef}
|
2019-01-29 19:52:56 +00:00
|
|
|
contentOffset={isIOS ? { 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
|
|
|
>
|
2018-12-21 10:55:35 +00:00
|
|
|
{this.renderListHeader()}
|
2018-08-31 16:46:33 +00:00
|
|
|
{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 = () => {
|
2019-04-17 18:57:46 +00:00
|
|
|
console.count(`${ this.constructor.name }.render calls`);
|
2018-08-31 16:46:33 +00:00
|
|
|
const {
|
2018-10-23 21:39:48 +00:00
|
|
|
sortBy, groupByType, showFavorites, showUnread, showServerDropdown, showSortDropdown
|
2018-08-31 16:46:33 +00:00
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
return (
|
2018-10-23 21:39:48 +00:00
|
|
|
<SafeAreaView style={styles.container} testID='rooms-list-view' forceInset={{ bottom: 'never' }}>
|
2019-03-12 16:23:06 +00:00
|
|
|
<StatusBar />
|
2018-08-31 16:46:33 +00:00
|
|
|
{this.renderScroll()}
|
2018-09-25 19:28:42 +00:00
|
|
|
{showSortDropdown
|
|
|
|
? (
|
|
|
|
<SortDropdown
|
|
|
|
close={this.toggleSort}
|
|
|
|
sortBy={sortBy}
|
|
|
|
groupByType={groupByType}
|
|
|
|
showFavorites={showFavorites}
|
|
|
|
showUnread={showUnread}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
: null
|
|
|
|
}
|
2019-04-17 18:57:46 +00:00
|
|
|
{showServerDropdown ? <ServerDropdown /> : null}
|
2019-03-12 16:23:06 +00:00
|
|
|
<NavigationEvents
|
|
|
|
onDidFocus={() => BackHandler.addEventListener('hardwareBackPress', this.handleBackPress)}
|
|
|
|
onWillBlur={() => BackHandler.removeEventListener('hardwareBackPress', this.handleBackPress)}
|
|
|
|
/>
|
2018-08-31 16:46:33 +00:00
|
|
|
</SafeAreaView>
|
|
|
|
);
|
|
|
|
}
|
2017-08-03 18:23:43 +00:00
|
|
|
}
|