2019-07-18 17:44:02 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import {
|
2019-07-29 16:33:28 +00:00
|
|
|
View, Text, LayoutAnimation, FlatList, ActivityIndicator, Keyboard, BackHandler
|
2019-07-18 17:44:02 +00:00
|
|
|
} from 'react-native';
|
|
|
|
import { SafeAreaView } from 'react-navigation';
|
|
|
|
import ShareExtension from 'rn-extensions-share';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import RNFetchBlob from 'rn-fetch-blob';
|
|
|
|
import * as mime from 'react-native-mime-types';
|
|
|
|
import { isEqual } from 'lodash';
|
|
|
|
|
2019-07-29 16:33:28 +00:00
|
|
|
import Navigation from '../../lib/ShareNavigation';
|
|
|
|
import database from '../../lib/realm';
|
2019-07-18 17:44:02 +00:00
|
|
|
import { isIOS, isAndroid } from '../../utils/deviceInfo';
|
|
|
|
import I18n from '../../i18n';
|
|
|
|
import { CustomIcon } from '../../lib/Icons';
|
|
|
|
import log from '../../utils/log';
|
2019-07-29 18:26:18 +00:00
|
|
|
import { canUploadFile } from '../../utils/media';
|
2019-07-18 17:44:02 +00:00
|
|
|
import DirectoryItem, { ROW_HEIGHT } from '../../presentation/DirectoryItem';
|
2019-07-29 16:33:28 +00:00
|
|
|
import ServerItem from '../../presentation/ServerItem';
|
2019-07-18 17:44:02 +00:00
|
|
|
import { CloseShareExtensionButton, CustomHeaderButtons, Item } from '../../containers/HeaderButton';
|
|
|
|
import ShareListHeader from './Header';
|
|
|
|
|
|
|
|
import styles from './styles';
|
2019-07-29 16:33:28 +00:00
|
|
|
import StatusBar from '../../containers/StatusBar';
|
2019-07-18 17:44:02 +00:00
|
|
|
|
2019-07-29 16:33:28 +00:00
|
|
|
const LIMIT = 50;
|
|
|
|
const getItemLayout = (data, index) => ({ length: ROW_HEIGHT, offset: ROW_HEIGHT * index, index });
|
2019-07-18 17:44:02 +00:00
|
|
|
const keyExtractor = item => item.rid;
|
|
|
|
|
2019-08-07 13:51:34 +00:00
|
|
|
class ShareListView extends React.Component {
|
2019-07-18 17:44:02 +00:00
|
|
|
static navigationOptions = ({ navigation }) => {
|
|
|
|
const searching = navigation.getParam('searching');
|
2019-07-29 16:33:28 +00:00
|
|
|
const initSearch = navigation.getParam('initSearch', () => {});
|
|
|
|
const cancelSearch = navigation.getParam('cancelSearch', () => {});
|
|
|
|
const search = navigation.getParam('search', () => {});
|
|
|
|
|
|
|
|
if (isIOS) {
|
|
|
|
return {
|
|
|
|
headerTitle: (
|
|
|
|
<ShareListHeader
|
|
|
|
searching={searching}
|
|
|
|
initSearch={initSearch}
|
|
|
|
cancelSearch={cancelSearch}
|
|
|
|
search={search}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
};
|
|
|
|
}
|
2019-07-18 17:44:02 +00:00
|
|
|
|
|
|
|
return {
|
2019-07-29 16:33:28 +00:00
|
|
|
headerBackTitle: null,
|
2019-07-18 17:44:02 +00:00
|
|
|
headerLeft: searching
|
|
|
|
? (
|
|
|
|
<CustomHeaderButtons left>
|
2019-07-29 16:33:28 +00:00
|
|
|
<Item title='cancel' iconName='cross' onPress={cancelSearch} />
|
2019-07-18 17:44:02 +00:00
|
|
|
</CustomHeaderButtons>
|
|
|
|
)
|
|
|
|
: (
|
|
|
|
<CloseShareExtensionButton
|
|
|
|
onPress={ShareExtension.close}
|
|
|
|
testID='share-extension-close'
|
|
|
|
/>
|
|
|
|
),
|
2019-07-29 16:33:28 +00:00
|
|
|
headerTitle: <ShareListHeader searching={searching} search={search} />,
|
2019-07-18 17:44:02 +00:00
|
|
|
headerRight: (
|
|
|
|
searching
|
|
|
|
? null
|
|
|
|
: (
|
|
|
|
<CustomHeaderButtons>
|
2019-07-29 16:33:28 +00:00
|
|
|
{isAndroid ? <Item title='search' iconName='magnifier' onPress={initSearch} /> : null}
|
2019-07-18 17:44:02 +00:00
|
|
|
</CustomHeaderButtons>
|
|
|
|
)
|
|
|
|
)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
navigation: PropTypes.object,
|
|
|
|
server: PropTypes.string,
|
|
|
|
baseUrl: PropTypes.string,
|
|
|
|
token: PropTypes.string,
|
2019-07-29 16:33:28 +00:00
|
|
|
userId: PropTypes.string
|
2019-07-18 17:44:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.data = [];
|
|
|
|
this.state = {
|
2019-07-29 16:33:28 +00:00
|
|
|
showError: false,
|
2019-07-18 17:44:02 +00:00
|
|
|
searching: false,
|
2019-07-29 16:33:28 +00:00
|
|
|
searchText: '',
|
2019-07-18 17:44:02 +00:00
|
|
|
value: '',
|
|
|
|
isMedia: false,
|
|
|
|
mediaLoading: false,
|
|
|
|
fileInfo: null,
|
2019-07-29 16:33:28 +00:00
|
|
|
searchResults: [],
|
2019-07-18 17:44:02 +00:00
|
|
|
chats: [],
|
2019-07-29 16:33:28 +00:00
|
|
|
servers: [],
|
|
|
|
loading: true,
|
|
|
|
serverInfo: null
|
2019-07-18 17:44:02 +00:00
|
|
|
};
|
2019-07-29 16:33:28 +00:00
|
|
|
this.didFocusListener = props.navigation.addListener('didFocus', () => BackHandler.addEventListener('hardwareBackPress', this.handleBackPress));
|
|
|
|
this.willBlurListener = props.navigation.addListener('willBlur', () => BackHandler.addEventListener('hardwareBackPress', this.handleBackPress));
|
2019-07-18 17:44:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async componentDidMount() {
|
2019-07-29 16:33:28 +00:00
|
|
|
const { navigation, server } = this.props;
|
2019-07-18 17:44:02 +00:00
|
|
|
navigation.setParams({
|
2019-07-29 16:33:28 +00:00
|
|
|
initSearch: this.initSearch,
|
|
|
|
cancelSearch: this.cancelSearch,
|
|
|
|
search: this.search
|
2019-07-18 17:44:02 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
const { value, type } = await ShareExtension.data();
|
|
|
|
let fileInfo = null;
|
|
|
|
const isMedia = (type === 'media');
|
|
|
|
if (isMedia) {
|
|
|
|
this.setState({ mediaLoading: true });
|
|
|
|
const data = await RNFetchBlob.fs.stat(this.uriToPath(value));
|
|
|
|
fileInfo = {
|
|
|
|
name: data.filename,
|
|
|
|
description: '',
|
|
|
|
size: data.size,
|
2019-07-29 18:26:18 +00:00
|
|
|
mime: mime.lookup(data.path),
|
2019-07-18 17:44:02 +00:00
|
|
|
path: isIOS ? data.path : `file://${ data.path }`
|
|
|
|
};
|
|
|
|
}
|
|
|
|
this.setState({
|
|
|
|
value, fileInfo, isMedia, mediaLoading: false
|
|
|
|
});
|
|
|
|
} catch (e) {
|
2019-08-23 13:18:47 +00:00
|
|
|
log(e);
|
2019-07-18 17:44:02 +00:00
|
|
|
this.setState({ mediaLoading: false });
|
|
|
|
}
|
2019-07-29 16:33:28 +00:00
|
|
|
|
|
|
|
this.getSubscriptions(server);
|
2019-07-18 17:44:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps(nextProps) {
|
2019-07-29 16:33:28 +00:00
|
|
|
const { server } = this.props;
|
|
|
|
if (nextProps.server !== server) {
|
|
|
|
this.getSubscriptions(nextProps.server);
|
2019-07-18 17:44:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
2019-07-29 16:33:28 +00:00
|
|
|
const { searching } = this.state;
|
|
|
|
if (nextState.searching !== searching) {
|
2019-07-18 17:44:02 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-07-29 16:33:28 +00:00
|
|
|
|
|
|
|
const { isMedia } = this.state;
|
|
|
|
if (nextState.isMedia !== isMedia) {
|
|
|
|
this.getSubscriptions(nextProps.server, nextState.fileInfo);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { server } = this.props;
|
|
|
|
if (server !== nextProps.server) {
|
2019-07-18 17:44:02 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-29 16:33:28 +00:00
|
|
|
const { searchResults } = this.state;
|
|
|
|
if (!isEqual(nextState.searchResults, searchResults)) {
|
2019-07-18 17:44:02 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// eslint-disable-next-line react/sort-comp
|
|
|
|
internalSetState = (...args) => {
|
|
|
|
const { navigation } = this.props;
|
|
|
|
if (isIOS && navigation.isFocused()) {
|
|
|
|
LayoutAnimation.easeInEaseOut();
|
|
|
|
}
|
|
|
|
this.setState(...args);
|
|
|
|
}
|
|
|
|
|
2019-07-29 16:33:28 +00:00
|
|
|
getSubscriptions = (server, fileInfo) => {
|
2019-07-29 18:26:18 +00:00
|
|
|
const { fileInfo: fileData } = this.state;
|
2019-07-18 17:44:02 +00:00
|
|
|
const { serversDB } = database.databases;
|
|
|
|
|
|
|
|
if (server) {
|
2019-07-29 16:33:28 +00:00
|
|
|
this.data = database.objects('subscriptions').filtered('archived != true && open == true').sorted('roomUpdatedAt', true);
|
2019-07-18 17:44:02 +00:00
|
|
|
this.servers = serversDB.objects('servers');
|
2019-07-29 16:33:28 +00:00
|
|
|
this.chats = this.data.slice(0, LIMIT);
|
|
|
|
const serverInfo = serversDB.objectForPrimaryKey('servers', server);
|
2019-07-18 17:44:02 +00:00
|
|
|
|
|
|
|
this.internalSetState({
|
|
|
|
chats: this.chats ? this.chats.slice() : [],
|
|
|
|
servers: this.servers ? this.servers.slice() : [],
|
2019-07-29 16:33:28 +00:00
|
|
|
loading: false,
|
2019-07-29 18:26:18 +00:00
|
|
|
showError: !canUploadFile(fileInfo || fileData, serverInfo),
|
2019-07-29 16:33:28 +00:00
|
|
|
serverInfo
|
2019-07-18 17:44:02 +00:00
|
|
|
});
|
|
|
|
this.forceUpdate();
|
2019-07-29 16:33:28 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
uriToPath = uri => decodeURIComponent(isIOS ? uri.replace(/^file:\/\//, '') : uri);
|
2019-07-18 17:44:02 +00:00
|
|
|
|
|
|
|
getRoomTitle = (item) => {
|
2019-07-29 16:33:28 +00:00
|
|
|
const { serverInfo } = this.state;
|
|
|
|
const { useRealName } = serverInfo;
|
2019-07-18 17:44:02 +00:00
|
|
|
return ((item.prid || useRealName) && item.fname) || item.name;
|
|
|
|
}
|
|
|
|
|
|
|
|
shareMessage = (item) => {
|
|
|
|
const { value, isMedia, fileInfo } = this.state;
|
|
|
|
const { navigation } = this.props;
|
|
|
|
|
|
|
|
navigation.navigate('ShareView', {
|
|
|
|
rid: item.rid,
|
|
|
|
value,
|
|
|
|
isMedia,
|
|
|
|
fileInfo,
|
|
|
|
name: this.getRoomTitle(item)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
search = (text) => {
|
|
|
|
const result = database.objects('subscriptions').filtered('name CONTAINS[c] $0', text);
|
|
|
|
this.internalSetState({
|
2019-07-29 16:33:28 +00:00
|
|
|
searchResults: result.slice(0, LIMIT),
|
|
|
|
searchText: text
|
2019-07-18 17:44:02 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-07-29 16:33:28 +00:00
|
|
|
initSearch = () => {
|
|
|
|
const { chats } = this.state;
|
|
|
|
const { navigation } = this.props;
|
|
|
|
this.setState({ searching: true, searchResults: chats });
|
2019-07-18 17:44:02 +00:00
|
|
|
navigation.setParams({ searching: true });
|
|
|
|
}
|
|
|
|
|
2019-07-29 16:33:28 +00:00
|
|
|
cancelSearch = () => {
|
|
|
|
const { navigation } = this.props;
|
|
|
|
this.internalSetState({ searching: false, searchResults: [], searchText: '' });
|
|
|
|
navigation.setParams({ searching: false });
|
|
|
|
Keyboard.dismiss();
|
|
|
|
}
|
|
|
|
|
|
|
|
handleBackPress = () => {
|
|
|
|
const { searching } = this.state;
|
|
|
|
if (searching) {
|
|
|
|
this.cancelSearch();
|
|
|
|
return true;
|
2019-07-18 17:44:02 +00:00
|
|
|
}
|
2019-07-29 16:33:28 +00:00
|
|
|
return false;
|
2019-07-18 17:44:02 +00:00
|
|
|
}
|
|
|
|
|
2019-07-29 16:33:28 +00:00
|
|
|
renderSectionHeader = (header) => {
|
|
|
|
const { searching } = this.state;
|
|
|
|
if (searching) {
|
|
|
|
return null;
|
|
|
|
}
|
2019-07-18 17:44:02 +00:00
|
|
|
|
2019-07-29 16:33:28 +00:00
|
|
|
return (
|
|
|
|
<View style={styles.headerContainer}>
|
|
|
|
<Text style={styles.headerText}>
|
|
|
|
{I18n.t(header)}
|
|
|
|
</Text>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
2019-07-18 17:44:02 +00:00
|
|
|
|
|
|
|
renderItem = ({ item }) => {
|
|
|
|
const { userId, token, baseUrl } = this.props;
|
|
|
|
return (
|
|
|
|
<DirectoryItem
|
|
|
|
user={{
|
|
|
|
userId,
|
|
|
|
token
|
|
|
|
}}
|
|
|
|
title={this.getRoomTitle(item)}
|
|
|
|
baseUrl={baseUrl}
|
|
|
|
avatar={this.getRoomTitle(item)}
|
|
|
|
description={
|
|
|
|
item.t === 'c'
|
|
|
|
? (item.topic || item.description)
|
|
|
|
: item.fname
|
|
|
|
}
|
|
|
|
type={item.t}
|
|
|
|
onPress={() => this.shareMessage(item)}
|
|
|
|
testID={`share-extension-item-${ item.name }`}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderSeparator = () => <View style={styles.separator} />;
|
|
|
|
|
2019-07-29 16:33:28 +00:00
|
|
|
renderBorderBottom = () => <View style={styles.borderBottom} />;
|
2019-07-18 17:44:02 +00:00
|
|
|
|
2019-07-29 16:33:28 +00:00
|
|
|
renderSelectServer = () => {
|
2019-07-18 17:44:02 +00:00
|
|
|
const { servers } = this.state;
|
|
|
|
const { server } = this.props;
|
|
|
|
const currentServer = servers.find(serverFiltered => serverFiltered.id === server);
|
|
|
|
return currentServer ? (
|
|
|
|
<React.Fragment>
|
|
|
|
{this.renderSectionHeader('Select_Server')}
|
|
|
|
<View style={styles.bordered}>
|
|
|
|
<ServerItem
|
|
|
|
server={server}
|
|
|
|
onPress={() => Navigation.navigate('SelectServerView')}
|
|
|
|
item={currentServer}
|
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
</React.Fragment>
|
|
|
|
) : null;
|
|
|
|
}
|
|
|
|
|
2019-07-29 16:33:28 +00:00
|
|
|
renderEmptyComponent = () => (
|
|
|
|
<View style={[styles.container, styles.emptyContainer]}>
|
|
|
|
<Text style={styles.title}>{I18n.t('No_results_found')}</Text>
|
|
|
|
</View>
|
|
|
|
);
|
2019-07-18 17:44:02 +00:00
|
|
|
|
2019-07-29 16:33:28 +00:00
|
|
|
renderHeader = () => {
|
|
|
|
const { searching } = this.state;
|
2019-07-18 17:44:02 +00:00
|
|
|
return (
|
2019-07-29 16:33:28 +00:00
|
|
|
<React.Fragment>
|
|
|
|
{ !searching
|
|
|
|
? (
|
|
|
|
<React.Fragment>
|
|
|
|
{this.renderSelectServer()}
|
|
|
|
{this.renderSectionHeader('Chats')}
|
|
|
|
</React.Fragment>
|
|
|
|
)
|
|
|
|
: null
|
|
|
|
}
|
|
|
|
</React.Fragment>
|
2019-07-18 17:44:02 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-07-29 16:33:28 +00:00
|
|
|
renderContent = () => {
|
|
|
|
const {
|
|
|
|
chats, mediaLoading, loading, searchResults, searching, searchText
|
|
|
|
} = this.state;
|
2019-07-18 17:44:02 +00:00
|
|
|
|
|
|
|
if (mediaLoading || loading) {
|
|
|
|
return <ActivityIndicator style={styles.loading} />;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2019-07-29 16:33:28 +00:00
|
|
|
<FlatList
|
|
|
|
data={searching ? searchResults : chats}
|
|
|
|
keyExtractor={keyExtractor}
|
|
|
|
style={styles.flatlist}
|
|
|
|
renderItem={this.renderItem}
|
|
|
|
getItemLayout={getItemLayout}
|
|
|
|
ItemSeparatorComponent={this.renderSeparator}
|
|
|
|
ListHeaderComponent={this.renderHeader}
|
|
|
|
ListFooterComponent={!searching && this.renderBorderBottom}
|
|
|
|
ListHeaderComponentStyle={!searching ? styles.borderBottom : {}}
|
|
|
|
ListEmptyComponent={searching && searchText ? this.renderEmptyComponent : null}
|
|
|
|
enableEmptySections
|
|
|
|
removeClippedSubviews
|
2019-07-18 17:44:02 +00:00
|
|
|
keyboardShouldPersistTaps='always'
|
2019-07-29 16:33:28 +00:00
|
|
|
initialNumToRender={12}
|
|
|
|
windowSize={20}
|
|
|
|
/>
|
2019-07-18 17:44:02 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderError = () => {
|
2019-07-29 16:33:28 +00:00
|
|
|
const {
|
|
|
|
fileInfo: file, loading, searching, serverInfo
|
|
|
|
} = this.state;
|
|
|
|
const { FileUpload_MaxFileSize } = serverInfo;
|
2019-07-18 17:44:02 +00:00
|
|
|
const errorMessage = (FileUpload_MaxFileSize < file.size)
|
|
|
|
? 'error-file-too-large'
|
|
|
|
: 'error-invalid-file-type';
|
2019-07-29 16:33:28 +00:00
|
|
|
|
|
|
|
if (loading) {
|
|
|
|
return <ActivityIndicator style={styles.loading} />;
|
|
|
|
}
|
|
|
|
|
2019-07-18 17:44:02 +00:00
|
|
|
return (
|
|
|
|
<View style={styles.container}>
|
2019-07-29 16:33:28 +00:00
|
|
|
{ !searching
|
|
|
|
? (
|
|
|
|
<React.Fragment>
|
|
|
|
{this.renderSelectServer()}
|
|
|
|
</React.Fragment>
|
|
|
|
)
|
|
|
|
: null
|
|
|
|
}
|
|
|
|
<View style={[styles.container, styles.centered]}>
|
|
|
|
<Text style={styles.title}>{I18n.t(errorMessage)}</Text>
|
|
|
|
<CustomIcon name='circle-cross' size={120} style={styles.errorIcon} />
|
2019-07-29 18:26:18 +00:00
|
|
|
<Text style={styles.fileMime}>{ file.mime }</Text>
|
2019-07-29 16:33:28 +00:00
|
|
|
</View>
|
2019-07-18 17:44:02 +00:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2019-07-29 16:33:28 +00:00
|
|
|
const { showError } = this.state;
|
2019-07-18 17:44:02 +00:00
|
|
|
return (
|
2019-08-07 13:51:34 +00:00
|
|
|
<SafeAreaView style={styles.container} forceInset={{ vertical: 'never' }}>
|
2019-07-29 16:33:28 +00:00
|
|
|
<StatusBar />
|
|
|
|
{ showError ? this.renderError() : this.renderContent() }
|
2019-07-18 17:44:02 +00:00
|
|
|
</SafeAreaView>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-08-07 13:51:34 +00:00
|
|
|
|
|
|
|
const mapStateToProps = (({ share }) => ({
|
|
|
|
userId: share.user && share.user.id,
|
|
|
|
token: share.user && share.user.token,
|
|
|
|
server: share.server,
|
|
|
|
baseUrl: share ? share.server : ''
|
|
|
|
}));
|
|
|
|
|
|
|
|
export default connect(mapStateToProps)(ShareListView);
|