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';
|
2022-05-02 17:59:49 +00:00
|
|
|
import { Observable, Subscription } from 'rxjs';
|
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 I18n from '../../i18n';
|
|
|
|
import StatusBar from '../../containers/StatusBar';
|
2020-10-30 13:59:44 +00:00
|
|
|
import * as List from '../../containers/List';
|
2022-04-07 14:10:03 +00:00
|
|
|
import { SWITCH_TRACK_COLOR, themes } from '../../lib/constants';
|
2022-05-02 17:59:49 +00:00
|
|
|
import { withTheme } from '../../theme';
|
2020-06-15 14:00:46 +00:00
|
|
|
import SafeAreaView from '../../containers/SafeAreaView';
|
2022-06-06 14:17:51 +00:00
|
|
|
import { events, logEvent } from '../../lib/methods/helpers/log';
|
2022-05-02 17:59:49 +00:00
|
|
|
import { IBaseScreen, ISubscription } from '../../definitions';
|
2022-04-28 20:37:25 +00:00
|
|
|
import { Services } from '../../lib/services';
|
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
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-05-02 17:59:49 +00:00
|
|
|
type TAutoTranslateViewProps = IBaseScreen<ChatsStackParamList, 'AutoTranslateView'>;
|
2021-09-15 14:38:51 +00:00
|
|
|
|
2022-05-02 17:59:49 +00:00
|
|
|
class AutoTranslateView extends React.Component<TAutoTranslateViewProps, 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;
|
2022-02-15 18:50:55 +00:00
|
|
|
private rid: string;
|
2022-05-02 17:59:49 +00:00
|
|
|
private roomObservable?: Observable<ISubscription>;
|
|
|
|
private subscription?: Subscription;
|
2019-06-28 17:02:30 +00:00
|
|
|
|
2022-05-02 17:59:49 +00:00
|
|
|
constructor(props: TAutoTranslateViewProps) {
|
2019-06-28 17:02:30 +00:00
|
|
|
super(props);
|
2020-06-17 19:22:22 +00:00
|
|
|
this.mounted = false;
|
2022-02-15 18:50:55 +00:00
|
|
|
this.rid = props.route.params?.rid ?? '';
|
2020-06-15 14:00:46 +00:00
|
|
|
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 {
|
2022-04-28 20:37:25 +00:00
|
|
|
const languages = await Services.getSupportedLanguagesAutoTranslate();
|
2019-06-28 17:02:30 +00:00
|
|
|
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 {
|
2022-04-28 20:37:25 +00:00
|
|
|
await Services.saveAutoTranslate({
|
2019-06-28 17:02:30 +00:00
|
|
|
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 {
|
2022-04-28 20:37:25 +00:00
|
|
|
await Services.saveAutoTranslate({
|
2019-06-28 17:02:30 +00:00
|
|
|
rid: this.rid,
|
|
|
|
field: 'autoTranslateLanguage',
|
2022-03-16 00:54:43 +00:00
|
|
|
value: language
|
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;
|
2022-05-02 17:59:49 +00:00
|
|
|
return <List.Icon name='check' 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}`}
|
2022-01-17 16:10:39 +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);
|