2022-12-07 23:17:08 +00:00
|
|
|
import React, { useEffect, useLayoutEffect, useRef, useState } from 'react';
|
2022-12-07 03:11:25 +00:00
|
|
|
import { ScrollView, View } from 'react-native';
|
2022-12-07 05:01:00 +00:00
|
|
|
import { RouteProp, useNavigation, useRoute } from '@react-navigation/native';
|
|
|
|
import { StackNavigationProp } from '@react-navigation/stack';
|
2022-12-09 01:27:30 +00:00
|
|
|
import ImagePicker, { Image } from 'react-native-image-crop-picker';
|
2022-12-07 03:11:25 +00:00
|
|
|
|
2022-12-13 17:18:31 +00:00
|
|
|
import { compareServerVersion } from '../../lib/methods/helpers';
|
2022-12-07 03:11:25 +00:00
|
|
|
import KeyboardView from '../../containers/KeyboardView';
|
|
|
|
import sharedStyles from '../Styles';
|
|
|
|
import scrollPersistTaps from '../../lib/methods/helpers/scrollPersistTaps';
|
2022-12-07 23:17:08 +00:00
|
|
|
import { showConfirmationAlert, handleError } from '../../lib/methods/helpers/info';
|
2022-12-07 03:11:25 +00:00
|
|
|
import StatusBar from '../../containers/StatusBar';
|
|
|
|
import { useTheme } from '../../theme';
|
|
|
|
import SafeAreaView from '../../containers/SafeAreaView';
|
|
|
|
import * as List from '../../containers/List';
|
|
|
|
import styles from './styles';
|
|
|
|
import { useAppSelector } from '../../lib/hooks';
|
|
|
|
import { getUserSelector } from '../../selectors/login';
|
|
|
|
import Avatar from '../../containers/Avatar';
|
|
|
|
import AvatarUrl from './AvatarUrl';
|
|
|
|
import Button from '../../containers/Button';
|
|
|
|
import I18n from '../../i18n';
|
2022-12-07 05:01:00 +00:00
|
|
|
import { ChatsStackParamList } from '../../stacks/types';
|
|
|
|
import { IAvatar } from '../../definitions';
|
|
|
|
import { Services } from '../../lib/services';
|
|
|
|
import AvatarSuggestion from './AvatarSuggestion';
|
2022-12-07 23:17:08 +00:00
|
|
|
import log from '../../lib/methods/helpers/log';
|
2022-12-07 03:11:25 +00:00
|
|
|
|
|
|
|
const ChangeAvatarView = () => {
|
2022-12-20 16:40:53 +00:00
|
|
|
const [avatar, setAvatarState] = useState<IAvatar | null>(null);
|
2022-12-13 17:18:31 +00:00
|
|
|
|
2022-12-07 23:17:08 +00:00
|
|
|
const [textAvatar, setTextAvatar] = useState('');
|
|
|
|
const [saving, setSaving] = useState(false);
|
2022-12-07 03:11:25 +00:00
|
|
|
const { colors } = useTheme();
|
2022-12-13 17:18:31 +00:00
|
|
|
const { user, serverVersion } = useAppSelector(state => ({
|
2022-12-07 23:17:08 +00:00
|
|
|
user: getUserSelector(state),
|
2022-12-13 17:18:31 +00:00
|
|
|
isMasterDetail: state.app.isMasterDetail,
|
|
|
|
serverVersion: state.server.version
|
2022-12-07 23:17:08 +00:00
|
|
|
}));
|
|
|
|
|
|
|
|
const avatarUrl = useRef<string | undefined>('');
|
2022-12-07 03:11:25 +00:00
|
|
|
|
2022-12-07 05:01:00 +00:00
|
|
|
const navigation = useNavigation<StackNavigationProp<ChatsStackParamList, 'ChangeAvatarView'>>();
|
2022-12-13 15:54:47 +00:00
|
|
|
const { fromUser, titleHeader, room, t } = useRoute<RouteProp<ChatsStackParamList, 'ChangeAvatarView'>>().params;
|
2022-12-07 05:01:00 +00:00
|
|
|
|
|
|
|
useLayoutEffect(() => {
|
2022-12-07 23:17:08 +00:00
|
|
|
navigation.setOptions({
|
|
|
|
title: titleHeader || I18n.t('Avatar')
|
|
|
|
});
|
2022-12-07 05:01:00 +00:00
|
|
|
}, [titleHeader, navigation]);
|
|
|
|
|
2022-12-07 23:17:08 +00:00
|
|
|
useEffect(() => {
|
|
|
|
navigation.addListener('beforeRemove', e => {
|
|
|
|
if (!avatarUrl.current) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
showConfirmationAlert({
|
|
|
|
title: I18n.t('Discard_changes'),
|
|
|
|
message: I18n.t('Discard_changes_description'),
|
|
|
|
confirmationText: I18n.t('Discard'),
|
|
|
|
onPress: () => {
|
|
|
|
navigation.dispatch(e.data.action);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}, [navigation]);
|
|
|
|
|
2022-12-20 16:40:53 +00:00
|
|
|
const setAvatar = (value: IAvatar | null) => {
|
2022-12-07 23:17:08 +00:00
|
|
|
avatarUrl.current = value?.url;
|
|
|
|
setAvatarState(value);
|
|
|
|
};
|
|
|
|
|
|
|
|
const submit = async () => {
|
2022-12-14 17:15:16 +00:00
|
|
|
try {
|
|
|
|
setSaving(true);
|
|
|
|
if (!fromUser && room?.rid) {
|
|
|
|
// Change Rooms Avatar
|
2022-12-13 15:54:47 +00:00
|
|
|
await Services.saveRoomSettings(room.rid, { roomAvatar: avatar?.data });
|
2022-12-14 17:15:16 +00:00
|
|
|
} else if (avatar?.url) {
|
|
|
|
// Change User's Avatar
|
2022-12-07 23:17:08 +00:00
|
|
|
await Services.setAvatarFromService(avatar);
|
2022-12-14 17:15:16 +00:00
|
|
|
} else if (textAvatar) {
|
|
|
|
// Change User's Avatar
|
2022-12-07 23:17:08 +00:00
|
|
|
await Services.resetAvatar(user.id);
|
|
|
|
}
|
2022-12-14 17:15:16 +00:00
|
|
|
setSaving(false);
|
|
|
|
avatarUrl.current = '';
|
|
|
|
return navigation.goBack();
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
setSaving(false);
|
|
|
|
if (!fromUser && room?.rid) {
|
|
|
|
return handleError(e, 'saveRoomSettings', 'changing_avatar');
|
|
|
|
}
|
|
|
|
if (textAvatar) {
|
|
|
|
return handleError(e, 'resetAvatar', 'changing_avatar');
|
|
|
|
}
|
|
|
|
return handleError(e, 'setAvatarFromService', 'changing_avatar');
|
2022-12-07 23:17:08 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const resetAvatar = () => {
|
2022-12-20 16:40:53 +00:00
|
|
|
setAvatar(null);
|
2022-12-07 23:17:08 +00:00
|
|
|
setTextAvatar(`@${user.username}`);
|
|
|
|
avatarUrl.current = `@${user.username}`;
|
|
|
|
};
|
2022-12-07 03:11:25 +00:00
|
|
|
|
2022-12-12 21:13:12 +00:00
|
|
|
const resetRoomAvatar = () => {
|
2022-12-13 15:54:47 +00:00
|
|
|
setAvatar({ data: null });
|
|
|
|
avatarUrl.current = 'resetRoomAvatar';
|
2022-12-12 21:13:12 +00:00
|
|
|
};
|
|
|
|
|
2022-12-09 01:27:30 +00:00
|
|
|
const pickImage = async () => {
|
|
|
|
const options = {
|
|
|
|
cropping: true,
|
|
|
|
compressImageQuality: 0.8,
|
|
|
|
freeStyleCropEnabled: true,
|
|
|
|
cropperAvoidEmptySpaceAroundImage: false,
|
|
|
|
cropperChooseText: I18n.t('Choose'),
|
|
|
|
cropperCancelText: I18n.t('Cancel'),
|
|
|
|
includeBase64: true
|
|
|
|
};
|
|
|
|
try {
|
|
|
|
const response: Image = await ImagePicker.openPicker(options);
|
|
|
|
setAvatar({ url: response.path, data: `data:image/jpeg;base64,${response.data}`, service: 'upload' });
|
|
|
|
} catch (error) {
|
|
|
|
log(error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-12-13 15:54:47 +00:00
|
|
|
const ridProps = avatarUrl.current !== 'resetRoomAvatar' ? { rid: room?.rid } : {};
|
|
|
|
|
2022-12-07 03:11:25 +00:00
|
|
|
return (
|
|
|
|
<KeyboardView
|
|
|
|
style={{ backgroundColor: colors.auxiliaryBackground }}
|
|
|
|
contentContainerStyle={sharedStyles.container}
|
|
|
|
keyboardVerticalOffset={128}
|
|
|
|
>
|
|
|
|
<StatusBar />
|
|
|
|
<SafeAreaView testID='change-avatar-view'>
|
|
|
|
<ScrollView
|
|
|
|
contentContainerStyle={sharedStyles.containerScrollView}
|
|
|
|
testID='change-avatar-view-list'
|
|
|
|
{...scrollPersistTaps}
|
|
|
|
>
|
|
|
|
<View style={styles.avatarContainer} testID='change-avatar-view-avatar'>
|
2022-12-09 21:29:16 +00:00
|
|
|
<Avatar
|
2022-12-13 15:54:47 +00:00
|
|
|
text={room?.name || textAvatar || user.username}
|
2022-12-09 21:29:16 +00:00
|
|
|
avatar={avatar?.url}
|
|
|
|
isStatic={avatar?.url}
|
|
|
|
size={100}
|
2022-12-13 15:54:47 +00:00
|
|
|
type={t}
|
|
|
|
{...ridProps}
|
2022-12-09 21:29:16 +00:00
|
|
|
/>
|
2022-12-07 03:11:25 +00:00
|
|
|
</View>
|
2022-12-12 21:13:12 +00:00
|
|
|
{fromUser ? <AvatarUrl submit={value => setAvatar({ url: value, data: value, service: 'url' })} /> : null}
|
2022-12-07 03:11:25 +00:00
|
|
|
<List.Separator style={styles.separator} />
|
2022-12-12 21:13:12 +00:00
|
|
|
{fromUser ? <AvatarSuggestion resetAvatar={resetAvatar} user={user} onPress={setAvatar} /> : null}
|
2022-12-07 03:11:25 +00:00
|
|
|
|
|
|
|
<Button
|
|
|
|
title={I18n.t('Upload_image')}
|
|
|
|
type='secondary'
|
2022-12-07 23:17:08 +00:00
|
|
|
disabled={saving}
|
2022-12-15 12:51:26 +00:00
|
|
|
backgroundColor={colors.editAndUploadButtonAvatar}
|
2022-12-09 01:27:30 +00:00
|
|
|
onPress={pickImage}
|
2022-12-07 03:11:25 +00:00
|
|
|
testID='change-avatar-view-logout-other-locations'
|
|
|
|
/>
|
2022-12-13 17:18:31 +00:00
|
|
|
{!fromUser && serverVersion && compareServerVersion(serverVersion, 'greaterThanOrEqualTo', '3.6.0') ? (
|
2022-12-07 23:17:08 +00:00
|
|
|
<Button
|
|
|
|
title={I18n.t('Delete_image')}
|
|
|
|
type='primary'
|
|
|
|
disabled={saving}
|
|
|
|
backgroundColor={colors.dangerColor}
|
2022-12-12 21:13:12 +00:00
|
|
|
onPress={resetRoomAvatar}
|
2022-12-07 23:17:08 +00:00
|
|
|
testID='change-avatar-view-delete-my-account'
|
|
|
|
/>
|
|
|
|
) : null}
|
2022-12-07 03:11:25 +00:00
|
|
|
<Button
|
2022-12-07 23:17:08 +00:00
|
|
|
title={I18n.t('Save')}
|
|
|
|
disabled={!avatarUrl.current || saving}
|
2022-12-07 03:11:25 +00:00
|
|
|
type='primary'
|
2022-12-07 23:17:08 +00:00
|
|
|
loading={saving}
|
|
|
|
onPress={submit}
|
|
|
|
testID='change-avatar-view-submit'
|
2022-12-07 03:11:25 +00:00
|
|
|
/>
|
|
|
|
</ScrollView>
|
|
|
|
</SafeAreaView>
|
|
|
|
</KeyboardView>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ChangeAvatarView;
|