2023-06-29 22:01:34 +00:00
|
|
|
import React, { useEffect, useLayoutEffect, useRef, useState } from 'react';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { FlatList, StyleSheet, Switch } from 'react-native';
|
2023-06-29 22:01:34 +00:00
|
|
|
import { Subscription } from 'rxjs';
|
|
|
|
import { RouteProp, useNavigation, useRoute } from '@react-navigation/native';
|
2019-06-28 17:02:30 +00:00
|
|
|
|
2020-10-30 13:59:44 +00:00
|
|
|
import * as List from '../../containers/List';
|
2020-06-15 14:00:46 +00:00
|
|
|
import SafeAreaView from '../../containers/SafeAreaView';
|
2023-06-29 22:01:34 +00:00
|
|
|
import StatusBar from '../../containers/StatusBar';
|
|
|
|
import { ISubscription } from '../../definitions';
|
|
|
|
import I18n from '../../i18n';
|
|
|
|
import { SWITCH_TRACK_COLOR } from '../../lib/constants';
|
2022-06-06 14:17:51 +00:00
|
|
|
import { events, logEvent } from '../../lib/methods/helpers/log';
|
2022-04-28 20:37:25 +00:00
|
|
|
import { Services } from '../../lib/services';
|
2023-06-29 22:01:34 +00:00
|
|
|
import { ChatsStackParamList } from '../../stacks/types';
|
|
|
|
import { useTheme } from '../../theme';
|
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
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-06-29 22:01:34 +00:00
|
|
|
const AutoTranslateView = (): React.ReactElement => {
|
|
|
|
const navigation = useNavigation();
|
|
|
|
const {
|
|
|
|
params: { rid, room }
|
|
|
|
} = useRoute<RouteProp<ChatsStackParamList, 'AutoTranslateView'>>();
|
|
|
|
const { colors } = useTheme();
|
|
|
|
|
|
|
|
const [languages, setLanguages] = useState<{ language: string; name: string }[]>([]);
|
|
|
|
const [selectedLanguage, setSelectedLanguage] = useState<string | undefined>(room?.autoTranslateLanguage);
|
|
|
|
const [enableAutoTranslate, setEnableAutoTranslate] = useState<boolean | undefined>(room?.autoTranslate);
|
|
|
|
const subscription = useRef<Subscription | null>(null);
|
|
|
|
|
|
|
|
useLayoutEffect(() => {
|
|
|
|
navigation.setOptions({
|
|
|
|
title: I18n.t('Auto_Translate')
|
|
|
|
});
|
|
|
|
}, [navigation]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
(async () => {
|
|
|
|
try {
|
|
|
|
const languages = await Services.getSupportedLanguagesAutoTranslate();
|
|
|
|
setLanguages(languages);
|
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
let letSelectedLanguage = selectedLanguage;
|
|
|
|
let letAutoTranslate = enableAutoTranslate;
|
|
|
|
subscription.current = room.observe().subscribe((changes: ISubscription) => {
|
|
|
|
if (letSelectedLanguage !== changes.autoTranslateLanguage) {
|
|
|
|
setSelectedLanguage(changes.autoTranslateLanguage);
|
|
|
|
letSelectedLanguage = changes.autoTranslateLanguage;
|
|
|
|
}
|
|
|
|
if (letAutoTranslate !== changes.autoTranslate) {
|
|
|
|
setEnableAutoTranslate(changes.autoTranslate);
|
|
|
|
letAutoTranslate = changes.autoTranslate;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return () => subscription.current?.unsubscribe && subscription.current.unsubscribe();
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const toggleAutoTranslate = async () => {
|
2020-08-05 13:15:56 +00:00
|
|
|
logEvent(events.AT_TOGGLE_TRANSLATE);
|
2019-06-28 17:02:30 +00:00
|
|
|
try {
|
2023-06-29 22:01:34 +00:00
|
|
|
setEnableAutoTranslate(!enableAutoTranslate);
|
2022-04-28 20:37:25 +00:00
|
|
|
await Services.saveAutoTranslate({
|
2023-06-29 22:01:34 +00:00
|
|
|
rid,
|
2019-06-28 17:02:30 +00:00
|
|
|
field: 'autoTranslate',
|
|
|
|
value: enableAutoTranslate ? '0' : '1',
|
|
|
|
options: { defaultLanguage: 'en' }
|
|
|
|
});
|
|
|
|
} catch (error) {
|
2023-06-29 22:01:34 +00:00
|
|
|
setEnableAutoTranslate(!enableAutoTranslate);
|
2020-08-05 13:15:56 +00:00
|
|
|
logEvent(events.AT_TOGGLE_TRANSLATE_F);
|
2019-06-28 17:02:30 +00:00
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2019-06-28 17:02:30 +00:00
|
|
|
|
2023-06-29 22:01:34 +00:00
|
|
|
const saveAutoTranslateLanguage = async (selectedLanguage: 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({
|
2023-06-29 22:01:34 +00:00
|
|
|
rid,
|
2019-06-28 17:02:30 +00:00
|
|
|
field: 'autoTranslateLanguage',
|
2023-06-29 22:01:34 +00:00
|
|
|
value: selectedLanguage
|
2019-06-28 17:02:30 +00:00
|
|
|
});
|
2023-06-29 22:01:34 +00:00
|
|
|
setSelectedLanguage(selectedLanguage);
|
2019-06-28 17:02:30 +00:00
|
|
|
} catch (error) {
|
2020-08-05 13:15:56 +00:00
|
|
|
logEvent(events.AT_SET_LANG_F);
|
2019-06-28 17:02:30 +00:00
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2019-06-28 17:02:30 +00:00
|
|
|
|
2023-06-29 22:01:34 +00:00
|
|
|
const LanguageItem = React.memo(({ language, name }: { language: string; name?: string }) => (
|
|
|
|
<List.Item
|
|
|
|
title={name || language}
|
|
|
|
onPress={() => saveAutoTranslateLanguage(language)}
|
|
|
|
testID={`auto-translate-view-${language}`}
|
2023-08-14 20:22:46 +00:00
|
|
|
right={() =>
|
|
|
|
selectedLanguage === language ? (
|
|
|
|
<List.Icon testID={`auto-translate-view-${language}-check`} name='check' color={colors.tintColor} />
|
|
|
|
) : null
|
|
|
|
}
|
2023-06-29 22:01:34 +00:00
|
|
|
translateTitle={false}
|
|
|
|
/>
|
|
|
|
));
|
|
|
|
|
|
|
|
return (
|
2023-08-14 20:22:46 +00:00
|
|
|
<SafeAreaView>
|
2023-06-29 22:01:34 +00:00
|
|
|
<StatusBar />
|
|
|
|
<FlatList
|
2023-08-14 20:22:46 +00:00
|
|
|
testID='auto-translate-view'
|
2023-06-29 22:01:34 +00:00
|
|
|
data={languages}
|
|
|
|
keyExtractor={item => item.name || item.language}
|
|
|
|
renderItem={({ item: { language, name } }) => <LanguageItem language={language} name={name} />}
|
|
|
|
ListHeaderComponent={
|
|
|
|
<>
|
2020-10-30 13:59:44 +00:00
|
|
|
<List.Separator />
|
2023-06-29 22:01:34 +00:00
|
|
|
<List.Item
|
|
|
|
title='Enable_Auto_Translate'
|
|
|
|
right={() => (
|
2023-08-14 20:22:46 +00:00
|
|
|
<Switch
|
|
|
|
testID='auto-translate-view-switch'
|
|
|
|
value={enableAutoTranslate}
|
|
|
|
trackColor={SWITCH_TRACK_COLOR}
|
|
|
|
onValueChange={toggleAutoTranslate}
|
|
|
|
/>
|
2023-06-29 22:01:34 +00:00
|
|
|
)}
|
|
|
|
/>
|
2020-10-30 13:59:44 +00:00
|
|
|
<List.Separator />
|
2023-06-29 22:01:34 +00:00
|
|
|
</>
|
|
|
|
}
|
|
|
|
ItemSeparatorComponent={List.Separator}
|
|
|
|
ListFooterComponent={List.Separator}
|
|
|
|
contentContainerStyle={[List.styles.contentContainerStyleFlatList, styles.list]}
|
|
|
|
/>
|
|
|
|
</SafeAreaView>
|
|
|
|
);
|
|
|
|
};
|
2019-12-04 16:39:53 +00:00
|
|
|
|
2023-06-29 22:01:34 +00:00
|
|
|
export default AutoTranslateView;
|