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 b767a8d
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion app/containers/MessageComposer/hooks/useChooseMedia.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Alert } from 'react-native';
import { Alert, Linking } from 'react-native';
import * as DocumentPicker from 'expo-document-picker';

import { IMAGE_PICKER_CONFIG, LIBRARY_PICKER_CONFIG, VIDEO_PICKER_CONFIG } from '../constants';
Expand All @@ -14,6 +14,19 @@ import ImagePicker from '../../../lib/methods/helpers/ImagePicker/ImagePicker';
import { mapMediaResult } from '../../../lib/methods/helpers/ImagePicker/mapMediaResult';
import { IShareAttachment } from '../../../definitions';

const getPermissions = async (type: 'camera' | 'media') => {
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();
};

export const useChooseMedia = ({
rid,
tmid,
Expand All @@ -30,6 +43,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 +67,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 +91,7 @@ export const useChooseMedia = ({

const chooseFromLibrary = async () => {
try {
await getPermissions('media');
const result = await ImagePicker.launchImageLibraryAsync(LIBRARY_PICKER_CONFIG);
if (result.canceled) {
return;
Expand Down

0 comments on commit b767a8d

Please sign in to comment.