2019-06-28 17:02:30 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import {
|
|
|
|
FlatList, Switch, View, StyleSheet
|
|
|
|
} from 'react-native';
|
2019-10-02 12:18:08 +00:00
|
|
|
import { SafeAreaView, ScrollView } from 'react-navigation';
|
2019-06-28 17:02:30 +00:00
|
|
|
|
|
|
|
import RocketChat from '../../lib/rocketchat';
|
|
|
|
import I18n from '../../i18n';
|
|
|
|
import StatusBar from '../../containers/StatusBar';
|
|
|
|
import { CustomIcon } from '../../lib/Icons';
|
|
|
|
import sharedStyles from '../Styles';
|
|
|
|
import ListItem from '../../containers/ListItem';
|
|
|
|
import Separator from '../../containers/Separator';
|
2019-12-04 16:39:53 +00:00
|
|
|
import { SWITCH_TRACK_COLOR, themes } from '../../constants/colors';
|
2019-06-28 17:02:30 +00:00
|
|
|
import scrollPersistTaps from '../../utils/scrollPersistTaps';
|
2019-12-04 16:39:53 +00:00
|
|
|
import { withTheme } from '../../theme';
|
|
|
|
import { themedHeader } from '../../utils/navigation';
|
2019-06-28 17:02:30 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
contentContainerStyle: {
|
|
|
|
borderTopWidth: StyleSheet.hairlineWidth,
|
|
|
|
marginTop: 10,
|
|
|
|
paddingBottom: 30
|
|
|
|
},
|
2019-12-04 16:39:53 +00:00
|
|
|
flatListContainerStyle: {
|
|
|
|
borderBottomWidth: StyleSheet.hairlineWidth
|
|
|
|
},
|
2019-06-28 17:02:30 +00:00
|
|
|
sectionSeparator: {
|
|
|
|
...sharedStyles.separatorVertical,
|
|
|
|
height: 10
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
const SectionSeparator = React.memo(({ theme }) => (
|
|
|
|
<View
|
|
|
|
style={[
|
|
|
|
styles.sectionSeparator,
|
|
|
|
{
|
|
|
|
backgroundColor: themes[theme].auxiliaryBackground,
|
|
|
|
borderColor: themes[theme].separatorColor
|
|
|
|
}
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
));
|
|
|
|
|
|
|
|
SectionSeparator.propTypes = {
|
|
|
|
theme: PropTypes.string
|
|
|
|
};
|
|
|
|
|
|
|
|
class AutoTranslateView extends React.Component {
|
|
|
|
static navigationOptions = ({ screenProps }) => ({
|
|
|
|
title: I18n.t('Auto_Translate'),
|
|
|
|
...themedHeader(screenProps.theme)
|
2019-06-28 17:02:30 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
static propTypes = {
|
2019-12-04 16:39:53 +00:00
|
|
|
navigation: PropTypes.object,
|
|
|
|
theme: PropTypes.string
|
2019-06-28 17:02:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.rid = props.navigation.getParam('rid');
|
2019-09-16 20:26:32 +00:00
|
|
|
const room = props.navigation.getParam('room');
|
|
|
|
|
|
|
|
if (room && room.observe) {
|
|
|
|
this.roomObservable = room.observe();
|
|
|
|
this.subscription = this.roomObservable
|
|
|
|
.subscribe((changes) => {
|
|
|
|
this.room = changes;
|
|
|
|
});
|
|
|
|
}
|
2019-06-28 17:02:30 +00:00
|
|
|
this.state = {
|
|
|
|
languages: [],
|
2019-09-16 20:26:32 +00:00
|
|
|
selectedLanguage: room.autoTranslateLanguage,
|
|
|
|
enableAutoTranslate: room.autoTranslate
|
2019-06-28 17:02:30 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async componentDidMount() {
|
|
|
|
try {
|
|
|
|
const languages = await RocketChat.getSupportedLanguagesAutoTranslate();
|
|
|
|
this.setState({ languages });
|
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
componentWillUnmount() {
|
|
|
|
if (this.subscription && this.subscription.unsubscribe) {
|
|
|
|
this.subscription.unsubscribe();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-28 17:02:30 +00:00
|
|
|
toggleAutoTranslate = async() => {
|
|
|
|
const { enableAutoTranslate } = this.state;
|
|
|
|
try {
|
|
|
|
await RocketChat.saveAutoTranslate({
|
|
|
|
rid: this.rid,
|
|
|
|
field: 'autoTranslate',
|
|
|
|
value: enableAutoTranslate ? '0' : '1',
|
|
|
|
options: { defaultLanguage: 'en' }
|
|
|
|
});
|
|
|
|
this.setState({ enableAutoTranslate: !enableAutoTranslate });
|
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
saveAutoTranslateLanguage = async(language) => {
|
|
|
|
try {
|
|
|
|
await RocketChat.saveAutoTranslate({
|
|
|
|
rid: this.rid,
|
|
|
|
field: 'autoTranslateLanguage',
|
|
|
|
value: language
|
|
|
|
});
|
|
|
|
this.setState({ selectedLanguage: language });
|
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
renderSeparator = () => {
|
|
|
|
const { theme } = this.props;
|
|
|
|
return <Separator theme={theme} />;
|
|
|
|
}
|
2019-06-28 17:02:30 +00:00
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
renderIcon = () => {
|
|
|
|
const { theme } = this.props;
|
|
|
|
return <CustomIcon name='check' size={20} style={{ color: themes[theme].tintColor }} />;
|
|
|
|
}
|
2019-06-28 17:02:30 +00:00
|
|
|
|
|
|
|
renderSwitch = () => {
|
|
|
|
const { enableAutoTranslate } = this.state;
|
|
|
|
return (
|
|
|
|
<Switch
|
|
|
|
value={enableAutoTranslate}
|
|
|
|
trackColor={SWITCH_TRACK_COLOR}
|
|
|
|
onValueChange={this.toggleAutoTranslate}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderItem = ({ item }) => {
|
|
|
|
const { selectedLanguage } = this.state;
|
2019-12-04 16:39:53 +00:00
|
|
|
const { theme } = this.props;
|
2019-06-28 17:02:30 +00:00
|
|
|
const { language, name } = item;
|
|
|
|
const isSelected = selectedLanguage === language;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ListItem
|
|
|
|
title={name || language}
|
|
|
|
onPress={() => this.saveAutoTranslateLanguage(language)}
|
|
|
|
testID={`auto-translate-view-${ language }`}
|
|
|
|
right={isSelected ? this.renderIcon : null}
|
2019-12-04 16:39:53 +00:00
|
|
|
theme={theme}
|
2019-06-28 17:02:30 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { languages } = this.state;
|
2019-12-04 16:39:53 +00:00
|
|
|
const { theme } = this.props;
|
2019-06-28 17:02:30 +00:00
|
|
|
return (
|
2019-12-04 16:39:53 +00:00
|
|
|
<SafeAreaView
|
|
|
|
style={[sharedStyles.container, { backgroundColor: themes[theme].auxiliaryBackground }]}
|
|
|
|
forceInset={{ vertical: 'never' }}
|
|
|
|
testID='auto-translate-view'
|
|
|
|
>
|
|
|
|
<StatusBar theme={theme} />
|
2019-06-28 17:02:30 +00:00
|
|
|
<ScrollView
|
|
|
|
{...scrollPersistTaps}
|
2019-12-04 16:39:53 +00:00
|
|
|
contentContainerStyle={[
|
|
|
|
styles.contentContainerStyle,
|
|
|
|
{
|
|
|
|
backgroundColor: themes[theme].auxiliaryBackground,
|
|
|
|
borderColor: themes[theme].separatorColor
|
|
|
|
}
|
|
|
|
]}
|
2019-06-28 17:02:30 +00:00
|
|
|
testID='auto-translate-view-list'
|
|
|
|
>
|
|
|
|
<ListItem
|
|
|
|
title={I18n.t('Enable_Auto_Translate')}
|
|
|
|
testID='auto-translate-view-switch'
|
|
|
|
right={() => this.renderSwitch()}
|
2019-12-04 16:39:53 +00:00
|
|
|
theme={theme}
|
2019-06-28 17:02:30 +00:00
|
|
|
/>
|
2019-12-04 16:39:53 +00:00
|
|
|
<SectionSeparator theme={theme} />
|
2019-06-28 17:02:30 +00:00
|
|
|
<FlatList
|
|
|
|
data={languages}
|
|
|
|
extraData={this.state}
|
|
|
|
keyExtractor={item => item.language}
|
|
|
|
renderItem={this.renderItem}
|
|
|
|
ItemSeparatorComponent={this.renderSeparator}
|
2019-12-04 16:39:53 +00:00
|
|
|
contentContainerStyle={[styles.flatListContainerStyle, { borderColor: themes[theme].separatorColor }]}
|
2019-06-28 17:02:30 +00:00
|
|
|
/>
|
|
|
|
</ScrollView>
|
|
|
|
</SafeAreaView>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-12-04 16:39:53 +00:00
|
|
|
|
|
|
|
export default withTheme(AutoTranslateView);
|