Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TM] add spec for PermissionsAndroid #24886

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Libraries/PermissionsAndroid/NativePermissionsAndroid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow
*/

'use strict';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prettier/prettier: Delete


import type {TurboModule} from 'RCTExport';
import * as TurboModuleRegistry from 'TurboModuleRegistry';

export interface Spec extends TurboModule {
+checkPermission: (permission: string) => Promise<boolean>;

+requestPermission: (permission: string) => Promise<string>;

+shouldShowRequestPermissionRationale: (
permission: string,
) => Promise<boolean>;

+requestMultiplePermissions: (permissions: Array<string>) => Promise<Object>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Promise<Object> => Promise<{[permission: string]: PermissionStatus}>

}

export default TurboModuleRegistry.getEnforcing<Spec>('PermissionsAndroid');
19 changes: 8 additions & 11 deletions Libraries/PermissionsAndroid/PermissionsAndroid.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
'use strict';

const NativeModules = require('../BatchedBridge/NativeModules');
import NativePermissionsAndroid from './NativePermissionsAndroid';

export type Rationale = {
title: string,
Expand All @@ -20,7 +21,7 @@ export type Rationale = {
buttonNeutral?: string,
};

type PermissionStatus = 'granted' | 'denied' | 'never_ask_again';
type PermissionStatus = 'granted' | 'denied' | 'never_ask_again' | string;
Copy link
Contributor

@retyui retyui May 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you adding string ? @krizzu

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have had exact declared 3 types:

private final String GRANTED = "granted";
private final String DENIED = "denied";
private final String NEVER_ASK_AGAIN = "never_ask_again";

Copy link
Author

@krizzu krizzu May 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@retyui I've added it because of this method resolve with PermissionStatus. I'm not quite sure if I could add literals in Spec, so I ended up with adding string to the type.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@krizzu
Now Spec is source of truth

You should move PermissionStatus type into Libraries/PermissionsAndroid/NativePermissionsAndroid.js

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@retyui
Gotcha. Done that.

/**
* `PermissionsAndroid` provides access to Android M's new permissions model.
*
Expand Down Expand Up @@ -81,7 +82,7 @@ class PermissionsAndroid {
console.warn(
'"PermissionsAndroid.checkPermission" is deprecated. Use "PermissionsAndroid.check" instead',
);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

curly: Expected { after 'if' condition.

return NativeModules.PermissionsAndroid.checkPermission(permission);
return NativePermissionsAndroid.checkPermission(permission);
}

/**
Expand All @@ -91,7 +92,7 @@ class PermissionsAndroid {
* See https://facebook.github.io/react-native/docs/permissionsandroid.html#check
*/
check(permission: string): Promise<boolean> {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

curly: Expected { after 'if' condition.

return NativeModules.PermissionsAndroid.checkPermission(permission);
return NativePermissionsAndroid.checkPermission(permission);
}

/**
Expand Down Expand Up @@ -130,7 +131,7 @@ class PermissionsAndroid {
rationale?: Rationale,
): Promise<PermissionStatus> {
if (rationale) {
const shouldShowRationale = await NativeModules.PermissionsAndroid.shouldShowRequestPermissionRationale(
const shouldShowRationale = await NativePermissionsAndroid.shouldShowRequestPermissionRationale(
permission,
);

Expand All @@ -140,14 +141,12 @@ class PermissionsAndroid {
rationale,
() => reject(new Error('Error showing rationale')),
() =>
resolve(
NativeModules.PermissionsAndroid.requestPermission(permission),
),
resolve(NativePermissionsAndroid.requestPermission(permission)),
);
});
}
}
return NativeModules.PermissionsAndroid.requestPermission(permission);
return NativePermissionsAndroid.requestPermission(permission);
}

/**
Expand All @@ -160,9 +159,7 @@ class PermissionsAndroid {
requestMultiple(
permissions: Array<string>,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

curly: Expected { after 'if' condition.

): Promise<{[permission: string]: PermissionStatus}> {
return NativeModules.PermissionsAndroid.requestMultiplePermissions(
permissions,
);
return NativePermissionsAndroid.requestMultiplePermissions(permissions);
}
}

Expand Down