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 {
|
2018-10-23 21:39:48 +00:00
|
|
|
Platform, View, FlatList, BackHandler, ActivityIndicator, Text, Image, Dimensions, ScrollView, Keyboard, LayoutAnimation
|
2018-09-25 19:28:42 +00:00
|
|
|
} from 'react-native';
|
2018-09-26 13:56:36 +00:00
|
|
|
import { connect, Provider } from 'react-redux';
|
2018-08-31 16:46:33 +00:00
|
|
|
import { isEqual } from 'lodash';
|
2018-09-26 13:56:36 +00:00
|
|
|
import { Navigation } from 'react-native-navigation';
|
2018-10-23 21:39:48 +00:00
|
|
|
import SafeAreaView from 'react-native-safe-area-view';
|
2018-11-14 21:42:03 +00:00
|
|
|
import { gestureHandlerRootHOC } from 'react-native-gesture-handler';
|
2018-05-18 17:55:08 +00:00
|
|
|
|
2018-08-31 18:13:30 +00:00
|
|
|
import SearchBox from '../../containers/SearchBox';
|
2018-10-02 12:33:21 +00:00
|
|
|
import ConnectionBadge from '../../containers/ConnectionBadge';
|
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';
|
2018-10-23 21:39:48 +00:00
|
|
|
import { toggleSortDropdown as toggleSortDropdownAction, openSearchHeader as openSearchHeaderAction, closeSearchHeader as closeSearchHeaderAction } from '../../actions/rooms';
|
2018-09-26 13:56:36 +00:00
|
|
|
import store from '../../lib/createStore';
|
2018-10-23 21:39:48 +00:00
|
|
|
import Drawer from '../../Drawer';
|
2018-11-14 21:42:03 +00:00
|
|
|
import { DEFAULT_HEADER } from '../../constants/headerOptions';
|
2018-08-31 16:46:33 +00:00
|
|
|
|
|
|
|
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 });
|
2018-09-26 13:56:36 +00:00
|
|
|
const keyExtractor = item => item.rid;
|
|
|
|
|
2018-08-31 16:46:33 +00:00
|
|
|
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-26 13:56:36 +00:00
|
|
|
let NewMessageView = null;
|
|
|
|
|
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-10-23 21:39:48 +00:00
|
|
|
toggleSortDropdown: () => dispatch(toggleSortDropdownAction()),
|
|
|
|
openSearchHeader: () => dispatch(openSearchHeaderAction()),
|
|
|
|
closeSearchHeader: () => dispatch(closeSearchHeaderAction())
|
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-10-23 21:39:48 +00:00
|
|
|
static options() {
|
|
|
|
return {
|
2018-11-14 21:42:03 +00:00
|
|
|
...DEFAULT_HEADER,
|
2018-10-23 21:39:48 +00:00
|
|
|
topBar: {
|
2018-11-14 21:42:03 +00:00
|
|
|
...DEFAULT_HEADER.topBar,
|
2018-10-23 21:39:48 +00:00
|
|
|
leftButtons,
|
|
|
|
rightButtons,
|
|
|
|
title: {
|
|
|
|
component: {
|
|
|
|
name: 'RoomsListHeaderView',
|
|
|
|
alignment: isAndroid() ? 'left' : 'center'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
sideMenu: {
|
|
|
|
left: {
|
|
|
|
enabled: true
|
2018-10-29 13:54:23 +00:00
|
|
|
},
|
|
|
|
right: {
|
|
|
|
enabled: true
|
2018-10-23 21:39:48 +00:00
|
|
|
}
|
2018-11-05 19:02:54 +00:00
|
|
|
},
|
|
|
|
blurOnUnmount: true
|
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 = {
|
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,
|
2018-10-23 21:39:48 +00:00
|
|
|
toggleSortDropdown: PropTypes.func,
|
|
|
|
openSearchHeader: PropTypes.func,
|
|
|
|
closeSearchHeader: 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);
|
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-10-23 21:39:48 +00:00
|
|
|
Navigation.events().bindComponent(this);
|
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) {
|
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) {
|
|
|
|
return !(isEqual(this.props, nextProps) && isEqual(this.state, nextState));
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate(prevProps) {
|
2018-09-25 19:28:42 +00:00
|
|
|
const {
|
|
|
|
sortBy, groupByType, showFavorites, showUnread
|
|
|
|
} = 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
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2018-10-23 21:39:48 +00:00
|
|
|
navigationButtonPressed = ({ buttonId }) => {
|
|
|
|
if (buttonId === 'newMessage') {
|
|
|
|
if (NewMessageView == null) {
|
|
|
|
NewMessageView = require('../NewMessageView').default;
|
2018-11-14 21:42:03 +00:00
|
|
|
Navigation.registerComponentWithRedux('NewMessageView', () => gestureHandlerRootHOC(NewMessageView), Provider, store);
|
2018-07-10 13:40:32 +00:00
|
|
|
}
|
2018-10-23 21:39:48 +00:00
|
|
|
|
|
|
|
Navigation.showModal({
|
|
|
|
stack: {
|
|
|
|
children: [{
|
|
|
|
component: {
|
|
|
|
name: 'NewMessageView',
|
|
|
|
passProps: {
|
|
|
|
onPressItem: this._onPressItem
|
|
|
|
},
|
|
|
|
options: {
|
|
|
|
topBar: {
|
|
|
|
title: {
|
|
|
|
text: I18n.t('New_Message')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}]
|
|
|
|
}
|
2018-07-10 13:40:32 +00:00
|
|
|
});
|
2018-10-23 21:39:48 +00:00
|
|
|
} else if (buttonId === 'settings') {
|
|
|
|
Drawer.toggle();
|
|
|
|
} else if (buttonId === 'search') {
|
|
|
|
this.initSearchingAndroid();
|
|
|
|
} else if (buttonId === 'back') {
|
|
|
|
this.cancelSearchingAndroid();
|
2018-07-10 13:40:32 +00:00
|
|
|
}
|
2017-08-12 20:52:55 +00:00
|
|
|
}
|
|
|
|
|
2018-10-23 21:39:48 +00:00
|
|
|
internalSetState = (...args) => {
|
2018-10-29 13:54:23 +00:00
|
|
|
if (Platform.OS === 'ios') {
|
|
|
|
LayoutAnimation.easeInEaseOut();
|
|
|
|
}
|
2018-10-23 21:39:48 +00:00
|
|
|
this.setState(...args);
|
|
|
|
}
|
|
|
|
|
2018-07-18 20:34:59 +00:00
|
|
|
getSubscriptions = () => {
|
2018-09-25 19:28:42 +00:00
|
|
|
const {
|
|
|
|
server, sortBy, showUnread, showFavorites, groupByType
|
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
if (server && this.hasActiveDB()) {
|
|
|
|
if (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-25 19:28:42 +00:00
|
|
|
if (showUnread) {
|
2018-10-16 20:28:04 +00:00
|
|
|
this.unread = this.data.filtered('archived != true && open == true').filtered('(unread > 0 || alert == true)');
|
2018-10-18 17:56:49 +00:00
|
|
|
unread = this.removeRealmInstance(this.unread);
|
2018-08-31 16:46:33 +00:00
|
|
|
setTimeout(() => {
|
2018-10-23 21:39:48 +00:00
|
|
|
this.unread.addListener(() => this.internalSetState({ unread: this.removeRealmInstance(this.unread) }));
|
2018-08-31 16:46:33 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.removeListener(unread);
|
|
|
|
}
|
|
|
|
// 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');
|
2018-10-18 17:56:49 +00:00
|
|
|
favorites = this.removeRealmInstance(this.favorites);
|
2018-08-31 16:46:33 +00:00
|
|
|
setTimeout(() => {
|
2018-10-23 21:39:48 +00:00
|
|
|
this.favorites.addListener(() => this.internalSetState({ favorites: this.removeRealmInstance(this.favorites) }));
|
2018-08-31 16:46:33 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.removeListener(favorites);
|
|
|
|
}
|
|
|
|
// type
|
2018-09-25 19:28:42 +00:00
|
|
|
if (groupByType) {
|
2018-08-31 16:46:33 +00:00
|
|
|
// channels
|
|
|
|
this.channels = this.data.filtered('t == $0', 'c');
|
2018-10-18 17:56:49 +00:00
|
|
|
channels = this.removeRealmInstance(this.channels);
|
|
|
|
|
2018-08-31 16:46:33 +00:00
|
|
|
// private
|
|
|
|
this.privateGroup = this.data.filtered('t == $0', 'p');
|
2018-10-18 17:56:49 +00:00
|
|
|
privateGroup = this.removeRealmInstance(this.privateGroup);
|
|
|
|
|
2018-08-31 16:46:33 +00:00
|
|
|
// direct
|
|
|
|
this.direct = this.data.filtered('t == $0', 'd');
|
2018-10-18 17:56:49 +00:00
|
|
|
direct = this.removeRealmInstance(this.direct);
|
|
|
|
|
2018-08-31 16:46:33 +00:00
|
|
|
// livechat
|
|
|
|
this.livechat = this.data.filtered('t == $0', 'l');
|
2018-10-18 17:56:49 +00:00
|
|
|
livechat = this.removeRealmInstance(this.livechat);
|
|
|
|
|
2018-08-31 16:46:33 +00:00
|
|
|
setTimeout(() => {
|
2018-10-23 21:39:48 +00:00
|
|
|
this.channels.addListener(() => this.internalSetState({ channels: this.removeRealmInstance(this.channels) }));
|
|
|
|
this.privateGroup.addListener(() => this.internalSetState({ privateGroup: this.removeRealmInstance(this.privateGroup) }));
|
|
|
|
this.direct.addListener(() => this.internalSetState({ direct: this.removeRealmInstance(this.direct) }));
|
|
|
|
this.livechat.addListener(() => this.internalSetState({ livechat: this.removeRealmInstance(this.livechat) }));
|
2018-08-31 16:46:33 +00:00
|
|
|
});
|
|
|
|
this.removeListener(this.chats);
|
|
|
|
} else {
|
|
|
|
// chats
|
2018-09-25 19:28:42 +00:00
|
|
|
if (showUnread) {
|
2018-09-19 17:08:41 +00:00
|
|
|
this.chats = this.data.filtered('(unread == 0 && alert == false)');
|
|
|
|
} else {
|
|
|
|
this.chats = this.data;
|
|
|
|
}
|
2018-10-18 17:56:49 +00:00
|
|
|
chats = this.removeRealmInstance(this.chats);
|
|
|
|
|
2018-08-31 16:46:33 +00:00
|
|
|
setTimeout(() => {
|
2018-10-18 17:56:49 +00:00
|
|
|
this.chats.addListener(() => {
|
2018-10-23 21:39:48 +00:00
|
|
|
this.internalSetState({ chats: this.removeRealmInstance(this.chats) });
|
2018-10-18 17:56:49 +00:00
|
|
|
});
|
2018-08-31 16:46:33 +00:00
|
|
|
});
|
|
|
|
this.removeListener(this.channels);
|
|
|
|
this.removeListener(this.privateGroup);
|
|
|
|
this.removeListener(this.direct);
|
|
|
|
this.removeListener(this.livechat);
|
|
|
|
}
|
|
|
|
|
|
|
|
// setState
|
2018-10-23 21:39:48 +00:00
|
|
|
this.internalSetState({
|
2018-08-31 16:46:33 +00:00
|
|
|
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(() => {
|
2018-10-23 21:39:48 +00:00
|
|
|
this.internalSetState({ loading: false });
|
2018-08-01 19:35:06 +00:00
|
|
|
}, 200);
|
2018-07-18 20:34:59 +00:00
|
|
|
}
|
|
|
|
|
2018-10-18 17:56:49 +00:00
|
|
|
removeRealmInstance = (data) => {
|
|
|
|
const array = Array.from(data);
|
|
|
|
return JSON.parse(JSON.stringify(array));
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
initSearchingAndroid = () => {
|
2018-10-23 21:39:48 +00:00
|
|
|
const { openSearchHeader } = this.props;
|
|
|
|
openSearchHeader();
|
|
|
|
Navigation.mergeOptions('RoomsListView', {
|
|
|
|
topBar: {
|
|
|
|
leftButtons: [{
|
|
|
|
id: 'back',
|
|
|
|
icon: { uri: 'back', scale: Dimensions.get('window').scale },
|
|
|
|
testID: 'rooms-list-view-cancel-search'
|
|
|
|
}],
|
|
|
|
rightButtons: []
|
|
|
|
}
|
2018-07-10 13:40:32 +00:00
|
|
|
});
|
|
|
|
BackHandler.addEventListener('hardwareBackPress', this.handleBackPress);
|
|
|
|
}
|
|
|
|
|
|
|
|
cancelSearchingAndroid = () => {
|
|
|
|
if (Platform.OS === 'android') {
|
2018-10-23 21:39:48 +00:00
|
|
|
const { closeSearchHeader } = this.props;
|
|
|
|
closeSearchHeader();
|
|
|
|
Navigation.mergeOptions('RoomsListView', {
|
|
|
|
topBar: {
|
|
|
|
leftButtons,
|
|
|
|
rightButtons
|
|
|
|
}
|
|
|
|
});
|
|
|
|
this.internalSetState({ search: [] });
|
2018-08-31 16:46:33 +00:00
|
|
|
Keyboard.dismiss();
|
2018-07-10 13:40:32 +00:00
|
|
|
BackHandler.removeEventListener('hardwareBackPress', this.handleBackPress);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 = () => {
|
|
|
|
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 });
|
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
|
|
|
|
2018-10-31 18:40:08 +00:00
|
|
|
goRoom = (rid) => {
|
2018-11-05 19:02:54 +00:00
|
|
|
this.cancelSearchingAndroid();
|
2018-10-23 21:39:48 +00:00
|
|
|
Navigation.push('RoomsListView', {
|
|
|
|
component: {
|
|
|
|
name: 'RoomView',
|
|
|
|
passProps: {
|
|
|
|
rid
|
|
|
|
}
|
|
|
|
}
|
2018-05-24 20:17:00 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-02 21:31:44 +00:00
|
|
|
_onPressItem = async(item = {}) => {
|
|
|
|
if (!item.search) {
|
2018-10-31 18:40:08 +00:00
|
|
|
const { rid } = item;
|
|
|
|
return this.goRoom(rid);
|
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;
|
2018-10-31 18:40:08 +00:00
|
|
|
return this.goRoom(rid);
|
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 {
|
2018-10-31 18:40:08 +00:00
|
|
|
const { rid } = item;
|
|
|
|
return this.goRoom(rid);
|
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;
|
|
|
|
|
2018-09-05 18:15:03 +00:00
|
|
|
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(() => {
|
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
|
|
|
|
|
2018-08-31 16:46:33 +00:00
|
|
|
renderHeader = () => {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { search } = this.state;
|
|
|
|
if (search.length > 0) {
|
2018-08-31 16:46:33 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return this.renderSort();
|
|
|
|
}
|
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
renderSort = () => {
|
|
|
|
const { sortBy } = this.props;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Touch
|
|
|
|
onPress={this.toggleSort}
|
|
|
|
style={styles.dropdownContainerHeader}
|
|
|
|
>
|
|
|
|
<View style={styles.sortItemContainer}>
|
|
|
|
<Text style={styles.sortToggleText}>{I18n.t('Sorting_by', { key: I18n.t(sortBy === 'alphabetical' ? 'name' : 'activity') })}</Text>
|
|
|
|
<Image style={styles.sortIcon} source={{ uri: 'group_type' }} />
|
|
|
|
</View>
|
|
|
|
</Touch>
|
|
|
|
);
|
|
|
|
}
|
2018-08-31 16:46:33 +00:00
|
|
|
|
|
|
|
renderSearchBar = () => {
|
|
|
|
if (Platform.OS === 'ios') {
|
2018-09-28 20:17:49 +00:00
|
|
|
return <SearchBox onChangeText={this.search} 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-09-25 19:28:42 +00:00
|
|
|
const { useRealName, userId, baseUrl } = this.props;
|
|
|
|
const id = item.rid.replace(userId, '').trim();
|
|
|
|
|
|
|
|
return (
|
|
|
|
<RoomItem
|
|
|
|
alert={item.alert}
|
|
|
|
unread={item.unread}
|
|
|
|
userMentions={item.userMentions}
|
|
|
|
favorite={item.f}
|
|
|
|
lastMessage={item.lastMessage}
|
|
|
|
name={(useRealName && item.fname) || item.name}
|
|
|
|
_updatedAt={item.roomUpdatedAt}
|
|
|
|
key={item._id}
|
|
|
|
id={id}
|
|
|
|
type={item.t}
|
|
|
|
baseUrl={baseUrl}
|
|
|
|
onPress={() => this._onPressItem(item)}
|
|
|
|
testID={`rooms-list-view-item-${ item.name }`}
|
|
|
|
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-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;
|
2018-09-25 19:28:42 +00:00
|
|
|
} else if (['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;
|
|
|
|
}
|
2018-08-31 16:46:33 +00:00
|
|
|
if (data.length > 0) {
|
|
|
|
return (
|
|
|
|
<FlatList
|
|
|
|
data={data}
|
|
|
|
extraData={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}
|
|
|
|
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}
|
2018-09-26 13:56:36 +00:00
|
|
|
keyExtractor={keyExtractor}
|
2018-08-31 16:46:33 +00:00
|
|
|
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-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-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}
|
2018-09-05 18:15:03 +00:00
|
|
|
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-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' }}>
|
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
|
|
|
|
}
|
|
|
|
{showServerDropdown ? <ServerDropdown navigator={navigator} /> : null}
|
2018-10-02 12:33:21 +00:00
|
|
|
<ConnectionBadge />
|
2018-08-31 16:46:33 +00:00
|
|
|
</SafeAreaView>
|
|
|
|
);
|
|
|
|
}
|
2017-08-03 18:23:43 +00:00
|
|
|
}
|