From 7ab27279469ef5dda25d8a837b7c324dd885600c Mon Sep 17 00:00:00 2001 From: Gleidson Daniel Date: Tue, 23 Aug 2022 10:32:28 -0300 Subject: [PATCH] finish methods of pick images and videos --- app/lib/methods/mediaPicker.ts | 101 +++++++++++++++++---------------- 1 file changed, 53 insertions(+), 48 deletions(-) diff --git a/app/lib/methods/mediaPicker.ts b/app/lib/methods/mediaPicker.ts index 7a5050c0a..f71a55fa5 100644 --- a/app/lib/methods/mediaPicker.ts +++ b/app/lib/methods/mediaPicker.ts @@ -5,58 +5,40 @@ import { PermissionsAndroid } from 'react-native'; import { isAndroid } from './helpers'; import log from './helpers/log'; -export const pickImage = async (editImage = true): Promise => { - try { - const result = await ImagePicker.launchImageLibraryAsync({ - mediaTypes: ImagePicker.MediaTypeOptions.Images, - allowsEditing: editImage, - quality: 0 - }); - return result; - } catch (error) { - log(error); - return null; - } -}; - -export const pickVideo = async (editImage = true): Promise => { - try { - const result = await ImagePicker.launchImageLibraryAsync({ - mediaTypes: ImagePicker.MediaTypeOptions.Videos, - allowsEditing: editImage, - quality: 0 - }); - return result; - } catch (error) { - log(error); - return null; - } -}; - -export const pickImageFromCamera = async (allowsEditing = false): Promise => { - try { - if (isAndroid) { - const permissions = await PermissionsAndroid.requestMultiple([ - PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE, - PermissionsAndroid.PERMISSIONS.CAMERA - ]); - if (permissions['android.permission.CAMERA'] !== 'granted') { - return null; - } - } else { - const permission = await ImagePicker.requestCameraPermissionsAsync(); - if (!permission.granted) { - return null; - } +const handlePermission = async (): Promise => { + if (isAndroid) { + const permissions = await PermissionsAndroid.requestMultiple([ + PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE, + PermissionsAndroid.PERMISSIONS.CAMERA + ]); + if (permissions['android.permission.CAMERA'] !== 'granted') { + return false; } - - const permission = await ImagePicker.getCameraPermissionsAsync(); + } else { + const permission = await ImagePicker.requestCameraPermissionsAsync(); if (!permission.granted) { - return null; + return false; } + } + const permission = await ImagePicker.getCameraPermissionsAsync(); + if (!permission.granted) { + return false; + } + + return true; +}; + +const pickFromCamera = async ( + allowsEditing: boolean, + mediaType: ImagePicker.MediaTypeOptions, + mime: string +): Promise => { + try { + const hasPermission = await handlePermission(); + if (!hasPermission) return null; const result = await ImagePicker.launchCameraAsync({ - mediaTypes: ImagePicker.MediaTypeOptions.Images, + mediaTypes: mediaType, quality: 1, allowsEditing }); @@ -68,7 +50,7 @@ export const pickImageFromCamera = async (allowsEditing = false): Promise => { + try { + const result = await ImagePicker.launchImageLibraryAsync({ + mediaTypes: ImagePicker.MediaTypeOptions.All, + quality: undefined, // to force animated gifs + allowsMultipleSelection: true + }); + if (!result.cancelled) { + return result; + } + return null; + } catch (error) { + log(error); + return null; + } +}; + +export const pickVideoFromCamera = (allowsEditing = false): Promise => + pickFromCamera(allowsEditing, ImagePicker.MediaTypeOptions.Videos, 'video/mp4'); + +export const pickImageFromCamera = (allowsEditing = false): Promise => + pickFromCamera(allowsEditing, ImagePicker.MediaTypeOptions.Images, 'image/jpeg');