You've already forked joplin
mirror of
https://github.com/laurent22/joplin.git
synced 2025-11-23 22:36:32 +02:00
37 lines
1.1 KiB
TypeScript
37 lines
1.1 KiB
TypeScript
import Logger from '@joplin/utils/Logger';
|
|
|
|
import { Platform, PermissionsAndroid, Permission } from 'react-native';
|
|
const logger = Logger.create('checkPermissions');
|
|
|
|
type Rationale = {
|
|
title: string;
|
|
message: string;
|
|
buttonPositive: string;
|
|
buttonNegative?: string;
|
|
buttonNeutral?: string;
|
|
};
|
|
|
|
interface Options {
|
|
rationale?: Rationale;
|
|
onRequestConfirmation?: ()=> Promise<boolean>;
|
|
}
|
|
|
|
export default async (permissions: Permission, { rationale, onRequestConfirmation }: Options = {}) => {
|
|
// On iOS, permissions are prompted for by the system, so here we assume it's granted.
|
|
if (Platform.OS !== 'android') return PermissionsAndroid.RESULTS.GRANTED;
|
|
|
|
const granted = await PermissionsAndroid.check(permissions);
|
|
logger.info('Checked permission:', granted);
|
|
if (granted) {
|
|
return PermissionsAndroid.RESULTS.GRANTED;
|
|
} else {
|
|
if (onRequestConfirmation && !await onRequestConfirmation()) {
|
|
return PermissionsAndroid.RESULTS.NEVER_ASK_AGAIN;
|
|
}
|
|
|
|
const result = await PermissionsAndroid.request(permissions, rationale);
|
|
logger.info('Requested permission:', result);
|
|
return result;
|
|
}
|
|
};
|