Skip to content

Instantly share code, notes, and snippets.

@steniowagner
Created February 18, 2019 22:43
Show Gist options
  • Save steniowagner/5b9d59461cf78f43f2f6680a2cad33c0 to your computer and use it in GitHub Desktop.
Save steniowagner/5b9d59461cf78f43f2f6680a2cad33c0 to your computer and use it in GitHub Desktop.
import { PermissionsAndroid } from 'react-native';
export const PERMISSIONS_TYPES = {
WRITE_EXTERNAL_STORAGE: 'WRITE_EXTERNAL_STORAGE',
READ_EXTERNAL_STORAGE: 'READ_EXTERNAL_STORAGE',
};
const PERMISSIONS = {
[PERMISSIONS_TYPES.WRITE_EXTERNAL_STORAGE]: {
type: 'WRITE_EXTERNAL_STORAGE',
config: {
title: 'MindCast App Create File Permission',
message:
'MindCast needs access to your storage '
+ 'so you can save your podcasts locally.',
},
},
[PERMISSIONS_TYPES.READ_EXTERNAL_STORAGE]: {
type: 'READ_EXTERNAL_STORAGE',
config: {
title: 'MindCast App Read File Permission',
message:
'MindCast needs access to your storage '
+ 'so you can listen your podcasts locally and offline.',
},
},
};
export const requestPermission = async (type) => {
try {
const permissionConfig = PERMISSIONS[type];
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS[type],
{
...permissionConfig.config,
buttonNeutral: 'Ask Me Later',
buttonNegative: 'Cancel',
buttonPositive: 'OK',
},
);
return granted === PermissionsAndroid.RESULTS.GRANTED;
} catch (err) {
console.tron.log(err);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment