2018-12-21 10:55:35 +00:00
|
|
|
import React, { PureComponent } from 'react';
|
2018-09-25 19:28:42 +00:00
|
|
|
import {
|
|
|
|
View, Text, Animated, Easing, Image, TouchableWithoutFeedback
|
|
|
|
} from 'react-native';
|
2018-08-31 16:46:33 +00:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
|
|
|
|
import Touch from '../../utils/touch';
|
|
|
|
import styles from './styles';
|
|
|
|
import RocketChat from '../../lib/rocketchat';
|
2018-09-04 14:29:20 +00:00
|
|
|
import { setPreference } from '../../actions/sortPreferences';
|
2018-08-31 16:46:33 +00:00
|
|
|
import log from '../../utils/log';
|
|
|
|
import I18n from '../../i18n';
|
2019-03-01 16:49:11 +00:00
|
|
|
import { CustomIcon } from '../../lib/Icons';
|
2019-06-10 16:22:35 +00:00
|
|
|
import Check from '../../containers/Check';
|
2018-08-31 16:46:33 +00:00
|
|
|
|
|
|
|
const ANIMATION_DURATION = 200;
|
|
|
|
|
2019-08-07 13:51:34 +00:00
|
|
|
class Sort extends PureComponent {
|
2018-08-31 16:46:33 +00:00
|
|
|
static propTypes = {
|
|
|
|
closeSortDropdown: PropTypes.bool,
|
|
|
|
close: PropTypes.func,
|
2018-09-04 14:29:20 +00:00
|
|
|
sortBy: PropTypes.string,
|
|
|
|
groupByType: PropTypes.bool,
|
|
|
|
showFavorites: PropTypes.bool,
|
|
|
|
showUnread: PropTypes.bool,
|
|
|
|
setSortPreference: PropTypes.func
|
2018-08-31 16:46:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.animatedValue = new Animated.Value(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
Animated.timing(
|
|
|
|
this.animatedValue,
|
|
|
|
{
|
|
|
|
toValue: 1,
|
|
|
|
duration: ANIMATION_DURATION,
|
2018-12-21 10:55:35 +00:00
|
|
|
easing: Easing.inOut(Easing.quad),
|
2018-08-31 16:46:33 +00:00
|
|
|
useNativeDriver: true
|
2019-10-31 16:21:59 +00:00
|
|
|
}
|
2018-08-31 16:46:33 +00:00
|
|
|
).start();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate(prevProps) {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { closeSortDropdown } = this.props;
|
|
|
|
if (prevProps.closeSortDropdown !== closeSortDropdown) {
|
2018-08-31 16:46:33 +00:00
|
|
|
this.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-23 21:39:48 +00:00
|
|
|
setSortPreference = (param) => {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { setSortPreference } = this.props;
|
|
|
|
|
2018-08-31 16:46:33 +00:00
|
|
|
try {
|
2018-09-25 19:28:42 +00:00
|
|
|
setSortPreference(param);
|
2018-09-04 14:29:20 +00:00
|
|
|
RocketChat.saveSortPreference(param);
|
2018-08-31 16:46:33 +00:00
|
|
|
} catch (e) {
|
2019-08-23 13:18:47 +00:00
|
|
|
log(e);
|
2018-08-31 16:46:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sortByName = () => {
|
2018-09-04 14:29:20 +00:00
|
|
|
this.setSortPreference({ sortBy: 'alphabetical' });
|
2019-09-25 20:43:17 +00:00
|
|
|
this.close();
|
2018-08-31 16:46:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sortByActivity = () => {
|
2018-09-04 14:29:20 +00:00
|
|
|
this.setSortPreference({ sortBy: 'activity' });
|
2019-09-25 20:43:17 +00:00
|
|
|
this.close();
|
2018-08-31 16:46:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
toggleGroupByType = () => {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { groupByType } = this.props;
|
|
|
|
this.setSortPreference({ groupByType: !groupByType });
|
2018-08-31 16:46:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
toggleGroupByFavorites = () => {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { showFavorites } = this.props;
|
|
|
|
this.setSortPreference({ showFavorites: !showFavorites });
|
2018-08-31 16:46:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
toggleUnread = () => {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { showUnread } = this.props;
|
|
|
|
this.setSortPreference({ showUnread: !showUnread });
|
2018-08-31 16:46:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
close = () => {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { close } = this.props;
|
2018-08-31 16:46:33 +00:00
|
|
|
Animated.timing(
|
|
|
|
this.animatedValue,
|
|
|
|
{
|
|
|
|
toValue: 0,
|
|
|
|
duration: ANIMATION_DURATION,
|
2018-12-21 10:55:35 +00:00
|
|
|
easing: Easing.inOut(Easing.quad),
|
2018-08-31 16:46:33 +00:00
|
|
|
useNativeDriver: true
|
2019-10-31 16:21:59 +00:00
|
|
|
}
|
2018-09-25 19:28:42 +00:00
|
|
|
).start(() => close());
|
2018-08-31 16:46:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const translateY = this.animatedValue.interpolate({
|
|
|
|
inputRange: [0, 1],
|
2019-06-10 16:22:35 +00:00
|
|
|
outputRange: [-326, 0]
|
2018-08-31 16:46:33 +00:00
|
|
|
});
|
|
|
|
const backdropOpacity = this.animatedValue.interpolate({
|
|
|
|
inputRange: [0, 1],
|
|
|
|
outputRange: [0, 0.3]
|
|
|
|
});
|
|
|
|
const {
|
2018-09-04 14:29:20 +00:00
|
|
|
sortBy, groupByType, showFavorites, showUnread
|
2018-08-31 16:46:33 +00:00
|
|
|
} = this.props;
|
2018-09-25 19:28:42 +00:00
|
|
|
|
2018-08-31 16:46:33 +00:00
|
|
|
return (
|
2019-09-24 20:26:56 +00:00
|
|
|
<>
|
2018-08-31 16:46:33 +00:00
|
|
|
<TouchableWithoutFeedback key='sort-backdrop' onPress={this.close}>
|
|
|
|
<Animated.View style={[styles.backdrop, { opacity: backdropOpacity }]} />
|
2019-06-10 16:22:35 +00:00
|
|
|
</TouchableWithoutFeedback>
|
2018-08-31 16:46:33 +00:00
|
|
|
<Animated.View
|
|
|
|
key='sort-container'
|
|
|
|
style={[styles.dropdownContainer, { transform: [{ translateY }] }]}
|
|
|
|
>
|
2019-06-10 16:22:35 +00:00
|
|
|
<Touch
|
|
|
|
key='sort-toggle'
|
|
|
|
onPress={this.close}
|
|
|
|
style={styles.dropdownContainerHeader}
|
|
|
|
>
|
|
|
|
<View style={styles.sortItemContainer}>
|
|
|
|
<Text style={styles.sortToggleText}>{I18n.t('Sorting_by', { key: I18n.t(sortBy === 'alphabetical' ? 'name' : 'activity') })}</Text>
|
|
|
|
<CustomIcon style={styles.sortIcon} size={22} name='sort1' />
|
|
|
|
</View>
|
|
|
|
</Touch>
|
2018-08-31 16:46:33 +00:00
|
|
|
<Touch key='sort-alphabetical' style={styles.sortItemButton} onPress={this.sortByName}>
|
|
|
|
<View style={styles.sortItemContainer}>
|
2019-03-01 16:49:11 +00:00
|
|
|
<CustomIcon style={styles.sortIcon} size={22} name='sort' />
|
2018-08-31 16:46:33 +00:00
|
|
|
<Text style={styles.sortItemText}>{I18n.t('Alphabetical')}</Text>
|
2019-03-01 16:49:11 +00:00
|
|
|
{sortBy === 'alphabetical' ? <Check /> : null}
|
2018-08-31 16:46:33 +00:00
|
|
|
</View>
|
|
|
|
</Touch>
|
|
|
|
<Touch key='sort-activity' style={styles.sortItemButton} onPress={this.sortByActivity}>
|
|
|
|
<View style={styles.sortItemContainer}>
|
|
|
|
<Image style={styles.sortIcon} source={{ uri: 'sort_activity' }} />
|
|
|
|
<Text style={styles.sortItemText}>{I18n.t('Activity')}</Text>
|
2019-03-01 16:49:11 +00:00
|
|
|
{sortBy === 'activity' ? <Check /> : null}
|
2018-08-31 16:46:33 +00:00
|
|
|
</View>
|
|
|
|
</Touch>
|
|
|
|
<View style={styles.sortSeparator} />
|
|
|
|
<Touch key='group-type' style={styles.sortItemButton} onPress={this.toggleGroupByType}>
|
|
|
|
<View style={styles.sortItemContainer}>
|
2019-03-01 16:49:11 +00:00
|
|
|
<CustomIcon style={styles.sortIcon} size={22} name='sort1' />
|
2018-08-31 16:46:33 +00:00
|
|
|
<Text style={styles.sortItemText}>{I18n.t('Group_by_type')}</Text>
|
2019-03-01 16:49:11 +00:00
|
|
|
{groupByType ? <Check /> : null}
|
2018-08-31 16:46:33 +00:00
|
|
|
</View>
|
|
|
|
</Touch>
|
|
|
|
<Touch key='group-favorites' style={styles.sortItemButton} onPress={this.toggleGroupByFavorites}>
|
|
|
|
<View style={styles.sortItemContainer}>
|
2019-03-01 16:49:11 +00:00
|
|
|
<CustomIcon style={styles.sortIcon} size={22} name='star' />
|
2018-08-31 16:46:33 +00:00
|
|
|
<Text style={styles.sortItemText}>{I18n.t('Group_by_favorites')}</Text>
|
2019-03-01 16:49:11 +00:00
|
|
|
{showFavorites ? <Check /> : null}
|
2018-08-31 16:46:33 +00:00
|
|
|
</View>
|
|
|
|
</Touch>
|
|
|
|
<Touch key='group-unread' style={styles.sortItemButton} onPress={this.toggleUnread}>
|
|
|
|
<View style={styles.sortItemContainer}>
|
2019-03-01 16:49:11 +00:00
|
|
|
<CustomIcon style={styles.sortIcon} size={22} name='eye-off' />
|
2018-08-31 16:46:33 +00:00
|
|
|
<Text style={styles.sortItemText}>{I18n.t('Unread_on_top')}</Text>
|
2019-03-01 16:49:11 +00:00
|
|
|
{showUnread ? <Check /> : null}
|
2018-08-31 16:46:33 +00:00
|
|
|
</View>
|
|
|
|
</Touch>
|
2019-06-10 16:22:35 +00:00
|
|
|
</Animated.View>
|
2019-09-24 20:26:56 +00:00
|
|
|
</>
|
2018-08-31 16:46:33 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-08-07 13:51:34 +00:00
|
|
|
|
|
|
|
const mapStateToProps = state => ({
|
|
|
|
closeSortDropdown: state.rooms.closeSortDropdown
|
|
|
|
});
|
|
|
|
|
|
|
|
const mapDispatchToProps = dispatch => ({
|
|
|
|
setSortPreference: preference => dispatch(setPreference(preference))
|
|
|
|
});
|
|
|
|
|
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(Sort);
|