Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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);
},
);
})

}