Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/components/ScannerInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { useSelector } from 'react-redux';

import { isString } from 'lodash';
import { appConfig } from '../constants';
import { useScanListener } from '../hooks/useScanListener';
import { RootState } from '../redux/reducers';
import { hideSoftKeyboard, showSoftKeyboard } from '../utils/KeyboardUtils';
import Theme from '../utils/Theme';
Expand Down Expand Up @@ -162,6 +163,16 @@ export function ScannerInput({
return () => clearTimeout(timer);
}, [value, timeout, onSubmit]);

useScanListener((result) => {
const scanned = result.data;
if (!scanned || scanned === lastSubmittedValue.current) {
return;
}
lastSubmittedValue.current = scanned;
onChange(scanned);
onSubmit(scanned);
}, shouldBeFocused);

const handleFocus = useCallback(() => {
if (showKeyboard) {
showSoftKeyboard();
Expand All @@ -187,7 +198,7 @@ export function ScannerInput({
}

const trimmed = value.trim();
if (trimmed) {
if (trimmed && trimmed !== lastSubmittedValue.current) {
lastSubmittedValue.current = trimmed;
onSubmit(trimmed);
}
Expand Down
37 changes: 16 additions & 21 deletions src/hooks/onBarcodeScanned.ts → src/hooks/useBarcodeScanned.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
import useEventListener from './useEventListener';
import { useState } from 'react';
import { useDispatch } from 'react-redux';
import { useEffect, useState } from 'react';
import { searchBarcode } from '../redux/actions/products';

import showPopup from '../components/Popup';
import { searchBarcode } from '../redux/actions/products';
import { useScanListener } from './useScanListener';

const onBarcodeScanned = () => {
const useBarcodeScanned = () => {
const [state, setState] = useState<any>({
error: null,
searchProductCode: null
});
const dispatch = useDispatch();
const barcodeData = useEventListener();

useEffect(() => {
if (barcodeData && Object.keys(barcodeData).length !== 0) {
onBarCodeScan(barcodeData.data);
}
}, [barcodeData]);

const onBarCodeScan = (query: string) => {
onEmptyQuery(query);
Expand All @@ -25,19 +19,21 @@ const onBarcodeScanned = () => {
onError(data, query, () => {
dispatch(searchBarcode(query, actionCallback));
});
} else {
if (data.length == 0) {
onEmptyData(query);
} else {
if (data && Object.keys(data).length !== 0) {
onSuccess(data, query);
}
}
} else if (data.length === 0) {
onEmptyData(query);
} else if (data && Object.keys(data).length !== 0) {
onSuccess(data, query);
}
};
dispatch(searchBarcode(query, actionCallback));
};

useScanListener((result) => {
if (result.data) {
onBarCodeScan(result.data);
}
});

const onError = (data: any, query: any, callback: (data: any) => void) => {
showPopup({
title: data.errorMessage ?? `Failed to load search results with value = "${query}"`,
Expand All @@ -56,7 +52,6 @@ const onBarcodeScanned = () => {
message: 'Search query is empty',
positiveButton: { text: 'Ok' }
});
return;
}
};

Expand Down Expand Up @@ -85,4 +80,4 @@ const onBarcodeScanned = () => {
return state.searchProductCode;
};

export default onBarcodeScanned;
export default useBarcodeScanned;
185 changes: 0 additions & 185 deletions src/hooks/useEventListener.ts

This file was deleted.

73 changes: 73 additions & 0 deletions src/hooks/useScanListener.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { useEffect, useRef } from 'react';
import { DeviceEventEmitter, Platform } from 'react-native';
import DataWedgeIntents from 'react-native-datawedge-intents';

import {
ACTION,
FILTER_ACTIONS,
FILTER_CATEGORY,
LISTENER,
PROFILE,
PROFILE_CONFIG,
PROFILE_CONFIG2
} from './constant';

export type ScanResult = {
data: string;
labelType?: string;
};

let profileConfigured = false;

function sendDataWedgeCommand(extraName: string, extraValue: unknown): void {
DataWedgeIntents.sendBroadcastWithExtras({
action: ACTION.API_ACTION,
extras: { [extraName]: extraValue, SEND_RESULT: 'false' }
});
}

function configureProfileOnce(): void {
if (profileConfigured) {
return;
}
profileConfigured = true;
sendDataWedgeCommand(PROFILE.CREATE_PROFILE, PROFILE.NAME);
sendDataWedgeCommand(PROFILE.SET_CONFIG_PROFILE, PROFILE_CONFIG);
sendDataWedgeCommand(PROFILE.SET_CONFIG_PROFILE, PROFILE_CONFIG2);
}

function extractScan(intent: Record<string, any>): ScanResult | null {
const data = intent?.[ACTION.DATA_STRING] ?? intent?.data;
if (!data) {
return null;
}
const labelType = intent?.[ACTION.LABEL_TYPE] ?? intent?.labelType;
return { data: String(data).trim(), labelType: labelType ? String(labelType) : undefined };
}
Comment on lines +39 to +46

export function useScanListener(onScan: (result: ScanResult) => void, enabled = true): void {
const onScanRef = useRef(onScan);
onScanRef.current = onScan;

useEffect(() => {
if (!enabled || Platform.OS !== 'android' || !DataWedgeIntents?.registerBroadcastReceiver) {
return;
}

configureProfileOnce();
DataWedgeIntents.registerBroadcastReceiver({
filterActions: FILTER_ACTIONS,
filterCategories: FILTER_CATEGORY
});

const handleIntent = (intent: Record<string, any>) => {
const result = extractScan(intent);
if (result) {
onScanRef.current(result);
}
};

const subscription = DeviceEventEmitter.addListener(LISTENER.BROADCAST_INTENT, handleIntent);
return () => subscription.remove();
}, [enabled]);
}
4 changes: 2 additions & 2 deletions src/screens/Scan/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import { View } from 'react-native';
import styles from './styles';
import { useNavigation } from '@react-navigation/native';
import Product from '../../data/product/Product';
import onBarcodeScanned from '../../hooks/onBarcodeScanned';
import useBarcodeScanned from '../../hooks/useBarcodeScanned';
import EmptyView from '../../components/EmptyView';
import BarcodeSearchHeader from '../../components/BarcodeSearchHeader/BarcodeSearchHeader';
import { searchBarcode } from '../../redux/actions/products';
import showPopup from '../../components/Popup';
import { useDispatch } from 'react-redux';

const Scan = () => {
const barcodeData = onBarcodeScanned();
const barcodeData = useBarcodeScanned();
const navigation = useNavigation();
Comment on lines 14 to 16
const dispatch = useDispatch();

Expand Down