Application Android et IOS pour l'amicale des élèves https://play.google.com/store/apps/details?id=fr.amicaleinsat.application
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ScannerScreen.js 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // @flow
  2. import * as React from 'react';
  3. import {StyleSheet, View} from "react-native";
  4. import {Button, Text, withTheme} from 'react-native-paper';
  5. import {BarCodeScanner} from "expo-barcode-scanner";
  6. import {Camera} from 'expo-camera';
  7. import URLHandler from "../../utils/URLHandler";
  8. import {Linking} from "expo";
  9. import AlertDialog from "../../components/Dialogs/AlertDialog";
  10. import i18n from 'i18n-js';
  11. import CustomTabBar from "../../components/Tabbar/CustomTabBar";
  12. type Props = {};
  13. type State = {
  14. hasPermission: boolean,
  15. scanned: boolean,
  16. dialogVisible: boolean,
  17. dialogTitle: string,
  18. dialogMessage: string,
  19. };
  20. class ScannerScreen extends React.Component<Props, State> {
  21. state = {
  22. hasPermission: false,
  23. scanned: false,
  24. dialogVisible: false,
  25. dialogTitle: "",
  26. dialogMessage: "",
  27. };
  28. constructor() {
  29. super();
  30. }
  31. componentDidMount() {
  32. this.requestPermissions();
  33. }
  34. requestPermissions = () => Camera.requestPermissionsAsync().then(this.updatePermissionStatus);
  35. updatePermissionStatus = ({status}) => this.setState({hasPermission: status === "granted"});
  36. handleCodeScanned = ({type, data}) => {
  37. if (!URLHandler.isUrlValid(data))
  38. this.showErrorDialog();
  39. else {
  40. this.setState({scanned: true});
  41. Linking.openURL(data);
  42. }
  43. };
  44. getPermissionScreen() {
  45. return <View style={{marginLeft: 10, marginRight: 10}}>
  46. <Text>{i18n.t("scannerScreen.errorPermission")}</Text>
  47. <Button
  48. icon="camera"
  49. mode="contained"
  50. onPress={this.requestPermissions}
  51. style={{
  52. marginTop: 10,
  53. marginLeft: 'auto',
  54. marginRight: 'auto',
  55. }}
  56. >
  57. {i18n.t("scannerScreen.buttonPermission")}
  58. </Button>
  59. </View>
  60. }
  61. getOverlay() {
  62. return (
  63. <View style={{flex: 1}}>
  64. <View style={{flex: 1}}>
  65. <View style={{...overlayBackground, top: 0, height: '10%', width: '80%', left: '10%'}}/>
  66. <View style={{...overlayBackground, left: 0, width: '10%', height: '100%'}}/>
  67. <View style={{...overlayBackground, right: 0, width: '10%', height: '100%'}}/>
  68. <View style={{...overlayBackground, bottom: 0, height: '10%', width: '80%', left: '10%'}}/>
  69. </View>
  70. <View style={styles.overlayTopLeft}>
  71. <View style={{...overlayHorizontalLineStyle, top: 0}}/>
  72. <View style={{...overlayVerticalLineStyle, left: 0}}/>
  73. </View>
  74. <View style={styles.overlayTopRight}>
  75. <View style={{...overlayHorizontalLineStyle, top: 0}}/>
  76. <View style={{...overlayVerticalLineStyle, right: 0}}/>
  77. </View>
  78. <View style={styles.overlayBottomLeft}>
  79. <View style={{...overlayHorizontalLineStyle, bottom: 0}}/>
  80. <View style={{...overlayVerticalLineStyle, left: 0}}/>
  81. </View>
  82. <View style={styles.overlayBottomRight}>
  83. <View style={{...overlayHorizontalLineStyle, bottom: 0}}/>
  84. <View style={{...overlayVerticalLineStyle, right: 0}}/>
  85. </View>
  86. </View>
  87. );
  88. }
  89. showHelpDialog = () => {
  90. this.setState({
  91. dialogVisible: true,
  92. scanned: true,
  93. dialogTitle: i18n.t("scannerScreen.helpTitle"),
  94. dialogMessage: i18n.t("scannerScreen.helpMessage"),
  95. });
  96. };
  97. showErrorDialog() {
  98. this.setState({
  99. dialogVisible: true,
  100. scanned: true,
  101. dialogTitle: i18n.t("scannerScreen.errorTitle"),
  102. dialogMessage: i18n.t("scannerScreen.errorMessage"),
  103. });
  104. }
  105. onDialogDismiss = () => this.setState({
  106. dialogVisible: false,
  107. scanned: false,
  108. });
  109. getScanner() {
  110. return (
  111. <View style={styles.cameraContainer}>
  112. <Camera
  113. onBarCodeScanned={this.state.scanned ? undefined : this.handleCodeScanned}
  114. type={Camera.Constants.Type.back}
  115. barCodeScannerSettings={{
  116. barCodeTypes: [BarCodeScanner.Constants.BarCodeType.qr],
  117. }}
  118. style={StyleSheet.absoluteFill}
  119. ratio={'1:1'}
  120. >
  121. {this.getOverlay()}
  122. </Camera>
  123. </View>
  124. );
  125. }
  126. render() {
  127. return (
  128. <View style={{
  129. ...styles.container,
  130. marginBottom: CustomTabBar.TAB_BAR_HEIGHT + 20
  131. }}>
  132. {this.state.hasPermission
  133. ? this.getScanner()
  134. : this.getPermissionScreen()
  135. }
  136. <View style={{height: 50}}>
  137. <Button
  138. icon="information"
  139. mode="contained"
  140. onPress={this.showHelpDialog}
  141. style={styles.button}
  142. >
  143. {i18n.t("scannerScreen.helpButton")}
  144. </Button>
  145. </View>
  146. <AlertDialog
  147. visible={this.state.dialogVisible}
  148. onDismiss={this.onDialogDismiss}
  149. title={this.state.dialogTitle}
  150. message={this.state.dialogMessage}
  151. />
  152. </View>
  153. );
  154. }
  155. }
  156. const borderOffset = '10%';
  157. const overlayBoxStyle = {
  158. position: 'absolute',
  159. width: 25,
  160. height: 25,
  161. };
  162. const overlayLineStyle = {
  163. position: 'absolute',
  164. backgroundColor: "#fff",
  165. borderRadius: 2,
  166. };
  167. const overlayHorizontalLineStyle = {
  168. ...overlayLineStyle,
  169. width: '100%',
  170. height: 5,
  171. };
  172. const overlayVerticalLineStyle = {
  173. ...overlayLineStyle,
  174. height: '100%',
  175. width: 5,
  176. };
  177. const overlayBackground = {
  178. backgroundColor: "rgba(0,0,0,0.47)",
  179. position: "absolute",
  180. };
  181. const styles = StyleSheet.create({
  182. container: {
  183. flex: 1,
  184. justifyContent: 'center',
  185. },
  186. cameraContainer: {
  187. marginTop: 'auto',
  188. marginBottom: 'auto',
  189. aspectRatio: 1,
  190. width: '100%',
  191. },
  192. button: {
  193. position: 'absolute',
  194. bottom: 5,
  195. width: '80%',
  196. left: '10%'
  197. },
  198. overlayTopLeft: {
  199. ...overlayBoxStyle,
  200. top: borderOffset,
  201. left: borderOffset,
  202. },
  203. overlayTopRight: {
  204. ...overlayBoxStyle,
  205. top: borderOffset,
  206. right: borderOffset,
  207. },
  208. overlayBottomLeft: {
  209. ...overlayBoxStyle,
  210. bottom: borderOffset,
  211. left: borderOffset,
  212. },
  213. overlayBottomRight: {
  214. ...overlayBoxStyle,
  215. bottom: borderOffset,
  216. right: borderOffset,
  217. },
  218. });
  219. export default withTheme(ScannerScreen);