Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: qr scanner vibration #1125

Merged
merged 1 commit into from
Jun 23, 2023
Merged
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
42 changes: 21 additions & 21 deletions src/components/modals/qr/qr.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import React, {useCallback, useRef, useState} from 'react';

import {parseUri} from '@walletconnect/utils';
import {utils} from 'ethers';
import {Dimensions, View, useWindowDimensions} from 'react-native';
import _ from 'lodash';
import {Dimensions, StatusBar, View, useWindowDimensions} from 'react-native';
import {BarCodeReadEvent} from 'react-native-camera';
import {launchImageLibrary} from 'react-native-image-picker';
// @ts-ignore
Expand All @@ -19,30 +20,26 @@ import {useWalletsVisible} from '@app/hooks/use-wallets-visible';
import {I18N} from '@app/i18n';
import {HapticEffects, vibrate} from '@app/services/haptic';
import {Modals} from '@app/types';
import {QR_STATUS_BAR} from '@app/variables/common';

import {QrBottomView} from './qr-bottom-view';
import {QrNoAccess} from './qr-no-access';
import {QrTopView} from './qr-top-view';

export type QRModalProps = Modals['qr'];

const debbouncedVibrate = _.debounce(vibrate, 1000);

export const QRModal = ({onClose = () => {}, qrWithoutFrom}: QRModalProps) => {
const [isOpen, setIsOpen] = useState(false);
const visible = useWalletsVisible();
const [error, setError] = useState(false);
const [flashMode, setFlashMode] = useState(false);

const closeDistance = useWindowDimensions().height / 6;
const [code, setCode] = useState('');
const isProcessing = useRef(false);

const vibrateWrapper = useCallback(
(effect: HapticEffects) => {
if (!error) {
vibrate(effect);
}
},
[error],
);
const [error, setError] = useState(false);
const [flashMode, setFlashMode] = useState(false);

const prepareAddress = useCallback((data: string) => {
if (data.startsWith('haqq:')) {
Expand Down Expand Up @@ -77,7 +74,7 @@ export const QRModal = ({onClose = () => {}, qrWithoutFrom}: QRModalProps) => {
from: visible[0].address.trim(),
});
} else {
vibrateWrapper(HapticEffects.success);
vibrate(HapticEffects.success);
setIsOpen(true);
}
} else if (parseUri(address)?.protocol === 'wc') {
Expand All @@ -87,35 +84,37 @@ export const QRModal = ({onClose = () => {}, qrWithoutFrom}: QRModalProps) => {
}, 1000);
} else if (!error) {
setError(true);
vibrateWrapper(HapticEffects.error);
debbouncedVibrate(HapticEffects.error);
setTimeout(() => {
setError(false);
}, 5000);
}
},
[error, visible, onClose, prepareAddress, vibrateWrapper],
[error, visible, onClose, prepareAddress],
);

const onGetAddress = useCallback(
(slicedAddress: string) => {
if (slicedAddress && qrWithoutFrom) {
vibrateWrapper(HapticEffects.success);
vibrate(HapticEffects.success);
app.emit('address', {
to: slicedAddress,
});
} else if (slicedAddress) {
checkAddress(slicedAddress);
}
},
[checkAddress, qrWithoutFrom, vibrateWrapper],
[checkAddress, qrWithoutFrom],
);

const onSuccess = useCallback(
(e: BarCodeReadEvent) => {
if (!isProcessing.current && e.data && e.data !== code) {
const newCode = e.data?.trim?.()?.toLowerCase?.();
const currentCode = code?.trim?.()?.toLowerCase?.();
if (!isProcessing.current && e.data && newCode !== currentCode) {
isProcessing.current = true;
try {
vibrateWrapper(HapticEffects.selection);
vibrate(HapticEffects.selection);
setCode(e.data);
const slicedAddress = prepareAddress(e.data);
if (slicedAddress) {
Expand All @@ -126,7 +125,7 @@ export const QRModal = ({onClose = () => {}, qrWithoutFrom}: QRModalProps) => {
}
}
},
[code, onGetAddress, prepareAddress, vibrateWrapper],
[code, onGetAddress, prepareAddress],
);

const onClickGallery = useCallback(async () => {
Expand All @@ -153,11 +152,12 @@ export const QRModal = ({onClose = () => {}, qrWithoutFrom}: QRModalProps) => {

const onToggleFlashMode = useCallback(() => {
setFlashMode(pr => !pr);
vibrateWrapper(HapticEffects.impactLight);
}, [vibrateWrapper]);
vibrate(HapticEffects.impactLight);
}, []);

return (
<>
<StatusBar backgroundColor={QR_STATUS_BAR} />
<QRscanner
isRepeatScan={true}
vibrate={false}
Expand Down