Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798import {Linking} from 'react-native'import {useCameraPermissions as useExpoCameraPermissions} from 'expo-camera'import * as MediaLibrary from 'expo-media-library'
import {DEFAULT_BRAND_CONFIG} from '#/lib/community/BrandContext'import {Alert} from '#/view/com/util/Alert'import {IS_WEB} from '#/env'
const openPermissionAlert = (perm: string) => { Alert.alert( 'Permission needed', `${DEFAULT_BRAND_CONFIG.metadata.displayName} does not have permission to access your ${perm}.`, [ { text: 'Cancel', style: 'cancel', }, {text: 'Open Settings', onPress: () => Linking.openSettings()}, ], )}
export function usePhotoLibraryPermission() { const [res, requestPermission] = MediaLibrary.usePermissions({ granularPermissions: ['photo'], }) const requestPhotoAccessIfNeeded = async () => { // On the, we use <input type="file"> to produce a filepicker // This does not need any permission granting. if (IS_WEB) { return true }
if (res?.granted) { return true } else if (!res || res.status === 'undetermined' || res?.canAskAgain) { const {canAskAgain, granted, status} = await requestPermission()
if (!canAskAgain && status === 'undetermined') { openPermissionAlert('photo library') }
return granted } else { openPermissionAlert('photo library') return false } } return {requestPhotoAccessIfNeeded}}
export function useVideoLibraryPermission() { const [res, requestPermission] = MediaLibrary.usePermissions({ granularPermissions: ['video'], }) const requestVideoAccessIfNeeded = async () => { // On the, we use <input type="file"> to produce a filepicker // This does not need any permission granting. if (IS_WEB) { return true }
if (res?.granted) { return true } else if (!res || res.status === 'undetermined' || res?.canAskAgain) { const {canAskAgain, granted, status} = await requestPermission()
if (!canAskAgain && status === 'undetermined') { openPermissionAlert('video library') }
return granted } else { openPermissionAlert('video library') return false } } return {requestVideoAccessIfNeeded}}
export function useCameraPermission() { const [res, requestPermission] = useExpoCameraPermissions()
const requestCameraAccessIfNeeded = async () => { if (res?.granted) { return true } else if (!res || res?.status === 'undetermined' || res?.canAskAgain) { const updatedRes = await requestPermission() return updatedRes?.granted } else { openPermissionAlert('camera') return false } }
return {requestCameraAccessIfNeeded}}