Skip to content

Commit

Permalink
Get permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
diegolmello committed Feb 21, 2025
1 parent eedacf4 commit c7e6335
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
4 changes: 4 additions & 0 deletions app/containers/MessageComposer/hooks/useChooseMedia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { useAppSelector } from '../../../lib/hooks';
import { useRoomContext } from '../../../views/RoomView/context';
import ImagePicker from '../../../lib/methods/helpers/ImagePicker/ImagePicker';
import { mapMediaResult } from '../../../lib/methods/helpers/ImagePicker/mapMediaResult';
import { getPermissions } from '../../../lib/methods/helpers/ImagePicker/getPermissions';
import { IShareAttachment } from '../../../definitions';

export const useChooseMedia = ({
Expand All @@ -30,6 +31,7 @@ export const useChooseMedia = ({

const takePhoto = async () => {
try {
await getPermissions('camera');
const result = await ImagePicker.launchCameraAsync(IMAGE_PICKER_CONFIG);
if (result.canceled) {
return;
Expand All @@ -53,6 +55,7 @@ export const useChooseMedia = ({

const takeVideo = async () => {
try {
await getPermissions('camera');
const result = await ImagePicker.launchCameraAsync(VIDEO_PICKER_CONFIG);
if (result.canceled) {
return;
Expand All @@ -76,6 +79,7 @@ export const useChooseMedia = ({

const chooseFromLibrary = async () => {
try {
await getPermissions('library');
const result = await ImagePicker.launchImageLibraryAsync(LIBRARY_PICKER_CONFIG);
if (result.canceled) {
return;
Expand Down
16 changes: 16 additions & 0 deletions app/lib/methods/helpers/ImagePicker/getPermissions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Linking } from 'react-native';

import ImagePicker from './ImagePicker';

export const getPermissions = async (type: 'camera' | 'library') => {
const method = type === 'camera' ? 'requestCameraPermissionsAsync' : 'requestMediaLibraryPermissionsAsync';
const requestResult = await ImagePicker[method]();
if (!requestResult.canAskAgain) {
Linking.openURL('app-settings:');
return Promise.reject();
}
if (!requestResult.granted) {
return Promise.reject();
}
return Promise.resolve();
};
5 changes: 3 additions & 2 deletions app/views/ChangeAvatarView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import AvatarSuggestion from './AvatarSuggestion';
import log from '../../lib/methods/helpers/log';
import { changeRoomsAvatar, changeUserAvatar, resetUserAvatar } from './submitServices';
import ImagePicker from '../../lib/methods/helpers/ImagePicker/ImagePicker';
import { getPermissions } from '../../lib/methods/helpers/ImagePicker/getPermissions';
import { mapMediaResult } from '../../lib/methods/helpers/ImagePicker/mapMediaResult';
import { isImageURL, useDebounce } from '../../lib/methods/helpers';
import { FormTextInput } from '../../containers/TextInput';
Expand Down Expand Up @@ -172,8 +173,8 @@ const ChangeAvatarView = () => {
exif: true,
base64: true
};
const response =
isCam === true ? await ImagePicker.launchCameraAsync(options) : await ImagePicker.launchImageLibraryAsync(options);
await getPermissions(isCam ? 'camera' : 'library');
const response = isCam ? await ImagePicker.launchCameraAsync(options) : await ImagePicker.launchImageLibraryAsync(options);
if (response.canceled) {
return;
}
Expand Down

0 comments on commit c7e6335

Please sign in to comment.