2020-05-08 17:04:37 +00:00
|
|
|
import React, { useEffect, useState } from 'react';
|
|
|
|
import { Grid } from 'react-native-easy-grid';
|
|
|
|
|
|
|
|
import { themes } from '../../../constants/colors';
|
|
|
|
import { resetAttempts } from '../../../utils/localAuthentication';
|
|
|
|
import { TYPE } from '../constants';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { getDiff, getLockedUntil } from '../utils';
|
2020-05-08 17:04:37 +00:00
|
|
|
import I18n from '../../../i18n';
|
2022-03-23 00:43:59 +00:00
|
|
|
import { useTheme } from '../../../theme';
|
2020-05-08 17:04:37 +00:00
|
|
|
import styles from './styles';
|
|
|
|
import Title from './Title';
|
|
|
|
import Subtitle from './Subtitle';
|
|
|
|
import LockIcon from './LockIcon';
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
interface IPasscodeTimer {
|
2022-03-23 00:43:59 +00:00
|
|
|
time: Date | null;
|
2021-09-13 20:41:05 +00:00
|
|
|
setStatus: Function;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IPasscodeLocked {
|
|
|
|
setStatus: Function;
|
|
|
|
}
|
|
|
|
|
2022-03-23 00:43:59 +00:00
|
|
|
const Timer = React.memo(({ time, setStatus }: IPasscodeTimer) => {
|
2020-05-08 17:04:37 +00:00
|
|
|
const calcTimeLeft = () => {
|
2022-03-23 00:43:59 +00:00
|
|
|
const diff = getDiff(time || 0);
|
2020-05-08 17:04:37 +00:00
|
|
|
if (diff > 0) {
|
|
|
|
return Math.floor((diff / 1000) % 60);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-23 00:43:59 +00:00
|
|
|
const [timeLeft, setTimeLeft] = useState(calcTimeLeft());
|
2020-05-08 17:04:37 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setTimeout(() => {
|
|
|
|
setTimeLeft(calcTimeLeft());
|
2022-03-23 00:43:59 +00:00
|
|
|
if (timeLeft && timeLeft <= 1) {
|
2020-05-08 17:04:37 +00:00
|
|
|
resetAttempts();
|
|
|
|
setStatus(TYPE.ENTER);
|
|
|
|
}
|
|
|
|
}, 1000);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!timeLeft) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-03-23 00:43:59 +00:00
|
|
|
return <Subtitle text={I18n.t('Passcode_app_locked_subtitle', { timeLeft })} />;
|
2020-05-08 17:04:37 +00:00
|
|
|
});
|
|
|
|
|
2022-03-23 00:43:59 +00:00
|
|
|
const Locked = React.memo(({ setStatus }: IPasscodeLocked) => {
|
|
|
|
const [lockedUntil, setLockedUntil] = useState<Date | null>(null);
|
|
|
|
const { theme } = useTheme();
|
2020-05-08 17:04:37 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
const readItemFromStorage = async () => {
|
2020-05-08 17:04:37 +00:00
|
|
|
const l = await getLockedUntil();
|
|
|
|
setLockedUntil(l);
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
readItemFromStorage();
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return (
|
2021-09-13 20:41:05 +00:00
|
|
|
<Grid style={[styles.grid, { backgroundColor: themes[theme].passcodeBackground }]}>
|
2022-03-23 00:43:59 +00:00
|
|
|
<LockIcon />
|
|
|
|
<Title text={I18n.t('Passcode_app_locked_title')} />
|
|
|
|
<Timer time={lockedUntil} setStatus={setStatus} />
|
2020-05-08 17:04:37 +00:00
|
|
|
</Grid>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
export default Locked;
|