2020-08-29 03:40:55 +00:00
|
|
|
import {BarCodeEvent, BarCodeScanner, PermissionResponse} from 'expo-barcode-scanner';
|
|
|
|
import React, {useEffect, useState} from 'react';
|
2020-09-01 02:12:32 +00:00
|
|
|
import {AppRegistry, SafeAreaView, Text, View} from 'react-native';
|
|
|
|
import {Appbar, DarkTheme, DefaultTheme, Provider as PaperProvider, Surface} from 'react-native-paper';
|
|
|
|
import {expo as appExpo} from './app.json';
|
2020-08-29 03:40:55 +00:00
|
|
|
import {CancelButton} from './components/Common';
|
|
|
|
import {BarCodeDisplay, PrintButton, PrintingMessage} from './components/Print';
|
|
|
|
import {ScanButton, Scanner} from './components/Scan';
|
2020-09-01 02:12:32 +00:00
|
|
|
import {colors, styles} from './components/Styles';
|
2020-08-29 03:40:55 +00:00
|
|
|
import {BarcodeScannerAppState} from './models/BarcodeScannerAppState';
|
|
|
|
import {ElementProps, StateProps} from './models/ElementProps';
|
|
|
|
|
2020-09-01 02:12:32 +00:00
|
|
|
const theme = {
|
|
|
|
...DefaultTheme,
|
|
|
|
colors: colors,
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function Main () {
|
2020-08-29 03:40:55 +00:00
|
|
|
const [appState, setAppState] = useState<BarcodeScannerAppState>(BarcodeScannerAppState.INITIAL);
|
|
|
|
const [barCodeData, setBarCodeData] = useState<string>('');
|
|
|
|
const [date, setDate] = useState<Date>(new Date());
|
|
|
|
const [locationStr, setLocationStr] = useState<string>('4321');
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
BarCodeScanner.requestPermissionsAsync().then((value: PermissionResponse) => {
|
|
|
|
if (value.granted) {
|
|
|
|
setAppState(BarcodeScannerAppState.DEFAULT);
|
|
|
|
} else {
|
|
|
|
setAppState(BarcodeScannerAppState.ERROR);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, []);
|
|
|
|
|
2020-09-01 02:12:32 +00:00
|
|
|
const _scan = () => setAppState(BarcodeScannerAppState.SCANNING);
|
|
|
|
const _print = () => setAppState(BarcodeScannerAppState.PRINTING);
|
|
|
|
const _home = () => setAppState(BarcodeScannerAppState.DEFAULT);
|
|
|
|
|
2020-08-29 03:40:55 +00:00
|
|
|
const handleBarCodeScanned = (e: BarCodeEvent) => {
|
|
|
|
setBarCodeData(e.data);
|
|
|
|
setDate(new Date());
|
|
|
|
setAppState(BarcodeScannerAppState.SCANNED);
|
|
|
|
};
|
|
|
|
|
|
|
|
function ErrorMessage(props: ElementProps) {
|
|
|
|
return <Text>Something went wrong.</Text>;
|
|
|
|
}
|
|
|
|
|
|
|
|
function LoadingMessage(props: ElementProps) {
|
|
|
|
return <Text>Loading...</Text>;
|
|
|
|
}
|
|
|
|
|
|
|
|
function SuccessMessage(props: ElementProps) {
|
|
|
|
return <Text>Your barcode label has printed successfully.</Text>;
|
|
|
|
}
|
|
|
|
|
2020-09-01 02:12:32 +00:00
|
|
|
function ActionButtons(props: ElementProps) {
|
|
|
|
return <View style={styles.container}>
|
|
|
|
<PrintButton onClicked={_print}/>
|
|
|
|
<CancelButton onClicked={_home}/>
|
|
|
|
</View>
|
|
|
|
}
|
|
|
|
|
|
|
|
function App(props: StateProps) {
|
2020-08-29 03:40:55 +00:00
|
|
|
switch (props.appState) {
|
|
|
|
case BarcodeScannerAppState.INITIAL:
|
|
|
|
return <LoadingMessage/>;
|
|
|
|
case BarcodeScannerAppState.DEFAULT:
|
2020-09-01 02:12:32 +00:00
|
|
|
return <ScanButton onClicked={_scan}/>;
|
2020-08-29 03:40:55 +00:00
|
|
|
case BarcodeScannerAppState.PRINTED:
|
|
|
|
return <SuccessMessage/>;
|
|
|
|
case BarcodeScannerAppState.PRINTING:
|
2020-09-01 02:12:32 +00:00
|
|
|
return <PrintingMessage
|
|
|
|
onCancel={_home}
|
|
|
|
id={barCodeData}
|
|
|
|
date={date} location={locationStr}
|
|
|
|
/>;
|
2020-08-29 03:40:55 +00:00
|
|
|
case BarcodeScannerAppState.SCANNED:
|
2020-09-01 02:12:32 +00:00
|
|
|
return <View style={styles.container}>
|
2020-08-29 03:40:55 +00:00
|
|
|
<BarCodeDisplay id={barCodeData} date={date} location={locationStr}/>
|
2020-09-01 02:12:32 +00:00
|
|
|
<ActionButtons></ActionButtons>
|
2020-08-29 03:40:55 +00:00
|
|
|
</View>;
|
|
|
|
case BarcodeScannerAppState.SCANNING:
|
|
|
|
return <Scanner onScanned={handleBarCodeScanned}/>;
|
|
|
|
default:
|
|
|
|
return <ErrorMessage/>;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2020-09-01 02:12:32 +00:00
|
|
|
<PaperProvider theme={theme}>
|
|
|
|
<Appbar.Header>
|
|
|
|
<Appbar.Content title={appExpo.name} />
|
|
|
|
<Appbar.Action icon="home" onPress={_home} />
|
|
|
|
<Appbar.Action icon="camera" onPress={_scan} />
|
|
|
|
<Appbar.Action icon="printer" onPress={_print} />
|
|
|
|
</Appbar.Header>
|
|
|
|
<SafeAreaView style={styles.surface}>
|
|
|
|
<App appState={appState}/>
|
|
|
|
</SafeAreaView>
|
|
|
|
</PaperProvider>
|
2020-08-29 03:40:55 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-09-01 02:12:32 +00:00
|
|
|
AppRegistry.registerComponent(appExpo.name, () => Main);
|