2024-01-25 14:11:07 +00:00
|
|
|
import { Audio } from 'expo-av';
|
2024-01-31 14:28:34 +00:00
|
|
|
import React, { useContext } from 'react';
|
|
|
|
import { Alert } from 'react-native';
|
|
|
|
import { PermissionStatus } from 'expo-camera';
|
2024-01-25 14:11:07 +00:00
|
|
|
|
2024-01-31 14:28:34 +00:00
|
|
|
import i18n from '../../../../i18n';
|
2024-01-25 14:11:07 +00:00
|
|
|
import { useAppSelector } from '../../../../lib/hooks';
|
2024-01-31 14:28:34 +00:00
|
|
|
import { openAppSettings } from '../../../../lib/methods/helpers/openAppSettings';
|
|
|
|
import { useTheme } from '../../../../theme';
|
2024-01-25 14:11:07 +00:00
|
|
|
import { useRoomContext } from '../../../../views/RoomView/context';
|
2024-01-31 14:28:34 +00:00
|
|
|
import { MessageInnerContext, useMessageComposerApi, useMicOrSend } from '../../context';
|
|
|
|
import { useCanUploadFile } from '../../hooks';
|
|
|
|
import { BaseButton } from './BaseButton';
|
2024-01-25 14:11:07 +00:00
|
|
|
|
2024-01-31 14:28:34 +00:00
|
|
|
export const MicOrSendButton = (): React.ReactElement | null => {
|
2024-01-25 14:11:07 +00:00
|
|
|
const { rid, sharing } = useRoomContext();
|
|
|
|
const micOrSend = useMicOrSend();
|
|
|
|
const { sendMessage } = useContext(MessageInnerContext);
|
|
|
|
const permissionToUpload = useCanUploadFile(rid);
|
|
|
|
const { Message_AudioRecorderEnabled } = useAppSelector(state => state.settings);
|
|
|
|
const { colors } = useTheme();
|
|
|
|
const { setRecordingAudio } = useMessageComposerApi();
|
|
|
|
|
2024-01-31 14:28:34 +00:00
|
|
|
const requestPermissionAndStartToRecordAudio = () =>
|
|
|
|
Audio.requestPermissionsAsync()
|
|
|
|
.then(({ granted }) => setRecordingAudio(granted))
|
|
|
|
.catch(() => {});
|
|
|
|
|
2024-01-25 14:11:07 +00:00
|
|
|
const startRecording = async () => {
|
2024-01-31 14:28:34 +00:00
|
|
|
const { status, granted, canAskAgain } = await Audio.getPermissionsAsync();
|
|
|
|
if (granted) return setRecordingAudio(true);
|
|
|
|
if (status === PermissionStatus.UNDETERMINED) return requestPermissionAndStartToRecordAudio();
|
|
|
|
if (canAskAgain) return requestPermissionAndStartToRecordAudio();
|
|
|
|
|
|
|
|
Alert.alert(
|
|
|
|
i18n.t('Microphone_access_needed_to_record_audio'),
|
|
|
|
i18n.t('Go_to_your_device_settings_and_allow_microphone'),
|
|
|
|
[
|
|
|
|
{
|
|
|
|
text: i18n.t('Cancel'),
|
|
|
|
style: 'cancel'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: i18n.t('Settings'),
|
|
|
|
onPress: openAppSettings
|
|
|
|
}
|
|
|
|
],
|
|
|
|
{ cancelable: false }
|
|
|
|
);
|
2024-01-25 14:11:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (micOrSend === 'send' || sharing) {
|
|
|
|
return (
|
|
|
|
<BaseButton
|
|
|
|
onPress={sendMessage}
|
|
|
|
testID='message-composer-send'
|
|
|
|
accessibilityLabel='Send_message'
|
|
|
|
icon='send-filled'
|
|
|
|
color={colors.strokeHighlight}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Message_AudioRecorderEnabled && permissionToUpload) {
|
|
|
|
return (
|
|
|
|
<BaseButton
|
|
|
|
onPress={startRecording}
|
|
|
|
testID='message-composer-send-audio'
|
|
|
|
accessibilityLabel='Send_audio_message'
|
|
|
|
icon='microphone'
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
};
|