Skip to content

Commit

Permalink
feat(api): added methods to launch the image picker and camera as pro…
Browse files Browse the repository at this point in the history
…mise (#1873)

* feat(api): added methods to launch the image picker and camera as promise

* chore(adjust): adjusted readme to remove duplicate docs

* feat(api): added ability for launchCamera and launchImageLibrary to be ran as promises
  • Loading branch information
Luke Brandon Farrell committed Nov 15, 2021
1 parent 5376fe1 commit fde6838
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 23 deletions.
10 changes: 8 additions & 2 deletions README.md
Expand Up @@ -55,6 +55,9 @@ Launch camera to take photo or video.

```js
launchCamera(options?, callback);

// You can also use as a promise without 'callback':
const result = await launchCamera(options?);
```
See [Options](#options) for further information on `options`.
Expand All @@ -66,7 +69,10 @@ The `callback` will be called with a response object, refer to [The Response Obj
Launch gallery to pick image or video.
```js
launchImageLibrary(options?, callback);
launchImageLibrary(options?, callback)

// You can also use as a promise without 'callback':
const result = await launchImageLibrary(options?);
```
See [Options](#options) for further information on `options`.
Expand All @@ -84,7 +90,7 @@ The `callback` will be called with a response object, refer to [The Response Obj
| durationLimit | OK | OK | Video max duration in seconds |
| quality | OK | OK | 0 to 1, photos |
| cameraType | OK | OK | 'back' or 'front'. May not be supported in few android devices |
| includeBase64 | OK | OK | If true, creates base64 string of the image (Avoid using on large image |
| includeBase64 | OK | OK | If true, creates base64 string of the image (Avoid using on large image files due to performance) | |
| includeExtra | OK | OK | If true, will include extra data which requires library permissions to be requested (i.e. exif data) |
| saveToPhotos | OK | OK | (Boolean) Only for launchCamera, saves the image/video file captured to public photo |
| selectionLimit | OK | OK | Default is `1`, use `0` to allow any number of files. Only iOS version >= 14 support `0` and also it supports providing any integer value |
Expand Down
44 changes: 23 additions & 21 deletions src/index.ts
@@ -1,6 +1,6 @@
import {NativeModules} from 'react-native';

import {CameraOptions, ImageLibraryOptions, Callback} from './types';
import {CameraOptions, ImageLibraryOptions, Callback, ImagePickerResponse} from './types';
export * from './types';

const DEFAULT_OPTIONS: ImageLibraryOptions & CameraOptions = {
Expand All @@ -17,28 +17,30 @@ const DEFAULT_OPTIONS: ImageLibraryOptions & CameraOptions = {
includeExtra: false,
};

export function launchCamera(options: CameraOptions, callback: Callback) {
if (typeof callback !== 'function') {
console.error("Send proper callback function, check API");
return;
}

NativeModules.ImagePickerManager.launchCamera(
{...DEFAULT_OPTIONS, ...options},
callback,
);
export function launchCamera(options: CameraOptions, callback?: Callback) : Promise<ImagePickerResponse> {
return new Promise(resolve => {
NativeModules.ImagePickerManager.launchCamera(
{...DEFAULT_OPTIONS, ...options},
(result: ImagePickerResponse) => {
if(callback) callback(result);
resolve(result);
},
);
});
}

export function launchImageLibrary(
options: ImageLibraryOptions,
callback: Callback,
) {
if (typeof callback !== 'function') {
console.error("Send proper callback function, check API");
return;
}
NativeModules.ImagePickerManager.launchImageLibrary(
{...DEFAULT_OPTIONS, ...options},
callback,
);
callback?: Callback,
) : Promise<ImagePickerResponse> {
return new Promise(resolve => {
NativeModules.ImagePickerManager.launchImageLibrary(
{...DEFAULT_OPTIONS, ...options},
(result: ImagePickerResponse) => {
if(callback) callback(result);
resolve(result);
},
);
})

}

0 comments on commit fde6838

Please sign in to comment.