| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 | // @flow
import { NativeModules } from 'react-native'
const PermissionsIOS = NativeModules.ReactNativePermissions
const permissionTypes = [
  'location',
  'camera',
  'microphone',
  'photo',
  'contacts',
  'event',
  'reminder',
  'bluetooth',
  'notification',
  'backgroundRefresh',
  'speechRecognition',
]
const DEFAULTS = {
  location: 'whenInUse',
  notification: ['alert', 'badge', 'sound'],
}
class ReactNativePermissions {
  canOpenSettings = () => PermissionsIOS.canOpenSettings()
  openSettings = () => PermissionsIOS.openSettings()
  getTypes = () => permissionTypes
  check = (permission, type) => {
    if (!permissionTypes.includes(permission)) {
      return Promise.reject(
        `ReactNativePermissions: ${
          permission
        } is not a valid permission type on iOS`,
      )
    }
    return PermissionsIOS.getPermissionStatus(
      permission,
      type || DEFAULTS[permission],
    )
  }
  request = (permission, type) => {
    if (!permissionTypes.includes(permission)) {
      return Promise.reject(
        `ReactNativePermissions: ${
          permission
        } is not a valid permission type on iOS`,
      )
    }
    if (permission == 'backgroundRefresh') {
      return Promise.reject(
        'ReactNativePermissions: You cannot request backgroundRefresh',
      )
    }
    return PermissionsIOS.requestPermission(
      permission,
      type || DEFAULTS[permission],
    )
  }
  checkMultiple = permissions =>
    Promise.all(permissions.map(permission => this.check(permission))).then(
      result =>
        result.reduce((acc, value, index) => {
          const name = permissions[index]
          acc[name] = value
          return acc
        }, {}),
    )
}
module.exports = new ReactNativePermissions()
 |