Skip to content

Commit

Permalink
feat(mobile): ask permission with a button activate camera
Browse files Browse the repository at this point in the history
Signed-off-by: Sacha Froment <sfroment42@gmail.com>
  • Loading branch information
sfroment committed Mar 7, 2019
1 parent f1af34b commit 53b8060
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 25 deletions.
96 changes: 73 additions & 23 deletions client/react-native/common/components/Library/QRReader.js
@@ -1,35 +1,85 @@
import React, { PureComponent } from 'react'
import { View, Text, Button, NativeModules, Platform } from 'react-native'
import { withNamespaces } from 'react-i18next'
import QRCodeScanner from 'react-native-qrcode-scanner'
import Permissions from 'react-native-permissions'

import colors from '../../constants/colors'

export const permissionsStatus = {
notDetermined: 'undetermined',
denied: 'denied',
authorized: 'authorized',
restricted: 'restricted',
}

const { CoreModule } = NativeModules
class QRReader extends PureComponent {
constructor (props) {
super(props)
this.state = {
permStatus: permissionsStatus.notDetermined,
onPress: () => {},
}
}

componentWillMount () {
Permissions.check('camera').then(this.setStatus)
}

setStatus = (permStatus) => {
const onPress = Platform.OS === 'ios' && permStatus === permissionsStatus.denied
? () => CoreModule.openSettings()
: () => Permissions.request('camera').then(this.setStatus)

this.setState({ permStatus, onPress })
}

reactivate () {
this.scanner.reactivate()
}

render () {
const { onFound, style, cameraStyle } = this.props

return <QRCodeScanner
onRead={event => onFound(event.data)}
onError={error => {
console.error(error)
}}
cameraProps={{ captureAudio: false }}
ref={(scanner) => { this.scanner = scanner }}
containerStyle={style}
topViewStyle={{
width: 0,
height: 0,
}}
bottomViewStyle={{
width: 0,
height: 0,
}}
cameraStyle={cameraStyle || {}}
fadeIn={false}
showMarker
/>
const { t, onFound, style, cameraStyle } = this.props
const { permStatus, onPress } = this.state

return permStatus === permissionsStatus.authorized ? (
<QRCodeScanner
onRead={event => onFound(event.data)}
onError={error => {
console.error(error)
}}
cameraProps={{ captureAudio: false }}
ref={(scanner) => { this.scanner = scanner }}
containerStyle={style}
topViewStyle={{
width: 0,
height: 0,
}}
bottomViewStyle={{
width: 0,
height: 0,
}}
cameraStyle={cameraStyle || {}}
fadeIn={false}
showMarker
/>
) : (
<View style={{ flexDirection: 'column' }}>
<View style={{ flexGrow: 1, flexDirection: 'row', alignSelf: 'center', marginBottom: 10, alignItems: 'flex-end' }}>
<Text style={{ fontSize: 24, color: colors.white }}>
{t('contacts.add.qrcode-camera-ask-title')}
</Text>
</View>
<Text style={{ flexGrow: 0, fontSize: 18, textAlign: 'center', marginRight: 10, marginLeft: 10, color: colors.lightGrey }}>
{t('contacts.add.qrcode-camera-ask-text')}
</Text>
<View style={{ flexGrow: 1 }}>
<Button onPress={onPress} title={t('contacts.add.qrcode-camera-ask-button')} />
</View>
</View>
)
}
}

export default QRReader
export default withNamespaces()(QRReader)
5 changes: 4 additions & 1 deletion client/react-native/common/i18n/en/messages.json
Expand Up @@ -74,7 +74,7 @@
"notifications-not-supported": "Notifications are not currently supported on this platform",
"notifications-transport": "Notifications delivery",
"notifications-alerts": "Alerts",
"notifications-open-settings": "You need to activate notifications on your device settings, then come back to this page to enable notifications",
"notifications-open-settings": "You need to enable notifications on your device settings, then come back to this page to enable notifications",
"push-berty-apple-servers": "Via Berty and Apple servers",
"push-berty-google-firebase-servers": "Via Berty and Google's Firebase servers",
"push-mqtt-berty-servers": "Via Berty's MQTT servers",
Expand Down Expand Up @@ -137,6 +137,9 @@
"pub-key-paste": "Paste key",
"pub-key-add": "Add this key",
"qrcode-not-from-berty": "This is not a Berty QR Code",
"qrcode-camera-ask-title": "Scan QR Code",
"qrcode-camera-ask-button": "Allow camera access",
"qrcode-camera-ask-text": "Allow Berty acces to your camera to scan the QR Code of your contacts",
"nearby": "Nearby",
"invite": "Invite",
"invalid-public-key": "This public key is invalid",
Expand Down
3 changes: 3 additions & 0 deletions client/react-native/common/i18n/fr/messages.json
Expand Up @@ -137,6 +137,9 @@
"pub-key-paste": "Coller une clé",
"pub-key-add": "Ajouter cette clé",
"qrcode-not-from-berty": "Ce n'est pas un QR Code Berty",
"qrcode-camera-ask-button": "Autoriser l'accès à la caméra",
"qrcode-camera-ask-text": "Autorisez Berty à accéder à votre caméra pour scanner les QR Code de vos contacts",
"qrcode-camera-ask-title": "Scanner des QR Code",
"nearby": "Proche",
"invite": "Inviter",
"invalid-public-key": "Cette clé publique est invalide",
Expand Down
1 change: 1 addition & 0 deletions client/react-native/package.json
Expand Up @@ -46,6 +46,7 @@
"react-native-keyboard-spacer": "^0.4.1",
"react-native-languages": "^3.0.1",
"react-native-network-info": "^4.0.0",
"react-native-permissions": "1.1.1",
"react-native-qrcode-scanner": "^1.1.0",
"react-native-qrcode-svg": "^5.1.1",
"react-native-restart": "^0.0.7",
Expand Down
2 changes: 1 addition & 1 deletion client/react-native/yarn.lock
Expand Up @@ -6114,7 +6114,7 @@ react-native-network-info@^4.0.0:
resolved "https://registry.yarnpkg.com/react-native-network-info/-/react-native-network-info-4.0.1.tgz#e0242cfec0d9c0e1c6c32b8df596bb4c3223a5a3"
integrity sha512-ZeSkeBL/fHsFR6sAOVHMd4oMknCwsbEeL+Zewec1/3ielg8k9YcddK3wymXS7kvwV0mTB2ji4X5tI0b7XY0sDA==

react-native-permissions@^1.1.1:
react-native-permissions@1.1.1, react-native-permissions@^1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/react-native-permissions/-/react-native-permissions-1.1.1.tgz#4876004681ff8556454613d85249b01baff9b35b"
integrity sha512-t0Ujm177bagjUOSzhpmkSz+LqFW04HnY9TeZFavDCmV521fQvFz82aD+POXqWsAdsJVOK3umJYBNNqCjC3g0hQ==
Expand Down

0 comments on commit 53b8060

Please sign in to comment.