All files / apps/host/src/utils permissions.ts

95.65% Statements 22/23
92.85% Branches 13/14
80% Functions 4/5
95.65% Lines 22/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65      1x 7x     1x 1x                   1x             1x 7x 7x 7x   6x 3x     3x 1x       1x     2x   2x 1x     1x 1x     1x     2x     1x            
import { check, openSettings, PERMISSIONS, request, RESULTS } from 'react-native-permissions';
import { Alert, Platform } from 'react-native';
 
const getCameraPermission = () => {
  return Platform.OS === 'ios' ? PERMISSIONS.IOS.CAMERA : PERMISSIONS.ANDROID.CAMERA;
};
 
const showBlockedAlert = (title: string, message: string) => {
  Alert.alert(
    title,
    message,
    [
      {
        text: 'Cancel',
        style: 'cancel',
      },
      {
        text: 'Open Settings',
        onPress: () => openSettings(),
      },
    ],
    { cancelable: true },
  );
};
 
export const requestCameraPermission = async () => {
  const permission = getCameraPermission();
  try {
    const initialStatus = await check(permission);
 
    if (initialStatus === RESULTS.GRANTED || initialStatus === RESULTS.LIMITED) {
      return true;
    }
 
    if (initialStatus === RESULTS.BLOCKED) {
      showBlockedAlert(
        'Camera Permission',
        'Camera access is required to take photos. Please enable it in the app settings.',
      );
      return false;
    }
 
    const status = await request(permission);
 
    if (status === RESULTS.GRANTED || status === RESULTS.LIMITED) {
      return true;
    }
 
    Eif (status === RESULTS.UNAVAILABLE) {
      Alert.alert('Camera Unavailable', 'Camera is not available on this device.');
    }
  } catch (error) {
    console.error('Failed to handle camera permission', error);
  }
 
  return false;
};
 
export const requestPhotoLibraryPermission = async () => {
  // With modern React Native and react-native-image-picker, we rely on the out-of-process
  // system pickers (PHPickerViewController on iOS 14+, Photo Picker/ACTION_GET_CONTENT on Android).
  // These do not require the app to explicitly declare or request library permissions.
  return true;
};