2018-08-31 16:46:33 +00:00
|
|
|
import React, { Component } 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';
|
|
|
|
|
|
|
|
const ANIMATION_DURATION = 200;
|
|
|
|
|
|
|
|
@connect(state => ({
|
|
|
|
closeSortDropdown: state.rooms.closeSortDropdown
|
|
|
|
}), dispatch => ({
|
2018-09-04 14:29:20 +00:00
|
|
|
setSortPreference: preference => dispatch(setPreference(preference))
|
2018-08-31 16:46:33 +00:00
|
|
|
}))
|
|
|
|
export default class Sort extends Component {
|
|
|
|
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,
|
|
|
|
easing: Easing.ease,
|
|
|
|
useNativeDriver: true
|
|
|
|
},
|
|
|
|
).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) {
|
2018-09-04 14:29:20 +00:00
|
|
|
log('RoomsListView.setSortPreference', e);
|
2018-08-31 16:46:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sortByName = () => {
|
2018-09-04 14:29:20 +00:00
|
|
|
this.setSortPreference({ sortBy: 'alphabetical' });
|
2018-08-31 16:46:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sortByActivity = () => {
|
2018-09-04 14:29:20 +00:00
|
|
|
this.setSortPreference({ sortBy: 'activity' });
|
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,
|
|
|
|
easing: Easing.ease,
|
|
|
|
useNativeDriver: true
|
|
|
|
},
|
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],
|
|
|
|
outputRange: [-245, 41]
|
|
|
|
});
|
|
|
|
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 (
|
|
|
|
[
|
|
|
|
<TouchableWithoutFeedback key='sort-backdrop' onPress={this.close}>
|
|
|
|
<Animated.View style={[styles.backdrop, { opacity: backdropOpacity }]} />
|
|
|
|
</TouchableWithoutFeedback>,
|
|
|
|
<Animated.View
|
|
|
|
key='sort-container'
|
|
|
|
style={[styles.dropdownContainer, { transform: [{ translateY }] }]}
|
|
|
|
>
|
|
|
|
<Touch key='sort-alphabetical' style={styles.sortItemButton} onPress={this.sortByName}>
|
|
|
|
<View style={styles.sortItemContainer}>
|
|
|
|
<Image style={styles.sortIcon} source={{ uri: 'sort_alphabetically' }} />
|
|
|
|
<Text style={styles.sortItemText}>{I18n.t('Alphabetical')}</Text>
|
2018-09-04 14:29:20 +00:00
|
|
|
{sortBy === 'alphabetical' ? <Image style={styles.sortIcon} source={{ uri: '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>
|
2018-09-04 14:29:20 +00:00
|
|
|
{sortBy === 'activity' ? <Image style={styles.sortIcon} source={{ uri: '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}>
|
|
|
|
<Image style={styles.sortIcon} source={{ uri: 'group_type' }} />
|
|
|
|
<Text style={styles.sortItemText}>{I18n.t('Group_by_type')}</Text>
|
2018-09-04 14:29:20 +00:00
|
|
|
{groupByType ? <Image style={styles.sortIcon} source={{ uri: '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}>
|
|
|
|
<Image style={styles.sortIcon} source={{ uri: 'group_favorites' }} />
|
|
|
|
<Text style={styles.sortItemText}>{I18n.t('Group_by_favorites')}</Text>
|
2018-09-04 14:29:20 +00:00
|
|
|
{showFavorites ? <Image style={styles.sortIcon} source={{ uri: '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}>
|
|
|
|
<Image style={styles.sortIcon} source={{ uri: 'group_unread' }} />
|
|
|
|
<Text style={styles.sortItemText}>{I18n.t('Unread_on_top')}</Text>
|
2018-09-04 14:29:20 +00:00
|
|
|
{showUnread ? <Image style={styles.sortIcon} source={{ uri: 'check' }} /> : null}
|
2018-08-31 16:46:33 +00:00
|
|
|
</View>
|
|
|
|
</Touch>
|
|
|
|
</Animated.View>,
|
|
|
|
<Touch
|
|
|
|
key='sort-toggle'
|
|
|
|
onPress={this.close}
|
|
|
|
style={[styles.dropdownContainerHeader, styles.sortToggleContainerClose]}
|
|
|
|
>
|
|
|
|
<View style={styles.sortItemContainer}>
|
2018-09-25 19:28:42 +00:00
|
|
|
<Text style={styles.sortToggleText}>{I18n.t('Sorting_by', { key: I18n.t(sortBy === 'alphabetical' ? 'name' : 'activity') })}</Text>
|
2018-08-31 16:46:33 +00:00
|
|
|
<Image style={styles.sortIcon} source={{ uri: 'group_type' }} />
|
|
|
|
</View>
|
|
|
|
</Touch>
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|