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

Conversation

krizzu
Copy link

@krizzu krizzu commented May 16, 2019

Summary

Part of #24875

Changelog

[General] [Added] - Add TurboModule spec for PermissionsAndroid

Test Plan

Flow passes on root level

@facebook-github-bot facebook-github-bot added CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. p: Callstack Partner: Callstack Partner labels May 16, 2019
@react-native-bot react-native-bot added API: PermissionsAndroid Platform: Android Android applications. Type: Enhancement A new feature or enhancement of an existing feature. labels May 16, 2019
@@ -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.

Copy link

@analysis-bot analysis-bot left a comment

Choose a reason for hiding this comment

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

Code analysis results:

  • eslint found some issues. Run yarn lint --fix to automatically fix problems.

@@ -11,6 +11,9 @@
'use strict';

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

import type {PermissionStatus} from './NativePermissionsAndroid'

Choose a reason for hiding this comment

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

prettier/prettier: Insert ;

@@ -11,6 +11,9 @@
'use strict';

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

import type {PermissionStatus} from './NativePermissionsAndroid'

Choose a reason for hiding this comment

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

semi: Missing semicolon.

Copy link
Contributor

@RSNara RSNara left a comment

Choose a reason for hiding this comment

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

This is great! Thanks. 😁

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}>

@retyui
Copy link
Contributor

retyui commented May 16, 2019

@RSNara, @krizzu

How about making more strict permission?

See tests case in the end

@krizzu
Copy link
Author

krizzu commented May 16, 2019

@retyui I'm up for it. Moved permissions to NativeAndroidPermissions, where I export both types and values.

@krizzu krizzu force-pushed the spec-androidpermissions branch from 78d5707 to 68d987c Compare May 16, 2019 21:05
Copy link

@analysis-bot analysis-bot left a comment

Choose a reason for hiding this comment

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

Code analysis results:

  • eslint found some issues. Run yarn lint --fix to automatically fix problems.

* @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

});

export type PermissionStatus = $Values<typeof PERMISSION_REQUEST_RESULT>;
export type PermissionType = $Values<typeof PERMISSIONS>;

Choose a reason for hiding this comment

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

prettier/prettier: Insert ;


export type PermissionStatus = $Values<typeof PERMISSION_REQUEST_RESULT>;
export type PermissionType = $Values<typeof PERMISSIONS>;

Choose a reason for hiding this comment

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

prettier/prettier: Insert ;

) => Promise<boolean>;

+requestMultiplePermissions: (
permissions: Array<PermissionType>,

Choose a reason for hiding this comment

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

prettier/prettier: Replace permissions:·Array<PermissionType>)·=>·Promise<{[permission:·PermissionType]:·PermissionStatus}> with ⏎····permissions:·Array<PermissionType>,⏎··)·=>·Promise<{[permission:·PermissionType]:·PermissionStatus}>;

@@ -11,6 +11,15 @@
'use strict';

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

Choose a reason for hiding this comment

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

prettier/prettier: Replace PERMISSION_REQUEST_RESULT,·PERMISSIONS with ⏎··PERMISSION_REQUEST_RESULT,⏎··PERMISSIONS,⏎

@@ -11,6 +11,15 @@
'use strict';

const NativeModules = require('../BatchedBridge/NativeModules');
import NativePermissionsAndroid, {
PERMISSION_REQUEST_RESULT,
PERMISSIONS,

Choose a reason for hiding this comment

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

prettier/prettier: Replace PermissionStatus,·PermissionType}·from·'./NativePermissionsAndroid' with ⏎··PermissionStatus,⏎··PermissionType,⏎}·from·'./NativePermissionsAndroid';

@@ -11,6 +11,15 @@
'use strict';

const NativeModules = require('../BatchedBridge/NativeModules');
import NativePermissionsAndroid, {
PERMISSION_REQUEST_RESULT,
PERMISSIONS,

Choose a reason for hiding this comment

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

semi: Missing semicolon.

@ericlewis ericlewis added the Flow label May 16, 2019
@krizzu
Copy link
Author

krizzu commented May 17, 2019

Another question: This module is Android only. Would TurboModuleRegistry.getEnforcing<Spec>('PermissionsAndroid') throw/return null on iOS then?

@ericlewis
Copy link
Contributor

ericlewis commented May 17, 2019 via email

@krizzu
Copy link
Author

krizzu commented May 19, 2019

@ericlewis You think that checking for the platform at the call site and returning negatively when it's not Android?

@ericlewis
Copy link
Contributor

ericlewis commented May 19, 2019 via email

Copy link

@analysis-bot analysis-bot left a comment

Choose a reason for hiding this comment

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

Code analysis results:

  • eslint found some issues. Run yarn lint --fix to automatically fix problems.

@@ -77,11 +48,13 @@ class PermissionsAndroid {
*
* @deprecated
*/
checkPermission(permission: string): Promise<boolean> {
checkPermission(permission: PermissionType): Promise<boolean> {
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.

@@ -90,8 +63,9 @@ 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.

@@ -109,12 +83,14 @@ class PermissionsAndroid {
* @deprecated
*/
async requestPermission(
permission: string,
permission: PermissionType,
rationale?: Rationale,
): 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.

@@ -126,11 +102,13 @@ class PermissionsAndroid {
* See https://facebook.github.io/react-native/docs/permissionsandroid.html#request
*/
async request(

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.

@@ -158,11 +134,11 @@ class PermissionsAndroid {
* See https://facebook.github.io/react-native/docs/permissionsandroid.html#requestmultiple
*/
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.

@krizzu krizzu force-pushed the spec-androidpermissions branch from 7e82647 to 0e0e926 Compare May 19, 2019 18:51
Copy link
Contributor

@fkgozali fkgozali left a comment

Choose a reason for hiding this comment

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

Sorry for the confusion, but the typing should not add any cost to the JS bundle size. That said, let's just define union types inline, instead of defining a frozen object:

Instead of:

export const PERMISSION_REQUEST_RESULT = Object.freeze({
  GRANTED: 'granted',
  DENIED: 'denied',
  NEVER_ASK_AGAIN: 'never_ask_again',
});

export type PermissionStatus = $Values<typeof PERMISSION_REQUEST_RESULT>;

do:

export type PermissionStatus = 'granted' | 'denied' | 'never_ask_again';

Also, can you run prettier formatter?

@retyui
Copy link
Contributor

retyui commented May 20, 2019

@fkgozali
you should carefully look at PR

image

@fkgozali
Copy link
Contributor

you should carefully look at PR

Cool, but for the purpose for using Flow type to generate the C++/ObjC/Java code, we need the type to be fully inline for enum (can't use $Values<> atm), so we'll have to create the types without modifying the usage of PERMISSION_REQUEST_RESULT.

export type PermissionStatus = 'granted' | 'denied' | 'never_ask_again';

@retyui
Copy link
Contributor

retyui commented May 21, 2019

@fkgozali

purpose for using Flow type to generate the C++/ObjC/Java code

I can get acquainted with your native code generator?

@krizzu
Copy link
Author

krizzu commented May 23, 2019

@fkgozali @retyui
Right, so this means we have to drop $Values<>, and inline those string values instead.

I'm getting into it.

@fkgozali
Copy link
Contributor

fkgozali commented May 23, 2019 via email

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@fkgozali has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@react-native-bot
Copy link
Collaborator

This pull request was successfully merged by @krizzu in 82fe1b0.

When will my fix make it into a release? | Upcoming Releases

@react-native-bot react-native-bot added the Merged This PR has been merged. label May 30, 2019
M-i-k-e-l pushed a commit to M-i-k-e-l/react-native that referenced this pull request Mar 10, 2020
Summary:
Part of facebook#24875

## Changelog
[General] [Added] - Add TurboModule spec for PermissionsAndroid
Pull Request resolved: facebook#24886

Reviewed By: RSNara

Differential Revision: D15542996

Pulled By: fkgozali

fbshipit-source-id: cab02d97e70d65347f63e891cff98c17adc1fdba
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API: PermissionsAndroid CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Flow Merged This PR has been merged. Native Module p: Callstack Partner: Callstack Partner Platform: Android Android applications. Type: Enhancement A new feature or enhancement of an existing feature.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants