Browse Source

Add background compatibity for type options

Mathieu Acthernoene 7 years ago
parent
commit
06a834dd0e
5 changed files with 105 additions and 67 deletions
  1. 9
    0
      .flowconfig
  2. 59
    46
      lib/permissions.android.js
  3. 26
    15
      lib/permissions.ios.js
  4. 1
    0
      package.json
  5. 10
    6
      yarn.lock

+ 9
- 0
.flowconfig View File

1
+[ignore]
2
+
3
+[include]
4
+
5
+[libs]
6
+
7
+[lints]
8
+
9
+[options]

+ 59
- 46
lib/permissions.android.js View File

2
 
2
 
3
 import { AsyncStorage, NativeModules, PermissionsAndroid } from 'react-native'
3
 import { AsyncStorage, NativeModules, PermissionsAndroid } from 'react-native'
4
 
4
 
5
+type Status = 'authorized' | 'denied' | 'restricted' | 'undetermined'
6
+type Rationale = { title: string, message: string }
7
+type Options = string | { type: string, rationale?: Rationale }
8
+
5
 const permissionTypes = {
9
 const permissionTypes = {
6
   location: PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
10
   location: PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
7
   camera: PermissionsAndroid.PERMISSIONS.CAMERA,
11
   camera: PermissionsAndroid.PERMISSIONS.CAMERA,
23
 
27
 
24
 const STORAGE_KEY = '@RNPermissions:didAskPermission:'
28
 const STORAGE_KEY = '@RNPermissions:didAskPermission:'
25
 
29
 
26
-const setDidAskOnce = permission =>
30
+const setDidAskOnce = (permission: string) =>
27
   AsyncStorage.setItem(STORAGE_KEY + permission, 'true')
31
   AsyncStorage.setItem(STORAGE_KEY + permission, 'true')
28
 
32
 
29
-const getDidAskOnce = permission =>
33
+const getDidAskOnce = (permission: string) =>
30
   AsyncStorage.getItem(STORAGE_KEY + permission).then(item => !!item)
34
   AsyncStorage.getItem(STORAGE_KEY + permission).then(item => !!item)
31
 
35
 
32
 class ReactNativePermissions {
36
 class ReactNativePermissions {
33
-  canOpenSettings = () => false
34
-  openSettings = () => Promise.reject("'openSettings' is deprecated on android")
35
-  getTypes = () => Object.keys(permissionTypes)
37
+  canOpenSettings: () => Promise<boolean> = () => Promise.resolve(false)
38
+
39
+  openSettings: () => Promise<*> = () =>
40
+    Promise.reject(new Error("'openSettings' is deprecated on android"))
36
 
41
 
37
-  check = permission => {
38
-    const androidPermission = permissionTypes[permission]
42
+  getTypes: () => Array<string> = () => Object.keys(permissionTypes)
39
 
43
 
40
-    if (!androidPermission) {
41
-      return Promise.reject(
44
+  check = (permission: string, type?: string): Promise<Status> => {
45
+    if (!permissionTypes[permission]) {
46
+      const error = new Error(
42
         `ReactNativePermissions: ${
47
         `ReactNativePermissions: ${
43
           permission
48
           permission
44
         } is not a valid permission type on Android`,
49
         } is not a valid permission type on Android`,
45
       )
50
       )
46
-    }
47
 
51
 
48
-    return PermissionsAndroid.check(androidPermission).then(isAuthorized => {
49
-      if (isAuthorized) {
50
-        return 'authorized'
51
-      }
52
+      return Promise.reject(error)
53
+    }
52
 
54
 
53
-      return getDidAskOnce(permission).then(didAsk => {
54
-        if (didAsk) {
55
-          return NativeModules.PermissionsAndroid.shouldShowRequestPermissionRationale(
56
-            androidPermission,
57
-          ).then(shouldShow => (shouldShow ? 'denied' : 'restricted'))
55
+    return PermissionsAndroid.check(permissionTypes[permission]).then(
56
+      isAuthorized => {
57
+        if (isAuthorized) {
58
+          return 'authorized'
58
         }
59
         }
59
-        return 'undetermined'
60
-      })
61
-    })
62
-  }
63
 
60
 
64
-  request = (permission, options) => {
65
-    const androidPermission = permissionTypes[permission]
61
+        return getDidAskOnce(permission).then(didAsk => {
62
+          if (didAsk) {
63
+            return NativeModules.PermissionsAndroid.shouldShowRequestPermissionRationale(
64
+              permissionTypes[permission],
65
+            ).then(shouldShow => (shouldShow ? 'denied' : 'restricted'))
66
+          }
67
+
68
+          return 'undetermined'
69
+        })
70
+      },
71
+    )
72
+  }
66
 
73
 
67
-    if (!androidPermission) {
68
-      return Promise.reject(
74
+  request = (permission: string, options?: Options): Promise<Status> => {
75
+    if (!permissionTypes[permission]) {
76
+      const error = new Error(
69
         `ReactNativePermissions: ${
77
         `ReactNativePermissions: ${
70
           permission
78
           permission
71
         } is not a valid permission type on Android`,
79
         } is not a valid permission type on Android`,
72
       )
80
       )
81
+
82
+      return Promise.reject(error)
73
     }
83
     }
74
 
84
 
75
-    let rationale = null
76
-    if (options != null) {
85
+    let rationale
86
+
87
+    if (options && options.rationale) {
77
       rationale = options.rationale
88
       rationale = options.rationale
78
     }
89
     }
79
 
90
 
80
-    return PermissionsAndroid.request(androidPermission, rationale).then(
81
-      result => {
82
-        // PermissionsAndroid.request() to native module resolves to boolean
83
-        // rather than string if running on OS version prior to Android M
84
-        if (typeof result === 'boolean') {
85
-          return result ? 'authorized' : 'denied'
86
-        }
91
+    return PermissionsAndroid.request(
92
+      permissionTypes[permission],
93
+      rationale,
94
+    ).then(result => {
95
+      // PermissionsAndroid.request() to native module resolves to boolean
96
+      // rather than string if running on OS version prior to Android M
97
+      if (typeof result === 'boolean') {
98
+        return result ? 'authorized' : 'denied'
99
+      }
87
 
100
 
88
-        return setDidAskOnce(permission).then(() => RESULTS[result])
89
-      },
90
-    )
101
+      return setDidAskOnce(permission).then(() => RESULTS[result])
102
+    })
91
   }
103
   }
92
 
104
 
93
-  checkMultiple = permissions =>
94
-    Promise.all(permissions.map(this.check)).then(result =>
95
-      result.reduce((acc, value, index) => {
96
-        const name = permissions[index]
97
-        acc[name] = value
98
-        return acc
99
-      }, {}),
105
+  checkMultiple = (permissions: Array<string>): Promise<{ [string]: string }> =>
106
+    Promise.all(permissions.map(permission => this.check(permission))).then(
107
+      result =>
108
+        result.reduce((acc, value, index) => {
109
+          const name = permissions[index]
110
+          acc[name] = value
111
+          return acc
112
+        }, {}),
100
     )
113
     )
101
 }
114
 }
102
 
115
 

+ 26
- 15
lib/permissions.ios.js View File

3
 import { NativeModules } from 'react-native'
3
 import { NativeModules } from 'react-native'
4
 const PermissionsIOS = NativeModules.ReactNativePermissions
4
 const PermissionsIOS = NativeModules.ReactNativePermissions
5
 
5
 
6
+type Status = 'authorized' | 'denied' | 'restricted' | 'undetermined'
7
+type Rationale = { title: string, message: string }
8
+type Options = string | { type: string, rationale?: Rationale }
9
+
6
 const permissionTypes = [
10
 const permissionTypes = [
7
   'location',
11
   'location',
8
   'camera',
12
   'camera',
23
 }
27
 }
24
 
28
 
25
 class ReactNativePermissions {
29
 class ReactNativePermissions {
26
-  canOpenSettings = () => PermissionsIOS.canOpenSettings()
27
-  openSettings = () => PermissionsIOS.openSettings()
28
-  getTypes = () => permissionTypes
30
+  canOpenSettings: () => Promise<boolean> = () =>
31
+    PermissionsIOS.canOpenSettings()
32
+
33
+  openSettings: () => Promise<*> = () => PermissionsIOS.openSettings()
29
 
34
 
30
-  check = (permission, type) => {
35
+  getTypes: () => Array<string> = () => permissionTypes
36
+
37
+  check = (permission: string, type?: string): Promise<Status> => {
31
     if (!permissionTypes.includes(permission)) {
38
     if (!permissionTypes.includes(permission)) {
32
-      return Promise.reject(
39
+      const error = new Error(
33
         `ReactNativePermissions: ${
40
         `ReactNativePermissions: ${
34
           permission
41
           permission
35
         } is not a valid permission type on iOS`,
42
         } is not a valid permission type on iOS`,
36
       )
43
       )
44
+
45
+      return Promise.reject(error)
37
     }
46
     }
38
 
47
 
39
     return PermissionsIOS.getPermissionStatus(
48
     return PermissionsIOS.getPermissionStatus(
42
     )
51
     )
43
   }
52
   }
44
 
53
 
45
-  request = (permission, options) => {
46
-    let type = null
47
-    if (typeof options === 'string' || options instanceof Array) {
48
-      console.warn(
49
-        '[react-native-permissions] : You are using a deprecated version of request(). You should use an object as second parameter. Please check the documentation for more information : https://github.com/yonahforst/react-native-permissions',
50
-      )
54
+  request = (permission: string, options?: Options): Promise<Status> => {
55
+    let type
56
+
57
+    if (typeof options === 'string') {
51
       type = options
58
       type = options
52
-    } else if (options != null) {
59
+    } else if (options && options.type) {
53
       type = options.type
60
       type = options.type
54
     }
61
     }
55
 
62
 
56
     if (!permissionTypes.includes(permission)) {
63
     if (!permissionTypes.includes(permission)) {
57
-      return Promise.reject(
64
+      const error = new Error(
58
         `ReactNativePermissions: ${
65
         `ReactNativePermissions: ${
59
           permission
66
           permission
60
         } is not a valid permission type on iOS`,
67
         } is not a valid permission type on iOS`,
61
       )
68
       )
69
+
70
+      return Promise.reject(error)
62
     }
71
     }
63
 
72
 
64
     if (permission == 'backgroundRefresh') {
73
     if (permission == 'backgroundRefresh') {
65
-      return Promise.reject(
74
+      const error = new Error(
66
         'ReactNativePermissions: You cannot request backgroundRefresh',
75
         'ReactNativePermissions: You cannot request backgroundRefresh',
67
       )
76
       )
77
+
78
+      return Promise.reject(error)
68
     }
79
     }
69
 
80
 
70
     return PermissionsIOS.requestPermission(
81
     return PermissionsIOS.requestPermission(
73
     )
84
     )
74
   }
85
   }
75
 
86
 
76
-  checkMultiple = permissions =>
87
+  checkMultiple = (permissions: Array<string>): Promise<{ [string]: string }> =>
77
     Promise.all(permissions.map(permission => this.check(permission))).then(
88
     Promise.all(permissions.map(permission => this.check(permission))).then(
78
       result =>
89
       result =>
79
         result.reduce((acc, value, index) => {
90
         result.reduce((acc, value, index) => {

+ 1
- 0
package.json View File

19
     "**/*.{js,json,md}": ["prettier --write", "git add"]
19
     "**/*.{js,json,md}": ["prettier --write", "git add"]
20
   },
20
   },
21
   "devDependencies": {
21
   "devDependencies": {
22
+    "flow-bin": "^0.57.3",
22
     "husky": "^0.14.3",
23
     "husky": "^0.14.3",
23
     "lint-staged": "^5.0.0",
24
     "lint-staged": "^5.0.0",
24
     "prettier": "^1.8.2"
25
     "prettier": "^1.8.2"

+ 10
- 6
yarn.lock View File

103
   resolved "https://registry.yarnpkg.com/color-name/-/color-name-1.1.3.tgz#a7d0558bd89c42f795dd42328f740831ca53bc25"
103
   resolved "https://registry.yarnpkg.com/color-name/-/color-name-1.1.3.tgz#a7d0558bd89c42f795dd42328f740831ca53bc25"
104
 
104
 
105
 commander@^2.11.0, commander@^2.9.0:
105
 commander@^2.11.0, commander@^2.9.0:
106
-  version "2.11.0"
107
-  resolved "https://registry.yarnpkg.com/commander/-/commander-2.11.0.tgz#157152fd1e7a6c8d98a5b715cf376df928004563"
106
+  version "2.12.2"
107
+  resolved "https://registry.yarnpkg.com/commander/-/commander-2.12.2.tgz#0f5946c427ed9ec0d91a46bb9def53e54650e555"
108
 
108
 
109
 concat-map@0.0.1:
109
 concat-map@0.0.1:
110
   version "0.0.1"
110
   version "0.0.1"
180
   version "0.3.0"
180
   version "0.3.0"
181
   resolved "https://registry.yarnpkg.com/find-parent-dir/-/find-parent-dir-0.3.0.tgz#33c44b429ab2b2f0646299c5f9f718f376ff8d54"
181
   resolved "https://registry.yarnpkg.com/find-parent-dir/-/find-parent-dir-0.3.0.tgz#33c44b429ab2b2f0646299c5f9f718f376ff8d54"
182
 
182
 
183
+flow-bin@^0.57.3:
184
+  version "0.57.3"
185
+  resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.57.3.tgz#843fb80a821b6d0c5847f7bb3f42365ffe53b27b"
186
+
183
 get-own-enumerable-property-symbols@^2.0.1:
187
 get-own-enumerable-property-symbols@^2.0.1:
184
   version "2.0.1"
188
   version "2.0.1"
185
   resolved "https://registry.yarnpkg.com/get-own-enumerable-property-symbols/-/get-own-enumerable-property-symbols-2.0.1.tgz#5c4ad87f2834c4b9b4e84549dc1e0650fb38c24b"
189
   resolved "https://registry.yarnpkg.com/get-own-enumerable-property-symbols/-/get-own-enumerable-property-symbols-2.0.1.tgz#5c4ad87f2834c4b9b4e84549dc1e0650fb38c24b"
514
     onetime "^1.0.0"
518
     onetime "^1.0.0"
515
 
519
 
516
 rxjs@^5.4.2:
520
 rxjs@^5.4.2:
517
-  version "5.5.2"
518
-  resolved "https://registry.yarnpkg.com/rxjs/-/rxjs-5.5.2.tgz#28d403f0071121967f18ad665563255d54236ac3"
521
+  version "5.5.3"
522
+  resolved "https://registry.yarnpkg.com/rxjs/-/rxjs-5.5.3.tgz#b62227e74b84f4e77bdf440e50b5ee01a1bc7dcd"
519
   dependencies:
523
   dependencies:
520
     symbol-observable "^1.0.1"
524
     symbol-observable "^1.0.1"
521
 
525
 
596
   resolved "https://registry.yarnpkg.com/symbol-observable/-/symbol-observable-0.2.4.tgz#95a83db26186d6af7e7a18dbd9760a2f86d08f40"
600
   resolved "https://registry.yarnpkg.com/symbol-observable/-/symbol-observable-0.2.4.tgz#95a83db26186d6af7e7a18dbd9760a2f86d08f40"
597
 
601
 
598
 symbol-observable@^1.0.1:
602
 symbol-observable@^1.0.1:
599
-  version "1.0.4"
600
-  resolved "https://registry.yarnpkg.com/symbol-observable/-/symbol-observable-1.0.4.tgz#29bf615d4aa7121bdd898b22d4b3f9bc4e2aa03d"
603
+  version "1.1.0"
604
+  resolved "https://registry.yarnpkg.com/symbol-observable/-/symbol-observable-1.1.0.tgz#5c68fd8d54115d9dfb72a84720549222e8db9b32"
601
 
605
 
602
 which@^1.2.10, which@^1.2.9:
606
 which@^1.2.10, which@^1.2.9:
603
   version "1.3.0"
607
   version "1.3.0"