2022-09-22 21:40:32 +00:00
|
|
|
import React, { useLayoutEffect } from 'react';
|
2019-06-11 14:01:40 +00:00
|
|
|
import { FlatList } from 'react-native';
|
2020-11-30 21:47:05 +00:00
|
|
|
import RNRestart from 'react-native-restart';
|
2022-09-22 21:40:32 +00:00
|
|
|
import { useDispatch } from 'react-redux';
|
|
|
|
import { useNavigation } from '@react-navigation/native';
|
|
|
|
import { StackNavigationProp } from '@react-navigation/stack';
|
2019-06-11 14:01:40 +00:00
|
|
|
|
2022-09-22 21:40:32 +00:00
|
|
|
import { useAppSelector } from '../../lib/hooks';
|
2022-02-02 18:27:10 +00:00
|
|
|
import { appStart } from '../../actions/app';
|
|
|
|
import { setUser } from '../../actions/login';
|
|
|
|
import * as List from '../../containers/List';
|
|
|
|
import SafeAreaView from '../../containers/SafeAreaView';
|
|
|
|
import StatusBar from '../../containers/StatusBar';
|
2022-09-22 21:40:32 +00:00
|
|
|
import { RootEnum } from '../../definitions';
|
2022-02-02 18:27:10 +00:00
|
|
|
import I18n, { isRTL, LANGUAGES } from '../../i18n';
|
|
|
|
import database from '../../lib/database';
|
|
|
|
import { getUserSelector } from '../../selectors/login';
|
|
|
|
import { SettingsStackParamList } from '../../stacks/types';
|
2022-06-06 14:17:51 +00:00
|
|
|
import { showErrorAlert } from '../../lib/methods/helpers/info';
|
|
|
|
import log, { events, logEvent } from '../../lib/methods/helpers/log';
|
2022-04-28 20:37:25 +00:00
|
|
|
import { Services } from '../../lib/services';
|
2022-09-22 21:40:32 +00:00
|
|
|
import LanguageItem from './LanguageItem';
|
|
|
|
|
|
|
|
const LanguageView = () => {
|
|
|
|
const { languageDefault, id } = useAppSelector(state => ({
|
|
|
|
languageDefault: getUserSelector(state).language,
|
|
|
|
id: getUserSelector(state).id
|
|
|
|
}));
|
|
|
|
const language = languageDefault || 'en';
|
|
|
|
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
const navigation = useNavigation<StackNavigationProp<SettingsStackParamList, 'LanguageView'>>();
|
|
|
|
|
|
|
|
useLayoutEffect(() => {
|
|
|
|
navigation.setOptions({
|
|
|
|
title: I18n.t('Change_Language')
|
|
|
|
});
|
|
|
|
}, [navigation]);
|
|
|
|
|
|
|
|
const submit = async (language: string) => {
|
|
|
|
if (languageDefault === language) {
|
2019-06-11 14:01:40 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-22 21:40:32 +00:00
|
|
|
const shouldRestart = isRTL(language) || isRTL(languageDefault);
|
2019-06-11 14:01:40 +00:00
|
|
|
|
2022-02-02 18:27:10 +00:00
|
|
|
dispatch(appStart({ root: RootEnum.ROOT_LOADING, text: I18n.t('Change_language_loading') }));
|
2020-02-28 20:11:08 +00:00
|
|
|
|
|
|
|
// shows loading for at least 300ms
|
2022-09-22 21:40:32 +00:00
|
|
|
await Promise.all([changeLanguage(language), new Promise(resolve => setTimeout(resolve, 300))]);
|
2020-02-28 20:11:08 +00:00
|
|
|
|
2020-11-30 21:47:05 +00:00
|
|
|
if (shouldRestart) {
|
|
|
|
await RNRestart.Restart();
|
|
|
|
} else {
|
2022-02-02 18:27:10 +00:00
|
|
|
dispatch(appStart({ root: RootEnum.ROOT_INSIDE }));
|
2020-11-30 21:47:05 +00:00
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-02-28 20:11:08 +00:00
|
|
|
|
2022-09-22 21:40:32 +00:00
|
|
|
const changeLanguage = async (language: string) => {
|
2020-07-30 19:51:13 +00:00
|
|
|
logEvent(events.LANG_SET_LANGUAGE);
|
2019-06-11 14:01:40 +00:00
|
|
|
|
2021-11-10 14:35:01 +00:00
|
|
|
const params: { language?: string } = {};
|
2019-06-11 14:01:40 +00:00
|
|
|
|
|
|
|
// language
|
2022-09-22 21:40:32 +00:00
|
|
|
if (languageDefault !== language) {
|
2019-06-11 14:01:40 +00:00
|
|
|
params.language = language;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2022-04-28 20:37:25 +00:00
|
|
|
await Services.saveUserPreferences(params);
|
2022-02-02 18:27:10 +00:00
|
|
|
dispatch(setUser({ language: params.language }));
|
2019-06-11 14:01:40 +00:00
|
|
|
|
2020-02-11 20:28:40 +00:00
|
|
|
const serversDB = database.servers;
|
2021-02-26 16:25:51 +00:00
|
|
|
const usersCollection = serversDB.get('users');
|
2021-11-10 14:35:01 +00:00
|
|
|
await serversDB.write(async () => {
|
2020-02-11 20:28:40 +00:00
|
|
|
try {
|
2022-09-22 21:40:32 +00:00
|
|
|
const userRecord = await usersCollection.find(id);
|
2022-04-07 16:27:45 +00:00
|
|
|
await userRecord.update(record => {
|
2020-02-11 20:28:40 +00:00
|
|
|
record.language = params.language;
|
|
|
|
});
|
|
|
|
} catch (e) {
|
2020-07-30 19:51:13 +00:00
|
|
|
logEvent(events.LANG_SET_LANGUAGE_F);
|
2020-02-11 20:28:40 +00:00
|
|
|
}
|
|
|
|
});
|
2019-06-11 14:01:40 +00:00
|
|
|
} catch (e) {
|
2020-07-30 19:51:13 +00:00
|
|
|
logEvent(events.LANG_SET_LANGUAGE_F);
|
2020-02-11 20:28:40 +00:00
|
|
|
showErrorAlert(I18n.t('There_was_an_error_while_action', { action: I18n.t('saving_preferences') }));
|
|
|
|
log(e);
|
2019-06-11 14:01:40 +00:00
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2019-06-11 14:01:40 +00:00
|
|
|
|
2022-09-22 21:40:32 +00:00
|
|
|
return (
|
|
|
|
<SafeAreaView testID='language-view'>
|
|
|
|
<StatusBar />
|
|
|
|
<FlatList
|
|
|
|
data={LANGUAGES}
|
|
|
|
keyExtractor={item => item.value}
|
|
|
|
ListHeaderComponent={List.Separator}
|
|
|
|
ListFooterComponent={List.Separator}
|
|
|
|
contentContainerStyle={List.styles.contentContainerStyleFlatList}
|
|
|
|
renderItem={({ item }) => <LanguageItem item={item} language={language} submit={submit} />}
|
|
|
|
ItemSeparatorComponent={List.Separator}
|
2019-06-11 14:01:40 +00:00
|
|
|
/>
|
2022-09-22 21:40:32 +00:00
|
|
|
</SafeAreaView>
|
|
|
|
);
|
|
|
|
};
|
2019-06-11 14:01:40 +00:00
|
|
|
|
2022-09-22 21:40:32 +00:00
|
|
|
export default LanguageView;
|