Bläddra i källkod

Add background compatibity for type options

Mathieu Acthernoene 7 år sedan
förälder
incheckning
06a834dd0e
5 ändrade filer med 105 tillägg och 67 borttagningar
  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 Visa fil

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

+ 59
- 46
lib/permissions.android.js Visa fil

@@ -2,6 +2,10 @@
2 2
 
3 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 9
 const permissionTypes = {
6 10
   location: PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
7 11
   camera: PermissionsAndroid.PERMISSIONS.CAMERA,
@@ -23,80 +27,89 @@ const RESULTS = {
23 27
 
24 28
 const STORAGE_KEY = '@RNPermissions:didAskPermission:'
25 29
 
26
-const setDidAskOnce = permission =>
30
+const setDidAskOnce = (permission: string) =>
27 31
   AsyncStorage.setItem(STORAGE_KEY + permission, 'true')
28 32
 
29
-const getDidAskOnce = permission =>
33
+const getDidAskOnce = (permission: string) =>
30 34
   AsyncStorage.getItem(STORAGE_KEY + permission).then(item => !!item)
31 35
 
32 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 47
         `ReactNativePermissions: ${
43 48
           permission
44 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 77
         `ReactNativePermissions: ${
70 78
           permission
71 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 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 Visa fil

@@ -3,6 +3,10 @@
3 3
 import { NativeModules } from 'react-native'
4 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 10
 const permissionTypes = [
7 11
   'location',
8 12
   'camera',
@@ -23,17 +27,22 @@ const DEFAULTS = {
23 27
 }
24 28
 
25 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 38
     if (!permissionTypes.includes(permission)) {
32
-      return Promise.reject(
39
+      const error = new Error(
33 40
         `ReactNativePermissions: ${
34 41
           permission
35 42
         } is not a valid permission type on iOS`,
36 43
       )
44
+
45
+      return Promise.reject(error)
37 46
     }
38 47
 
39 48
     return PermissionsIOS.getPermissionStatus(
@@ -42,29 +51,31 @@ class ReactNativePermissions {
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 58
       type = options
52
-    } else if (options != null) {
59
+    } else if (options && options.type) {
53 60
       type = options.type
54 61
     }
55 62
 
56 63
     if (!permissionTypes.includes(permission)) {
57
-      return Promise.reject(
64
+      const error = new Error(
58 65
         `ReactNativePermissions: ${
59 66
           permission
60 67
         } is not a valid permission type on iOS`,
61 68
       )
69
+
70
+      return Promise.reject(error)
62 71
     }
63 72
 
64 73
     if (permission == 'backgroundRefresh') {
65
-      return Promise.reject(
74
+      const error = new Error(
66 75
         'ReactNativePermissions: You cannot request backgroundRefresh',
67 76
       )
77
+
78
+      return Promise.reject(error)
68 79
     }
69 80
 
70 81
     return PermissionsIOS.requestPermission(
@@ -73,7 +84,7 @@ class ReactNativePermissions {
73 84
     )
74 85
   }
75 86
 
76
-  checkMultiple = permissions =>
87
+  checkMultiple = (permissions: Array<string>): Promise<{ [string]: string }> =>
77 88
     Promise.all(permissions.map(permission => this.check(permission))).then(
78 89
       result =>
79 90
         result.reduce((acc, value, index) => {

+ 1
- 0
package.json Visa fil

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

+ 10
- 6
yarn.lock Visa fil

@@ -103,8 +103,8 @@ color-name@^1.1.1:
103 103
   resolved "https://registry.yarnpkg.com/color-name/-/color-name-1.1.3.tgz#a7d0558bd89c42f795dd42328f740831ca53bc25"
104 104
 
105 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 109
 concat-map@0.0.1:
110 110
   version "0.0.1"
@@ -180,6 +180,10 @@ find-parent-dir@^0.3.0:
180 180
   version "0.3.0"
181 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 187
 get-own-enumerable-property-symbols@^2.0.1:
184 188
   version "2.0.1"
185 189
   resolved "https://registry.yarnpkg.com/get-own-enumerable-property-symbols/-/get-own-enumerable-property-symbols-2.0.1.tgz#5c4ad87f2834c4b9b4e84549dc1e0650fb38c24b"
@@ -514,8 +518,8 @@ restore-cursor@^1.0.1:
514 518
     onetime "^1.0.0"
515 519
 
516 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 523
   dependencies:
520 524
     symbol-observable "^1.0.1"
521 525
 
@@ -596,8 +600,8 @@ symbol-observable@^0.2.2:
596 600
   resolved "https://registry.yarnpkg.com/symbol-observable/-/symbol-observable-0.2.4.tgz#95a83db26186d6af7e7a18dbd9760a2f86d08f40"
597 601
 
598 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 606
 which@^1.2.10, which@^1.2.9:
603 607
   version "1.3.0"