ES6 and general API alignment
This commit is contained in:
parent
dde75d895a
commit
c45e210102
212
index.ios.js
212
index.ios.js
|
@ -1,21 +1,54 @@
|
|||
var React = require('react-native');
|
||||
var { StyleSheet, requireNativeComponent, PropTypes, NativeModules, DeviceEventEmitter } = React;
|
||||
import React, {
|
||||
Component,
|
||||
NativeAppEventEmitter,
|
||||
NativeModules,
|
||||
PropTypes,
|
||||
StyleSheet,
|
||||
requireNativeComponent
|
||||
}
|
||||
|
||||
var CAMERA_REF = 'camera';
|
||||
const CameraManager = NativeModules.CameraManager;
|
||||
const CAMERA_REF = 'camera';
|
||||
|
||||
var constants = {
|
||||
Aspect: NativeModules.CameraManager.Aspect,
|
||||
BarCodeType: NativeModules.CameraManager.BarCodeType,
|
||||
Type: NativeModules.CameraManager.Type,
|
||||
CaptureMode: NativeModules.CameraManager.CaptureMode,
|
||||
CaptureTarget: NativeModules.CameraManager.CaptureTarget,
|
||||
Orientation: NativeModules.CameraManager.Orientation,
|
||||
FlashMode: NativeModules.CameraManager.FlashMode,
|
||||
TorchMode: NativeModules.CameraManager.TorchMode
|
||||
function convertStringProps(props) {
|
||||
const newProps = { ...props };
|
||||
if (typeof props.aspect === 'string') {
|
||||
newProps.aspect = constants.Aspect[aspect];
|
||||
}
|
||||
|
||||
if (typeof props.flashMode === 'string') {
|
||||
newProps.flashMode = constants.FlashMode[flashMode];
|
||||
}
|
||||
|
||||
if (typeof props.orientation === 'string') {
|
||||
newProps.orientation = constants.Orientation[orientation];
|
||||
}
|
||||
|
||||
if (typeof props.torchMode === 'string') {
|
||||
newProps.torchMode = constants.TorchMode[torchMode];
|
||||
}
|
||||
|
||||
if (typeof props.type === 'string') {
|
||||
newProps.type = constants.Type[type];
|
||||
}
|
||||
|
||||
return newProps;
|
||||
}
|
||||
|
||||
export default class Camera extends Component {
|
||||
|
||||
static constants = {
|
||||
Aspect: CameraManager.Aspect,
|
||||
BarCodeType: CameraManager.BarCodeType,
|
||||
Type: CameraManager.Type,
|
||||
CaptureMode: CameraManager.CaptureMode,
|
||||
CaptureTarget: CameraManager.CaptureTarget,
|
||||
Orientation: CameraManager.Orientation,
|
||||
FlashMode: CameraManager.FlashMode,
|
||||
TorchMode: CameraManager.TorchMode
|
||||
};
|
||||
|
||||
var Camera = React.createClass({
|
||||
propTypes: {
|
||||
static propTypes = {
|
||||
aspect: PropTypes.oneOfType([
|
||||
PropTypes.string,
|
||||
PropTypes.number
|
||||
|
@ -45,17 +78,12 @@ var Camera = React.createClass({
|
|||
PropTypes.string,
|
||||
PropTypes.number
|
||||
]),
|
||||
defaultOnFocusComponent: PropTypes.bool,
|
||||
onBarCodeRead: PropTypes.func,
|
||||
onFocusChanged: PropTypes.func,
|
||||
onZoomChanged: PropTypes.func
|
||||
},
|
||||
};
|
||||
|
||||
setNativeProps(props) {
|
||||
this.refs[CAMERA_REF].setNativeProps(props);
|
||||
},
|
||||
|
||||
getDefaultProps() {
|
||||
return {
|
||||
static defaultProps = {
|
||||
aspect: constants.Aspect.fill,
|
||||
type: constants.Type.back,
|
||||
orientation: constants.Orientation.auto,
|
||||
|
@ -63,24 +91,32 @@ var Camera = React.createClass({
|
|||
captureMode: constants.CaptureMode.still,
|
||||
captureTarget: constants.CaptureTarget.cameraRoll,
|
||||
flashMode: constants.FlashMode.off,
|
||||
torchMode: constants.TorchMode.off
|
||||
torchMode: constants.TorchMode.off,
|
||||
onBarCodeRead: () => {},
|
||||
onFocusChanged: () => {},
|
||||
onZoomChanged: () => {}
|
||||
};
|
||||
},
|
||||
|
||||
getInitialState() {
|
||||
return {
|
||||
static checkDeviceAuthorizationStatus = CameraManager.checkDeviceAuthorizationStatus;
|
||||
|
||||
setNativeProps(props) {
|
||||
this.refs[CAMERA_REF].setNativeProps(props);
|
||||
}
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this.state = {
|
||||
isAuthorized: false,
|
||||
isRecording: false
|
||||
};
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
componentWillMount() {
|
||||
NativeModules.CameraManager.checkDeviceAuthorizationStatus().then(
|
||||
isAuthorized => this.setState({ isAuthorized })
|
||||
);
|
||||
async componentWillMount() {
|
||||
const isAuthorized = await CameraManager.checkDeviceAuthorizationStatus();
|
||||
this.setState({ isAuthorized });
|
||||
|
||||
this.cameraBarCodeReadListener = DeviceEventEmitter.addListener('CameraBarCodeRead', this._onBarCodeRead);
|
||||
},
|
||||
this.cameraBarCodeReadListener = NativeAppEventEmitter.addListener('CameraBarCodeRead', this.props.onBarCodeRead);
|
||||
}
|
||||
|
||||
componentWillUnmount() {
|
||||
this.cameraBarCodeReadListener.remove();
|
||||
|
@ -88,81 +124,23 @@ var Camera = React.createClass({
|
|||
if (this.state.isRecording) {
|
||||
this.stopCapture();
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
render() {
|
||||
var style = [styles.base, this.props.style];
|
||||
|
||||
var aspect = this.props.aspect,
|
||||
type = this.props.type,
|
||||
orientation = this.props.orientation,
|
||||
flashMode = this.props.flashMode,
|
||||
torchMode = this.props.torchMode;
|
||||
|
||||
var legacyProps = {
|
||||
aspect: {
|
||||
Fill: 'fill',
|
||||
Fit: 'fit',
|
||||
Stretch: 'stretch'
|
||||
},
|
||||
orientation: {
|
||||
LandscapeLeft: 'landscapeLeft',
|
||||
LandscapeRight: 'landscapeRight',
|
||||
Portrait: 'portrait',
|
||||
PortraitUpsideDown: 'portraitUpsideDown'
|
||||
},
|
||||
type: {
|
||||
Front: 'front',
|
||||
Back: 'back'
|
||||
}
|
||||
};
|
||||
|
||||
if (typeof aspect === 'string') {
|
||||
aspect = constants.Aspect[aspect];
|
||||
}
|
||||
|
||||
if (typeof flashMode === 'string') {
|
||||
flashMode = constants.FlashMode[flashMode];
|
||||
}
|
||||
|
||||
if (typeof orientation === 'string') {
|
||||
orientation = constants.Orientation[orientation];
|
||||
}
|
||||
|
||||
if (typeof torchMode === 'string') {
|
||||
torchMode = constants.TorchMode[torchMode];
|
||||
}
|
||||
|
||||
if (typeof type === 'string') {
|
||||
type = constants.Type[type];
|
||||
}
|
||||
|
||||
var nativeProps = Object.assign({}, this.props, {
|
||||
style,
|
||||
aspect: aspect,
|
||||
type: type,
|
||||
orientation: orientation,
|
||||
flashMode: flashMode,
|
||||
torchMode: torchMode
|
||||
});
|
||||
const style = [styles.base, this.props.style];
|
||||
const nativeProps = convertStringProps(this.props);
|
||||
|
||||
return <RCTCamera ref={CAMERA_REF} {...nativeProps} />;
|
||||
},
|
||||
|
||||
_onBarCodeRead(e) {
|
||||
this.props.onBarCodeRead && this.props.onBarCodeRead(e);
|
||||
},
|
||||
}
|
||||
|
||||
capture(options) {
|
||||
options = Object.assign({}, {
|
||||
audio: this.props.captureAudio,
|
||||
mode: this.props.captureMode,
|
||||
target: this.props.captureTarget
|
||||
}, options);
|
||||
|
||||
if (typeof options.mode === 'string') {
|
||||
options.mode = constants.CaptureMode[options.mode];
|
||||
}
|
||||
const props = convertStringProps(this.props);
|
||||
options = {
|
||||
audio: props.captureAudio,
|
||||
mode: props.captureMode,
|
||||
target: props.captureTarget,
|
||||
...options
|
||||
};
|
||||
|
||||
if (options.mode === constants.CaptureMode.video) {
|
||||
options.totalSeconds = (options.totalSeconds > -1 ? options.totalSeconds : -1);
|
||||
|
@ -170,33 +148,27 @@ var Camera = React.createClass({
|
|||
this.setState({ isRecording: true });
|
||||
}
|
||||
|
||||
if (typeof options.target === 'string') {
|
||||
options.target = constants.CaptureTarget[options.target];
|
||||
return CameraManager.capture(options);
|
||||
}
|
||||
|
||||
return NativeModules.CameraManager.capture(options);
|
||||
},
|
||||
|
||||
stopCapture() {
|
||||
if (this.state.isRecording) {
|
||||
NativeModules.CameraManager.stopCapture();
|
||||
CameraManager.stopCapture();
|
||||
this.setState({ isRecording: false });
|
||||
}
|
||||
},
|
||||
|
||||
hasFlash(callback) {
|
||||
NativeModules.CameraManager.hasFlash(callback);
|
||||
}
|
||||
|
||||
getFOV() {
|
||||
return CameraManager.getFOV();
|
||||
}
|
||||
|
||||
hasFlash() {
|
||||
return CameraManager.hasFlash();
|
||||
}
|
||||
});
|
||||
|
||||
var RCTCamera = requireNativeComponent('RCTCamera', Camera);
|
||||
const RCTCamera = requireNativeComponent('RCTCamera', Camera);
|
||||
|
||||
var styles = StyleSheet.create({
|
||||
const styles = StyleSheet.create({
|
||||
base: {},
|
||||
});
|
||||
|
||||
Camera.constants = constants;
|
||||
Camera.checkDeviceAuthorizationStatus = NativeModules.CameraManager.checkDeviceAuthorizationStatus
|
||||
|
||||
module.exports = Camera;
|
||||
|
|
|
@ -71,6 +71,8 @@ typedef NS_ENUM(NSInteger, RCTCameraTorchMode) {
|
|||
- (void)changeTorchMode:(NSInteger)torchMode;
|
||||
- (AVCaptureDevice *)deviceWithMediaType:(NSString *)mediaType preferringPosition:(AVCaptureDevicePosition)position;
|
||||
- (void)capture:(NSDictionary*)options resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject;
|
||||
- (void)getFOV:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject;
|
||||
- (void)hasFlash:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject;
|
||||
- (void)initializeCaptureSessionInput:(NSString*)type;
|
||||
- (void)stopCapture;
|
||||
- (void)startSession;
|
||||
|
|
|
@ -141,12 +141,12 @@ RCT_EXPORT_METHOD(checkDeviceAuthorizationStatus:(RCTPromiseResolveBlock)resolve
|
|||
|
||||
[AVCaptureDevice requestAccessForMediaType:mediaType completionHandler:^(BOOL granted) {
|
||||
if (!granted) {
|
||||
resolve(@[@(granted)]);
|
||||
resolve(@(granted));
|
||||
}
|
||||
else {
|
||||
mediaType = AVMediaTypeAudio;
|
||||
[AVCaptureDevice requestAccessForMediaType:mediaType completionHandler:^(BOOL granted) {
|
||||
resolve(@[@(granted)]);
|
||||
resolve(@(granted));
|
||||
}];
|
||||
}
|
||||
}];
|
||||
|
@ -523,7 +523,7 @@ RCT_EXPORT_METHOD(hasFlash:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRej
|
|||
else if (target == RCTCameraCaptureTargetCameraRoll) {
|
||||
[[[ALAssetsLibrary alloc] init] writeImageDataToSavedPhotosAlbum:imageData metadata:metadata completionBlock:^(NSURL* url, NSError* error) {
|
||||
if (error == nil) {
|
||||
resolve(@[[url absoluteString]]);
|
||||
resolve([url absoluteString]);
|
||||
}
|
||||
else {
|
||||
reject(RCTErrorUnspecified, nil, RCTErrorWithMessage(error.description));
|
||||
|
@ -531,7 +531,7 @@ RCT_EXPORT_METHOD(hasFlash:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRej
|
|||
}];
|
||||
return;
|
||||
}
|
||||
resolve(@[responseString]);
|
||||
resolve(responseString);
|
||||
}
|
||||
|
||||
- (CGImageRef)newCGImageRotatedByAngle:(CGImageRef)imgRef angle:(CGFloat)angle
|
||||
|
@ -650,7 +650,7 @@ didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL
|
|||
self.videoReject(RCTErrorUnspecified, nil, RCTErrorWithMessage(error.description));
|
||||
return;
|
||||
}
|
||||
self.videoResolve(@[fullPath]);
|
||||
self.videoResolve(fullPath);
|
||||
}
|
||||
else if (self.videoTarget == RCTCameraCaptureTargetTemp) {
|
||||
NSString *fileName = [[NSProcessInfo processInfo] globallyUniqueString];
|
||||
|
@ -664,7 +664,7 @@ didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL
|
|||
self.videoReject(RCTErrorUnspecified, nil, RCTErrorWithMessage(error.description));
|
||||
return;
|
||||
}
|
||||
self.videoResolve(@[fullPath]);
|
||||
self.videoResolve(fullPath);
|
||||
}
|
||||
else {
|
||||
self.videoReject(RCTErrorUnspecified, nil, RCTErrorWithMessage(@"Target not supported"));
|
||||
|
@ -677,8 +677,7 @@ didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL
|
|||
for (id barcodeType in [self getBarCodeTypes]) {
|
||||
if (metadata.type == barcodeType) {
|
||||
|
||||
[self.bridge.eventDispatcher sendDeviceEventWithName:@"CameraBarCodeRead"
|
||||
body:@{
|
||||
NSDictionary *event = @{
|
||||
@"type": metadata.type,
|
||||
@"data": metadata.stringValue,
|
||||
@"bounds": @{
|
||||
|
@ -691,7 +690,9 @@ didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL
|
|||
@"width": [NSString stringWithFormat:@"%f", metadata.bounds.size.width],
|
||||
}
|
||||
}
|
||||
}];
|
||||
};
|
||||
|
||||
[self.bridge.eventDispatcher sendAppEventWithName:@"CameraBarCodeRead" body:event];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -805,6 +806,7 @@ didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL
|
|||
@"zoomFactor": [NSNumber numberWithDouble:zoomFactor],
|
||||
@"velocity": [NSNumber numberWithDouble:velocity]
|
||||
};
|
||||
|
||||
[self.bridge.eventDispatcher sendInputEventWithName:@"zoomChanged" body:event];
|
||||
|
||||
device.videoZoomFactor = zoomFactor;
|
||||
|
@ -814,7 +816,4 @@ didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@end
|
||||
|
|
Loading…
Reference in New Issue