2019-06-28 17:02:30 +00:00
|
|
|
import React from 'react';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { FlatList, StyleSheet, Switch } from 'react-native';
|
2021-12-03 19:27:57 +00:00
|
|
|
import { RouteProp } from '@react-navigation/core';
|
2019-06-28 17:02:30 +00:00
|
|
|
|
2021-12-03 19:27:57 +00:00
|
|
|
import { ChatsStackParamList } from '../../stacks/types';
|
2019-06-28 17:02:30 +00:00
|
|
|
import RocketChat from '../../lib/rocketchat';
|
|
|
|
import I18n from '../../i18n';
|
|
|
|
import StatusBar from '../../containers/StatusBar';
|
2020-10-30 13:59:44 +00:00
|
|
|
import * as List from '../../containers/List';
|
2019-12-04 16:39:53 +00:00
|
|
|
import { SWITCH_TRACK_COLOR, themes } from '../../constants/colors';
|
|
|
|
import { withTheme } from '../../theme';
|
2020-06-15 14:00:46 +00:00
|
|
|
import SafeAreaView from '../../containers/SafeAreaView';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { events, logEvent } from '../../utils/log';
|
2022-01-11 13:51:48 +00:00
|
|
|
import { ISubscription } from '../../definitions/ISubscription';
|
2019-06-28 17:02:30 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
2020-10-30 13:59:44 +00:00
|
|
|
list: {
|
|
|
|
paddingTop: 16
|
2019-06-28 17:02:30 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-09-15 14:38:51 +00:00
|
|
|
interface IAutoTranslateViewProps {
|
2021-12-03 19:27:57 +00:00
|
|
|
route: RouteProp<ChatsStackParamList, 'AutoTranslateView'>;
|
2021-09-15 14:38:51 +00:00
|
|
|
theme: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
class AutoTranslateView extends React.Component<IAutoTranslateViewProps, any> {
|
2020-07-31 18:30:36 +00:00
|
|
|
static navigationOptions = () => ({
|
2020-06-15 14:00:46 +00:00
|
|
|
title: I18n.t('Auto_Translate')
|
2021-09-13 20:41:05 +00:00
|
|
|
});
|
2019-06-28 17:02:30 +00:00
|
|
|
|
2021-09-15 14:38:51 +00:00
|
|
|
private mounted: boolean;
|
|
|
|
private rid: string | undefined;
|
|
|
|
private roomObservable: any;
|
|
|
|
private subscription: any;
|
2019-06-28 17:02:30 +00:00
|
|
|
|
2021-09-15 14:38:51 +00:00
|
|
|
constructor(props: IAutoTranslateViewProps) {
|
2019-06-28 17:02:30 +00:00
|
|
|
super(props);
|
2020-06-17 19:22:22 +00:00
|
|
|
this.mounted = false;
|
2020-06-15 14:00:46 +00:00
|
|
|
this.rid = props.route.params?.rid;
|
|
|
|
const room = props.route.params?.room;
|
2019-09-16 20:26:32 +00:00
|
|
|
|
|
|
|
if (room && room.observe) {
|
|
|
|
this.roomObservable = room.observe();
|
2022-01-11 13:51:48 +00:00
|
|
|
this.subscription = this.roomObservable.subscribe((changes: ISubscription) => {
|
2021-09-13 20:41:05 +00:00
|
|
|
if (this.mounted) {
|
|
|
|
const { selectedLanguage, enableAutoTranslate } = this.state;
|
|
|
|
if (selectedLanguage !== changes.autoTranslateLanguage) {
|
|
|
|
this.setState({ selectedLanguage: changes.autoTranslateLanguage });
|
|
|
|
}
|
|
|
|
if (enableAutoTranslate !== changes.autoTranslate) {
|
|
|
|
this.setState({ enableAutoTranslate: changes.autoTranslate });
|
2020-06-17 19:22:22 +00:00
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
}
|
|
|
|
});
|
2019-09-16 20:26:32 +00:00
|
|
|
}
|
2019-06-28 17:02:30 +00:00
|
|
|
this.state = {
|
|
|
|
languages: [],
|
2021-09-15 14:38:51 +00:00
|
|
|
selectedLanguage: room?.autoTranslateLanguage,
|
|
|
|
enableAutoTranslate: room?.autoTranslate
|
2019-06-28 17:02:30 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async componentDidMount() {
|
2020-06-17 19:22:22 +00:00
|
|
|
this.mounted = true;
|
2019-06-28 17:02:30 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
toggleAutoTranslate = async () => {
|
2020-08-05 13:15:56 +00:00
|
|
|
logEvent(events.AT_TOGGLE_TRANSLATE);
|
2019-06-28 17:02:30 +00:00
|
|
|
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) {
|
2020-08-05 13:15:56 +00:00
|
|
|
logEvent(events.AT_TOGGLE_TRANSLATE_F);
|
2019-06-28 17:02:30 +00:00
|
|
|
console.log(error);
|
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2019-06-28 17:02:30 +00:00
|
|
|
|
2021-09-15 14:38:51 +00:00
|
|
|
saveAutoTranslateLanguage = async (language: string) => {
|
2020-08-05 13:15:56 +00:00
|
|
|
logEvent(events.AT_SET_LANG);
|
2019-06-28 17:02:30 +00:00
|
|
|
try {
|
2021-09-15 14:38:51 +00:00
|
|
|
// TODO: remove the parameter options, after migrate the RocketChat
|
2019-06-28 17:02:30 +00:00
|
|
|
await RocketChat.saveAutoTranslate({
|
|
|
|
rid: this.rid,
|
|
|
|
field: 'autoTranslateLanguage',
|
2021-09-15 14:38:51 +00:00
|
|
|
value: language,
|
|
|
|
options: null
|
2019-06-28 17:02:30 +00:00
|
|
|
});
|
|
|
|
this.setState({ selectedLanguage: language });
|
|
|
|
} catch (error) {
|
2020-08-05 13:15:56 +00:00
|
|
|
logEvent(events.AT_SET_LANG_F);
|
2019-06-28 17:02:30 +00:00
|
|
|
console.log(error);
|
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2019-06-28 17:02:30 +00:00
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
renderIcon = () => {
|
|
|
|
const { theme } = this.props;
|
2020-10-30 13:59:44 +00:00
|
|
|
return <List.Icon name='check' style={{ color: themes[theme].tintColor }} />;
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2019-06-28 17:02:30 +00:00
|
|
|
|
|
|
|
renderSwitch = () => {
|
|
|
|
const { enableAutoTranslate } = this.state;
|
2021-09-13 20:41:05 +00:00
|
|
|
return <Switch value={enableAutoTranslate} trackColor={SWITCH_TRACK_COLOR} onValueChange={this.toggleAutoTranslate} />;
|
|
|
|
};
|
2019-06-28 17:02:30 +00:00
|
|
|
|
2021-09-15 14:38:51 +00:00
|
|
|
renderItem = ({ item }: { item: { language: string; name: string } }) => {
|
2019-06-28 17:02:30 +00:00
|
|
|
const { selectedLanguage } = this.state;
|
|
|
|
const { language, name } = item;
|
|
|
|
const isSelected = selectedLanguage === language;
|
|
|
|
|
|
|
|
return (
|
2020-10-30 13:59:44 +00:00
|
|
|
<List.Item
|
2019-06-28 17:02:30 +00:00
|
|
|
title={name || language}
|
|
|
|
onPress={() => this.saveAutoTranslateLanguage(language)}
|
2021-09-13 20:41:05 +00:00
|
|
|
testID={`auto-translate-view-${language}`}
|
2019-06-28 17:02:30 +00:00
|
|
|
right={isSelected ? this.renderIcon : null}
|
2020-10-30 13:59:44 +00:00
|
|
|
translateTitle={false}
|
2019-06-28 17:02:30 +00:00
|
|
|
/>
|
|
|
|
);
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2019-06-28 17:02:30 +00:00
|
|
|
|
|
|
|
render() {
|
|
|
|
const { languages } = this.state;
|
|
|
|
return (
|
2020-10-30 13:59:44 +00:00
|
|
|
<SafeAreaView testID='auto-translate-view'>
|
|
|
|
<StatusBar />
|
|
|
|
<List.Container testID='auto-translate-view-list'>
|
|
|
|
<List.Section>
|
|
|
|
<List.Separator />
|
2021-09-13 20:41:05 +00:00
|
|
|
<List.Item title='Enable_Auto_Translate' testID='auto-translate-view-switch' right={() => this.renderSwitch()} />
|
2020-10-30 13:59:44 +00:00
|
|
|
<List.Separator />
|
|
|
|
</List.Section>
|
2019-06-28 17:02:30 +00:00
|
|
|
<FlatList
|
|
|
|
data={languages}
|
|
|
|
extraData={this.state}
|
|
|
|
keyExtractor={item => item.language}
|
|
|
|
renderItem={this.renderItem}
|
2020-10-30 13:59:44 +00:00
|
|
|
ItemSeparatorComponent={List.Separator}
|
|
|
|
ListFooterComponent={List.Separator}
|
|
|
|
ListHeaderComponent={List.Separator}
|
|
|
|
contentContainerStyle={[List.styles.contentContainerStyleFlatList, styles.list]}
|
2019-06-28 17:02:30 +00:00
|
|
|
/>
|
2020-10-30 13:59:44 +00:00
|
|
|
</List.Container>
|
2019-06-28 17:02:30 +00:00
|
|
|
</SafeAreaView>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-12-04 16:39:53 +00:00
|
|
|
|
|
|
|
export default withTheme(AutoTranslateView);
|