An ATproto social media client -- with an independent Appview.

Save image to user media library when taken from camera during composing (#3180)

* save images to media library when taken from camera

* ensure we have access to media library

* `canAskAgain`

* just use MediaLibrary directly to avoid duplication

authored by hailey.at and committed by

GitHub 80cc1f18 ee57d747

+18 -1
+18 -1
src/view/com/composer/photos/OpenCameraBtn.tsx
··· 1 1 import React, {useCallback} from 'react' 2 2 import {TouchableOpacity, StyleSheet} from 'react-native' 3 + import * as MediaLibrary from 'expo-media-library' 3 4 import { 4 5 FontAwesomeIcon, 5 6 FontAwesomeIconStyle, ··· 24 25 const {track} = useAnalytics() 25 26 const {_} = useLingui() 26 27 const {requestCameraAccessIfNeeded} = useCameraPermission() 28 + const [mediaPermissionRes, requestMediaPermission] = 29 + MediaLibrary.usePermissions() 27 30 28 31 const onPressTakePicture = useCallback(async () => { 29 32 track('Composer:CameraOpened') 30 33 try { 31 34 if (!(await requestCameraAccessIfNeeded())) { 32 35 return 36 + } 37 + if (!mediaPermissionRes?.granted && mediaPermissionRes?.canAskAgain) { 38 + await requestMediaPermission() 33 39 } 34 40 35 41 const img = await openCamera({ ··· 38 44 freeStyleCropEnabled: true, 39 45 }) 40 46 47 + // If we don't have permissions it's fine, we just wont save it. The post itself will still have access to 48 + // the image even without these permissions 49 + if (mediaPermissionRes) { 50 + await MediaLibrary.createAssetAsync(img.path) 51 + } 41 52 gallery.add(img) 42 53 } catch (err: any) { 43 54 // ignore 44 55 logger.warn('Error using camera', {error: err}) 45 56 } 46 - }, [gallery, track, requestCameraAccessIfNeeded]) 57 + }, [ 58 + gallery, 59 + track, 60 + requestCameraAccessIfNeeded, 61 + mediaPermissionRes, 62 + requestMediaPermission, 63 + ]) 47 64 48 65 const shouldShowCameraButton = isNative || isMobileWeb 49 66 if (!shouldShowCameraButton) {