diff options
Diffstat (limited to 'src/lib/hooks/usePermissions.ts')
-rw-r--r-- | src/lib/hooks/usePermissions.ts | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/lib/hooks/usePermissions.ts b/src/lib/hooks/usePermissions.ts new file mode 100644 index 000000000..36a92ac32 --- /dev/null +++ b/src/lib/hooks/usePermissions.ts @@ -0,0 +1,50 @@ +import {Alert} from 'react-native' +import {Camera} from 'expo-camera' +import * as MediaLibrary from 'expo-media-library' +import {Linking} from 'react-native' + +const openSettings = () => { + Linking.openURL('app-settings:') +} + +const openPermissionAlert = (perm: string) => { + Alert.alert( + 'Permission needed', + `Bluesky does not have permission to access your ${perm}.`, + [ + { + text: 'Cancel', + style: 'cancel', + }, + {text: 'Open Settings', onPress: () => openSettings()}, + ], + ) +} + +export function usePhotoLibraryPermission() { + const [mediaLibraryPermissions] = MediaLibrary.usePermissions() + const requestPhotoAccessIfNeeded = async () => { + if (mediaLibraryPermissions?.status === 'granted') { + return true + } else { + openPermissionAlert('photo library') + return false + } + } + return {requestPhotoAccessIfNeeded} +} + +export function useCameraPermission() { + const [cameraPermissionStatus] = Camera.useCameraPermissions() + + const requestCameraAccessIfNeeded = async () => { + if (cameraPermissionStatus?.granted) { + return true + } else { + openPermissionAlert('camera') + return false + } + } + + return {requestCameraAccessIfNeeded} +} |