/* eslint-disable enforce-react-hooks/enforce-react-hooks */ const React = require('react'); const Component = React.Component; const { connect } = require('react-redux'); const { View, TouchableOpacity, Text, Dimensions } = require('react-native'); import { RNCamera } from 'react-native-camera'; const Icon = require('react-native-vector-icons/Ionicons').default; const { _ } = require('lib/locale.js'); const { shim } = require('lib/shim'); const Setting = require('lib/models/Setting'); class CameraView extends Component { constructor() { super(); const dimensions = Dimensions.get('window'); this.state = { snapping: false, ratios: [], screenWidth: dimensions.width, screenHeight: dimensions.height, }; this.back_onPress = this.back_onPress.bind(this); this.photo_onPress = this.photo_onPress.bind(this); this.reverse_onPress = this.reverse_onPress.bind(this); this.ratio_onPress = this.ratio_onPress.bind(this); this.onCameraReady = this.onCameraReady.bind(this); this.onLayout = this.onLayout.bind(this); } onLayout(event) { this.setState({ screenWidth: event.nativeEvent.layout.width, screenHeight: event.nativeEvent.layout.height, }); } back_onPress() { if (this.props.onCancel) this.props.onCancel(); } reverse_onPress() { if (this.props.cameraType === RNCamera.Constants.Type.back) { Setting.setValue('camera.type', RNCamera.Constants.Type.front); } else { Setting.setValue('camera.type', RNCamera.Constants.Type.back); } } ratio_onPress() { if (this.state.ratios.length <= 1) return; let index = this.state.ratios.indexOf(this.props.cameraRatio); index++; if (index >= this.state.ratios.length) index = 0; Setting.setValue('camera.ratio', this.state.ratios[index]); } async photo_onPress() { if (!this.camera || !this.props.onPhoto) return; this.setState({ snapping: true }); const result = await this.camera.takePictureAsync({ quality: 0.8, exif: true, fixOrientation: true, }); if (this.props.onPhoto) this.props.onPhoto(result); this.setState({ snapping: false }); } async onCameraReady() { const ratios = await this.camera.getSupportedRatiosAsync(); this.setState({ ratios: ratios }); } renderButton(onPress, iconName, style) { let icon = null; if (typeof iconName === 'string') { icon = ( ); } else { icon = iconName; } return ( { icon } ); } fitRectIntoBounds(rect, bounds) { var rectRatio = rect.width / rect.height; var boundsRatio = bounds.width / bounds.height; var newDimensions = {}; // Rect is more landscape than bounds - fit to width if (rectRatio > boundsRatio) { newDimensions.width = bounds.width; newDimensions.height = rect.height * (bounds.width / rect.width); } else { // Rect is more portrait than bounds - fit to height newDimensions.width = rect.width * (bounds.height / rect.height); newDimensions.height = bounds.height; } return newDimensions; } cameraRect(ratio) { // To keep the calculations simpler, it's assumed that the phone is in // portrait orientation. Then at the end we swap the values if needed. const splitted = ratio.split(':'); const output = this.fitRectIntoBounds({ width: Number(splitted[1]), height: Number(splitted[0]), }, { width: Math.min(this.state.screenWidth, this.state.screenHeight), height: Math.max(this.state.screenWidth, this.state.screenHeight), }); if (this.state.screenWidth > this.state.screenHeight) { const w = output.width; output.width = output.height; output.height = w; } return output; } render() { const photoIcon = this.state.snapping ? 'md-checkmark' : 'md-camera'; const displayRatios = shim.mobilePlatform() === 'android' && this.state.ratios.length > 1; const reverseCameraButton = this.renderButton(this.reverse_onPress, 'md-reverse-camera', { flex: 1, flexDirection: 'row', justifyContent: 'flex-start', marginLeft: 20 }); const ratioButton = !displayRatios ? : this.renderButton(this.ratio_onPress, {Setting.value('camera.ratio')}, { flex: 1, flexDirection: 'row', justifyContent: 'flex-end', marginRight: 20 }); let cameraRatio = '4:3'; const cameraProps = {}; if (displayRatios) { cameraProps.ratio = this.props.cameraRatio; cameraRatio = this.props.cameraRatio; } const cameraRect = this.cameraRect(cameraRatio); cameraRect.left = (this.state.screenWidth - cameraRect.width) / 2; cameraRect.top = (this.state.screenHeight - cameraRect.height) / 2; return ( { this.camera = ref; }} type={this.props.cameraType} captureAudio={false} onCameraReady={this.onCameraReady} androidCameraPermissionOptions={{ title: _('Permission to use camera'), message: _('Your permission to use your camera is required.'), buttonPositive: _('OK'), buttonNegative: _('Cancel'), }} { ...cameraProps } > { reverseCameraButton } { ratioButton } ); } } const mapStateToProps = state => { return { cameraRatio: state.settings['camera.ratio'], cameraType: state.settings['camera.type'], }; }; module.exports = connect(mapStateToProps)(CameraView);