From fbcbd67a1db8fc6f0cebb3e9dbdb7b346a805f27 Mon Sep 17 00:00:00 2001 From: Aaron Louie Date: Thu, 10 Sep 2020 18:23:55 -0400 Subject: [PATCH] Stores and restores settings to/from local AsyncStorage --- App.tsx | 39 +++++++++++++++++++++++++++++++++-- components/Print.tsx | 17 ++++++---------- components/Scan.tsx | 7 ++++--- components/Settings.tsx | 45 +++++++++++++++++++++++++++++++++++------ components/Styles.tsx | 7 ++++--- models/ElementProps.tsx | 4 +++- 6 files changed, 93 insertions(+), 26 deletions(-) diff --git a/App.tsx b/App.tsx index 16c788b..f58b82f 100644 --- a/App.tsx +++ b/App.tsx @@ -55,13 +55,30 @@ export default function Main() { const [sampleId, setSampleId] = useState(''); const [barCodeId, setBarCodeId] = useState(''); const [sampleDate, setSampleDate] = useState(new Date()); - const [locationStr, setLocationStr] = useState('4321'); + const [locationStr, setLocationStr] = useState('0000'); const [errorMessage, setErrorMessage] = useState(''); const [samples, setSamples] = useState([]); const [lineCounts, setLineCounts] = useState([]); const [cameraType, setCameraType] = useState('back'); + const [numCopies, setNumCopies] = useState(0); + + const defaultsInitializers = { + 'default.cameraType': (s: string) => setCameraType(s as CameraType), + 'default.numCopies': (s: string) => setNumCopies(parseInt(s, 10)), + 'default.locationStr': (s: string) => setLocationStr(s), + }; useEffect(() => { + AsyncStorage.multiGet(Object.keys(defaultsInitializers)).then(storedDefaults => { + console.log('storedDefaults', storedDefaults); + storedDefaults.forEach(d => { + if (d[1] !== null) { + // @ts-ignore + defaultsInitializers[d[0]](d[1]); + } + }); + }); + BarCodeScanner.requestPermissionsAsync().then((value: PermissionResponse) => { if (value.granted) { setAppState(BarcodeScannerAppState.DEFAULT); @@ -238,6 +255,7 @@ export default function Main() { case BarcodeScannerAppState.PRINTING: return { + onSave={(newCameraType: CameraType, newNumCopies: number, newLocationStr: string) => { setCameraType(newCameraType); + setNumCopies(newNumCopies); setLocationStr(newLocationStr); + + console.log(newCameraType); + console.log(newLocationStr); + console.log(newNumCopies); + + AsyncStorage.multiSet([ + ['default.cameraType', newCameraType as string], + ['default.locationStr', newLocationStr], + ['default.numCopies', newNumCopies.toString()], + ]).then(() => { + console.log('New defaults stored.'); + AsyncStorage.multiGet(Object.keys(defaultsInitializers)).then(storedDefaults => { + console.log('stored defaults after saving Settings:', storedDefaults); + }); + }); _home(); }} onCancel={_home} diff --git a/components/Print.tsx b/components/Print.tsx index 55714f9..028f3d0 100644 --- a/components/Print.tsx +++ b/components/Print.tsx @@ -28,7 +28,7 @@ const _save = (props: PrintingProps): Promise => { } const _print = async (props: PrintingProps): Promise => { - const numCopies = 2; // Number of copies to print + const numCopies = props.numCopies; // Number of copies to print const units = 'mm'; const pageSize = 28.6; const pageWidth = `${pageSize}${units}`; @@ -111,12 +111,7 @@ const _print = async (props: PrintingProps): Promise => { const pagesArray = []; for (let i=0; i => {
${svgString} -
${format(fakeDate, 'yyyy-MM-dd')}
+
${format(props.date, 'yyyy-MM-dd')}
T
- ${format(fakeDate, 'HH')}
- ${`${i}`.padStart(2, '0')} + ${format(props.date, 'HH')}
+ ${format(props.date, 'mm')}
L
${props.location.slice(0, 2)}
${props.location.slice(2)}
-
#${fakeBarcodeId}
+
#${props.barCodeId}
`; diff --git a/components/Scan.tsx b/components/Scan.tsx index 1bc113d..dd6dec9 100644 --- a/components/Scan.tsx +++ b/components/Scan.tsx @@ -17,10 +17,10 @@ export const Scanner = (props: ScannerProps): ReactElement => {
- - Place ID card with the barcode facing the camera. Keep the barcode in the green box. - + + Instruct the patient to hold their card up with the barcode facing the camera. Keep the barcode in the orange box. +