Kaynağa Gözat

Use ES modules, improve naming

Mathieu Acthernoene 7 yıl önce
ebeveyn
işleme
337784bbfd
2 değiştirilmiş dosya ile 79 ekleme ve 93 silme
  1. 56
    60
      index.android.js
  2. 23
    33
      index.ios.js

+ 56
- 60
index.android.js Dosyayı Görüntüle

@@ -1,100 +1,96 @@
1
-'use strict'
2
-
3
-const ReactNative = require('react-native')
4
-const RNPermissions = ReactNative.PermissionsAndroid
5
-const AsyncStorage = ReactNative.AsyncStorage
6
-
7
-const RNPTypes = {
8
-  location: RNPermissions.PERMISSIONS.ACCESS_FINE_LOCATION,
9
-  camera: RNPermissions.PERMISSIONS.CAMERA,
10
-  microphone: RNPermissions.PERMISSIONS.RECORD_AUDIO,
11
-  contacts: RNPermissions.PERMISSIONS.READ_CONTACTS,
12
-  event: RNPermissions.PERMISSIONS.READ_CALENDAR,
13
-  storage: RNPermissions.PERMISSIONS.READ_EXTERNAL_STORAGE,
14
-  photo: RNPermissions.PERMISSIONS.READ_EXTERNAL_STORAGE,
15
-  callPhone: RNPermissions.PERMISSIONS.CALL_PHONE,
16
-  readSms: RNPermissions.PERMISSIONS.READ_SMS,
17
-  receiveSms: RNPermissions.PERMISSIONS.RECEIVE_SMS,
1
+// @flow
2
+
3
+import { AsyncStorage, PermissionsAndroid } from 'react-native'
4
+
5
+const permissionTypes = {
6
+  location: PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
7
+  camera: PermissionsAndroid.PERMISSIONS.CAMERA,
8
+  microphone: PermissionsAndroid.PERMISSIONS.RECORD_AUDIO,
9
+  contacts: PermissionsAndroid.PERMISSIONS.READ_CONTACTS,
10
+  event: PermissionsAndroid.PERMISSIONS.READ_CALENDAR,
11
+  storage: PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE,
12
+  photo: PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE,
13
+  callPhone: PermissionsAndroid.PERMISSIONS.CALL_PHONE,
14
+  readSms: PermissionsAndroid.PERMISSIONS.READ_SMS,
15
+  receiveSms: PermissionsAndroid.PERMISSIONS.RECEIVE_SMS,
18 16
 }
19 17
 
20 18
 const RESULTS = {
21
-  [RNPermissions.RESULTS.GRANTED]: 'authorized',
22
-  [RNPermissions.RESULTS.DENIED]: 'denied',
23
-  [RNPermissions.RESULTS.NEVER_ASK_AGAIN]: 'restricted',
19
+  [PermissionsAndroid.RESULTS.GRANTED]: 'authorized',
20
+  [PermissionsAndroid.RESULTS.DENIED]: 'denied',
21
+  [PermissionsAndroid.RESULTS.NEVER_ASK_AGAIN]: 'restricted',
24 22
 }
25 23
 
26 24
 const STORAGE_KEY = '@RNPermissions:didAskPermission:'
27 25
 
28
-const setDidAskOnce = p => AsyncStorage.setItem(STORAGE_KEY + p, 'true')
29
-const getDidAskOnce = p =>
30
-  AsyncStorage.getItem(STORAGE_KEY + p).then(res => !!res)
26
+const setDidAskOnce = permission =>
27
+  AsyncStorage.setItem(STORAGE_KEY + permission, 'true')
31 28
 
32
-class ReactNativePermissions {
33
-  canOpenSettings() {
34
-    return false
35
-  }
29
+const getDidAskOnce = permission =>
30
+  AsyncStorage.getItem(STORAGE_KEY + permission).then(item => !!item)
36 31
 
37
-  openSettings() {
38
-    return Promise.reject("'openSettings' is Depricated on android")
39
-  }
32
+class ReactNativePermissions {
33
+  canOpenSettings = () => false
34
+  openSettings = () => Promise.reject("'openSettings' is deprecated on android")
35
+  getTypes = () => Object.keys(permissionTypes)
40 36
 
41
-  getTypes() {
42
-    return Object.keys(RNPTypes)
43
-  }
37
+  check = permission => {
38
+    const androidPermission = permissionTypes[permission]
44 39
 
45
-  check(permission) {
46
-    const androidPermission = RNPTypes[permission]
47
-    if (!androidPermission)
40
+    if (!androidPermission) {
48 41
       return Promise.reject(
49 42
         `ReactNativePermissions: ${
50 43
           permission
51 44
         } is not a valid permission type on Android`,
52 45
       )
46
+    }
53 47
 
54
-    const shouldShowRationale =
55
-      ReactNative.NativeModules.PermissionsAndroid
56
-        .shouldShowRequestPermissionRationale
57
-
58
-    return RNPermissions.check(androidPermission).then(isAuthorized => {
59
-      if (isAuthorized) return 'authorized'
48
+    return PermissionsAndroid.check(androidPermission).then(isAuthorized => {
49
+      if (isAuthorized) {
50
+        return 'authorized'
51
+      }
60 52
 
61 53
       return getDidAskOnce(permission).then(didAsk => {
62 54
         if (didAsk) {
63
-          return shouldShowRationale(androidPermission).then(
64
-            shouldShow => (shouldShow ? 'denied' : 'restricted'),
65
-          )
55
+          return PermissionsAndroid.shouldShowRequestPermissionRationale(
56
+            androidPermission,
57
+          ).then(shouldShow => (shouldShow ? 'denied' : 'restricted'))
66 58
         }
67 59
         return 'undetermined'
68 60
       })
69 61
     })
70 62
   }
71 63
 
72
-  request(permission) {
73
-    const androidPermission = RNPTypes[permission]
74
-    if (!androidPermission)
64
+  request = permission => {
65
+    const androidPermission = permissionTypes[permission]
66
+
67
+    if (!androidPermission) {
75 68
       return Promise.reject(
76 69
         `ReactNativePermissions: ${
77 70
           permission
78 71
         } is not a valid permission type on Android`,
79 72
       )
73
+    }
80 74
 
81
-    return RNPermissions.request(androidPermission).then(res => {
82
-      // RNPermissions.request() to native module resolves to boolean
75
+    return PermissionsAndroid.request(androidPermission).then(result => {
76
+      // PermissionsAndroid.request() to native module resolves to boolean
83 77
       // rather than string if running on OS version prior to Android M
84
-      if (typeof res === 'boolean') return res ? 'authorized' : 'denied'
85
-      return setDidAskOnce(permission).then(() => RESULTS[res])
78
+      if (typeof result === 'boolean') {
79
+        return result ? 'authorized' : 'denied'
80
+      }
81
+
82
+      return setDidAskOnce(permission).then(() => RESULTS[result])
86 83
     })
87 84
   }
88 85
 
89
-  checkMultiple(permissions) {
90
-    return Promise.all(permissions.map(this.check.bind(this))).then(res =>
91
-      res.reduce((pre, cur, i) => {
92
-        var name = permissions[i]
93
-        pre[name] = cur
94
-        return pre
86
+  checkMultiple = permissions =>
87
+    Promise.all(permissions.map(this.check)).then(result =>
88
+      result.reduce((acc, value, index) => {
89
+        const name = permissions[index]
90
+        acc[name] = value
91
+        return acc
95 92
       }, {}),
96 93
     )
97
-  }
98 94
 }
99 95
 
100
-module.exports = new ReactNativePermissions()
96
+export default new ReactNativePermissions()

+ 23
- 33
index.ios.js Dosyayı Görüntüle

@@ -1,9 +1,9 @@
1
-'use strict'
1
+// @flow
2 2
 
3
-const ReactNative = require('react-native')
4
-const RNPermissions = ReactNative.NativeModules.ReactNativePermissions
3
+import { NativeModules } from 'react-native'
4
+const PermissionsIOS = NativeModules.ReactNativePermissions
5 5
 
6
-const RNPTypes = [
6
+const permissionTypes = [
7 7
   'location',
8 8
   'camera',
9 9
   'microphone',
@@ -23,20 +23,12 @@ const DEFAULTS = {
23 23
 }
24 24
 
25 25
 class ReactNativePermissions {
26
-  canOpenSettings() {
27
-    return RNPermissions.canOpenSettings()
28
-  }
29
-
30
-  openSettings() {
31
-    return RNPermissions.openSettings()
32
-  }
33
-
34
-  getTypes() {
35
-    return RNPTypes
36
-  }
26
+  canOpenSettings = () => PermissionsIOS.canOpenSettings()
27
+  openSettings = () => PermissionsIOS.openSettings()
28
+  getTypes = () => permissionTypes
37 29
 
38
-  check(permission, type) {
39
-    if (!RNPTypes.includes(permission)) {
30
+  check = (permission, type) => {
31
+    if (!permissionTypes.includes(permission)) {
40 32
       return Promise.reject(
41 33
         `ReactNativePermissions: ${
42 34
           permission
@@ -44,11 +36,11 @@ class ReactNativePermissions {
44 36
       )
45 37
     }
46 38
 
47
-    return RNPermissions.getPermissionStatus(permission, type)
39
+    return PermissionsIOS.getPermissionStatus(permission, type)
48 40
   }
49 41
 
50
-  request(permission, type) {
51
-    if (!RNPTypes.includes(permission)) {
42
+  request = (permission, type) => {
43
+    if (!permissionTypes.includes(permission)) {
52 44
       return Promise.reject(
53 45
         `ReactNativePermissions: ${
54 46
           permission
@@ -62,22 +54,20 @@ class ReactNativePermissions {
62 54
       )
63 55
     }
64 56
 
65
-    type = type || DEFAULTS[permission]
66
-
67
-    return RNPermissions.requestPermission(permission, type)
57
+    return PermissionsIOS.requestPermission(
58
+      permission,
59
+      type || DEFAULTS[permission],
60
+    )
68 61
   }
69 62
 
70
-  checkMultiple(permissions) {
71
-    return Promise.all(
72
-      permissions.map(permission => this.check(permission)),
73
-    ).then(res =>
74
-      res.reduce((pre, cur, i) => {
75
-        var name = permissions[i]
76
-        pre[name] = cur
77
-        return pre
63
+  checkMultiple = permissions =>
64
+    Promise.all(permissions.map(this.check)).then(result =>
65
+      result.reduce((acc, value, index) => {
66
+        const name = permissions[index]
67
+        acc[name] = value
68
+        return acc
78 69
       }, {}),
79 70
     )
80
-  }
81 71
 }
82 72
 
83
-module.exports = new ReactNativePermissions()
73
+export default new ReactNativePermissions()