Quellcode durchsuchen

react-native: refactor `takePictureWithExpo` (#2946)

Antoine du Hamel vor 3 Jahren
Ursprung
Commit
2c996d7043

+ 7 - 19
packages/@uppy/react-native/file-picker/selectImage.js

@@ -1,25 +1,13 @@
-import * as Permissions from 'expo-permissions'
+// Using leading underscore so eslint compat plugin doesn't yell at us.
+import * as _Permissions from 'expo-permissions'
 import * as ImagePicker from 'expo-image-picker'
 
 function selectImageWithExpo (options) {
-  return new Promise((resolve, reject) => {
-    // This is a different `Permissions` object than eslint-plugin-compat thinks it is
-    // eslint-disable-next-line compat/compat
-    return Permissions.askAsync(Permissions.CAMERA_ROLL)
-      .then((isAllowed) => {
-        if (!isAllowed) {
-          return reject(new Error('Permissions denied'))
-        }
-
-        return ImagePicker.launchImageLibraryAsync(options)
-          .then((result) => {
-            console.log(result)
-            if (!result.cancelled) {
-              return resolve(result)
-            }
-          })
-      })
-  })
+  return _Permissions.askAsync(_Permissions.CAMERA_ROLL)
+    .then((isAllowed) => (isAllowed ? ImagePicker.launchImageLibraryAsync(options)
+      : Promise.reject(new Error('Permissions denied'))))
+    .then((result) => (!result.cancelled ? result
+      : Promise.reject(new Error('Operation cancelled'))))
 }
 
 export default selectImageWithExpo

+ 8 - 18
packages/@uppy/react-native/file-picker/takePicture.js

@@ -1,23 +1,13 @@
-import * as Permissions from 'expo-permissions'
+// Using leading underscore so eslint compat plugin doesn't yell at us.
+import * as _Permissions from 'expo-permissions'
 import * as ImagePicker from 'expo-image-picker'
 
-function takePictureWithExpo (options) {
-  return new Promise((resolve, reject) => {
-    // This is a different `Permissions` object than eslint-plugin-compat thinks it is
-    // eslint-disable-next-line compat/compat
-    return Permissions.askAsync(Permissions.CAMERA).then((isAllowed) => {
-      if (!isAllowed) {
-        return reject(new Error('Permissions denied'))
-      }
-
-      return ImagePicker.launchCameraAsync({ allowsEditing: true })
-        .then((result) => {
-          if (!result.cancelled) {
-            return resolve(result)
-          }
-        })
-    })
-  })
+function takePictureWithExpo () {
+  return _Permissions.askAsync(_Permissions.CAMERA)
+    .then((isAllowed) => (isAllowed ? ImagePicker.launchCameraAsync({ allowsEditing: true })
+      : Promise.reject(new Error('Permissions denied'))))
+    .then((result) => (!result.cancelled ? result
+      : Promise.reject(new Error('Operation cancelled'))))
 }
 
 export default takePictureWithExpo