From 45cc8f25d2de71b9eee29e1fe14e2f4f3d2feee9 Mon Sep 17 00:00:00 2001 From: AndreiCalazans Date: Wed, 25 Apr 2018 18:10:00 -0300 Subject: [PATCH 1/2] feat(rn_camera): add function as children --- docs/RNCamera.md | 95 ++++++++++++++++++++++++++++++++++++++++++++++++ src/RNCamera.js | 23 +++++++++++- 2 files changed, 116 insertions(+), 2 deletions(-) diff --git a/docs/RNCamera.md b/docs/RNCamera.md index 10355dd0d..35af236bb 100644 --- a/docs/RNCamera.md +++ b/docs/RNCamera.md @@ -78,6 +78,101 @@ const styles = StyleSheet.create({ AppRegistry.registerComponent('BadInstagramCloneApp', () => BadInstagramCloneApp); ``` +## FaCC (Function as Child Components) + +**You can also use it with Facc.* + +```javascript + 'use strict'; +import React, { Component } from 'react'; +import { StyleSheet, Text, TouchableOpacity, View } from 'react-native'; +import { RNCamera } from 'react-native-camera'; + +const PendingView = () => ( + + Waiting + +); + +class App extends Component { + render() { + return ( + + { + this.camera = ref; + }} + style={styles.preview} + type={RNCamera.Constants.Type.back} + flashMode={RNCamera.Constants.FlashMode.on} + permissionDialogTitle={'Permission to use camera'} + permissionDialogMessage={'We need your permission to use your camera phone'} + > + {({ camera, status }) => { + if (status !== 'READY') return ; + return ( + + this.takePicture(camera)} style={styles.capture}> + SNAP + + + ); + }} + + + ); + } + + takePicture = async function(camera) { + const options = { quality: 0.5, base64: true }; + const data = await camera.takePictureAsync(options); + // eslint-disable-next-line + console.log(data.uri); + } +} + +const styles = StyleSheet.create({ + container: { + flex: 1, + flexDirection: 'column', + backgroundColor: 'black', + }, + preview: { + flex: 1, + justifyContent: 'flex-end', + alignItems: 'center', + }, + capture: { + flex: 0, + backgroundColor: '#fff', + borderRadius: 5, + padding: 15, + paddingHorizontal: 20, + alignSelf: 'center', + margin: 20, + }, +}); + +AppRegistry.registerComponent('BadInstagramCloneApp', () => App); +``` + +#### `camera` + +*It's the RNCamera's reference* + +#### `status` + +'READY' | 'PENDING_AUTHORIZATION' | 'NOT_AUTHORIZED' + + + ## Properties #### `autoFocus` diff --git a/src/RNCamera.js b/src/RNCamera.js index 31e26e629..d178780e1 100644 --- a/src/RNCamera.js +++ b/src/RNCamera.js @@ -100,6 +100,8 @@ type StateType = { isAuthorizationChecked: boolean, }; +type Status = 'READY' | 'PENDING_AUTHORIZATION' | 'NOT_AUTHORIZED'; + const CameraManager: Object = NativeModules.RNCameraManager || NativeModules.RNCameraModule || { stubbed: true, @@ -314,10 +316,25 @@ export default class Camera extends React.Component { this.setState({ isAuthorized, isAuthorizationChecked: true }); } + getStatus = (): Status => { + const { isAuthorized, isAuthorizationChecked } = this.state; + return isAuthorized ? 'READY' : isAuthorizationChecked ? 'PENDING_AUTHORIZATION' : 'NOT_AUTHORIZED'; + } + + // CAF = Children as Function; + hasCAF = (): * => typeof this.props.children === 'function'; + + renderChildren = (): * => { + if (this.hasCAF()) { + return this.props.children({ camera: this, status: this.getStatus() }) + } + return null; + } + render() { const nativeProps = this._convertNativeProps(this.props); - if (this.state.isAuthorized) { + if (this.state.isAuthorized || this.hasCAF()) { return ( { onBarCodeRead={this._onObjectDetected(this.props.onBarCodeRead)} onFacesDetected={this._onObjectDetected(this.props.onFacesDetected)} onTextRecognized={this._onObjectDetected(this.props.onTextRecognized)} - /> + > + {this.renderChildren()} + ); } else if (!this.state.isAuthorizationChecked) { return this.props.pendingAuthorizationView; From 3811d82c75ceedc27b8aa5550e352159d5daf2b8 Mon Sep 17 00:00:00 2001 From: AndreiCalazans Date: Wed, 25 Apr 2018 18:12:53 -0300 Subject: [PATCH 2/2] fix(rn_camera): improve naming --- docs/RNCamera.md | 3 --- src/RNCamera.js | 8 ++++---- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/docs/RNCamera.md b/docs/RNCamera.md index 35af236bb..7147e3f99 100644 --- a/docs/RNCamera.md +++ b/docs/RNCamera.md @@ -106,9 +106,6 @@ class App extends Component { return ( { - this.camera = ref; - }} style={styles.preview} type={RNCamera.Constants.Type.back} flashMode={RNCamera.Constants.FlashMode.on} diff --git a/src/RNCamera.js b/src/RNCamera.js index d178780e1..37e836101 100644 --- a/src/RNCamera.js +++ b/src/RNCamera.js @@ -321,11 +321,11 @@ export default class Camera extends React.Component { return isAuthorized ? 'READY' : isAuthorizationChecked ? 'PENDING_AUTHORIZATION' : 'NOT_AUTHORIZED'; } - // CAF = Children as Function; - hasCAF = (): * => typeof this.props.children === 'function'; + // FaCC = Function as Child Component; + hasFaCC = (): * => typeof this.props.children === 'function'; renderChildren = (): * => { - if (this.hasCAF()) { + if (this.hasFaCC()) { return this.props.children({ camera: this, status: this.getStatus() }) } return null; @@ -334,7 +334,7 @@ export default class Camera extends React.Component { render() { const nativeProps = this._convertNativeProps(this.props); - if (this.state.isAuthorized || this.hasCAF()) { + if (this.state.isAuthorized || this.hasFaCC()) { return (