2020-04-17 19:11:06 +00:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2020-04-13 20:36:39 +00:00
|
|
|
import {
|
2020-04-22 16:34:53 +00:00
|
|
|
View, StyleSheet, Text
|
2020-04-13 20:36:39 +00:00
|
|
|
} from 'react-native';
|
2020-04-22 16:34:53 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2020-04-20 19:49:09 +00:00
|
|
|
import PINCode, { PinStatus } from '@haskkor/react-native-pincode';
|
2020-04-17 19:11:06 +00:00
|
|
|
import Modal from 'react-native-modal';
|
|
|
|
import useDeepCompareEffect from 'use-deep-compare-effect';
|
|
|
|
import _ from 'lodash';
|
2020-04-20 13:09:19 +00:00
|
|
|
import RNUserDefaults from 'rn-user-defaults';
|
2020-04-22 16:34:53 +00:00
|
|
|
import { useAsyncStorage } from '@react-native-community/async-storage';
|
|
|
|
import moment from 'moment';
|
2020-04-13 20:36:39 +00:00
|
|
|
|
|
|
|
import I18n from '../i18n';
|
|
|
|
import { withTheme } from '../theme';
|
|
|
|
import { themes } from '../constants/colors';
|
2020-04-17 19:11:06 +00:00
|
|
|
import EventEmitter from '../utils/events';
|
2020-04-13 20:36:39 +00:00
|
|
|
|
|
|
|
import sharedStyles from './Styles';
|
2020-04-17 19:11:06 +00:00
|
|
|
import { withSplit } from '../split';
|
2020-04-20 13:09:19 +00:00
|
|
|
import { PASSCODE_KEY, PASSCODE_LENGTH } from '../constants/passcode';
|
2020-04-17 19:11:06 +00:00
|
|
|
|
|
|
|
export const LOCAL_AUTHENTICATE = 'LOCAL_AUTHENTICATE';
|
2020-04-13 20:36:39 +00:00
|
|
|
|
2020-04-22 16:34:53 +00:00
|
|
|
const LOCKED_OUT_TIMER_KEY = 'kLockedOutTimer';
|
|
|
|
const ATTEMPTS_KEY = 'kAttempts';
|
|
|
|
const MAX_ATTEMPTS = 6;
|
|
|
|
const TIME_TO_LOCK = 30000;
|
|
|
|
|
2020-04-13 20:36:39 +00:00
|
|
|
const styles = StyleSheet.create({
|
|
|
|
container: {
|
|
|
|
flex: 1,
|
|
|
|
justifyContent: 'center',
|
2020-04-22 16:34:53 +00:00
|
|
|
width: '100%'
|
|
|
|
},
|
|
|
|
title: {
|
|
|
|
...sharedStyles.textRegular,
|
|
|
|
fontSize: 20,
|
|
|
|
fontWeight: '400',
|
|
|
|
marginBottom: 10,
|
|
|
|
textAlign: 'center'
|
2020-04-13 20:36:39 +00:00
|
|
|
},
|
2020-04-22 16:34:53 +00:00
|
|
|
subtitle: {
|
|
|
|
...sharedStyles.textRegular,
|
2020-04-13 20:36:39 +00:00
|
|
|
fontSize: 16,
|
2020-04-22 16:34:53 +00:00
|
|
|
fontWeight: '400',
|
|
|
|
textAlign: 'center'
|
|
|
|
},
|
|
|
|
circleButtonText: {
|
2020-04-13 20:36:39 +00:00
|
|
|
...sharedStyles.textRegular,
|
2020-04-22 16:34:53 +00:00
|
|
|
fontWeight: '100'
|
|
|
|
},
|
|
|
|
circleButton: {
|
|
|
|
borderWidth: 1
|
2020-04-13 20:36:39 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-04-22 16:34:53 +00:00
|
|
|
const getLockedUntil = t => moment(t).add(TIME_TO_LOCK);
|
|
|
|
|
|
|
|
const getDiff = t => new Date(t) - new Date();
|
|
|
|
|
|
|
|
const Timer = ({ time, theme, changeStatus }) => {
|
|
|
|
const calcTimeLeft = () => {
|
|
|
|
const diff = getDiff(time);
|
|
|
|
if (diff > 0) {
|
|
|
|
return Math.floor((diff / 1000) % 60);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const [timeLeft, setTimeLeft] = useState(calcTimeLeft());
|
|
|
|
const { removeItem } = useAsyncStorage(LOCKED_OUT_TIMER_KEY);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setTimeout(() => {
|
|
|
|
setTimeLeft(calcTimeLeft());
|
|
|
|
if (timeLeft <= 1) {
|
|
|
|
removeItem(LOCKED_OUT_TIMER_KEY);
|
|
|
|
removeItem(ATTEMPTS_KEY);
|
|
|
|
changeStatus(PinStatus.initial);
|
|
|
|
}
|
|
|
|
}, 1000);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!timeLeft) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Text style={[styles.subtitle, { color: themes[theme].bodyText }]}>Try again in {timeLeft} seconds</Text>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
// `changeStatus` prop is injected from react-native-pincode
|
|
|
|
const AppLocked = withTheme(({ theme, changeStatus }) => {
|
|
|
|
const [lockedUntil, setLockedUntil] = useState(null);
|
|
|
|
const { getItem } = useAsyncStorage(LOCKED_OUT_TIMER_KEY);
|
|
|
|
|
|
|
|
const readItemFromStorage = async() => {
|
|
|
|
const item = await getItem();
|
|
|
|
setLockedUntil(getLockedUntil(item));
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
readItemFromStorage();
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<View style={[styles.container, { backgroundColor: themes[theme].auxiliaryBackground }]}>
|
|
|
|
<Text style={[styles.title, { color: themes[theme].titleText }]}>App locked</Text>
|
|
|
|
<Timer theme={theme} time={lockedUntil} changeStatus={changeStatus} />
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
const ScreenLockedView = ({ theme }) => {
|
2020-04-20 13:09:19 +00:00
|
|
|
const [passcode, setPasscode] = useState('');
|
2020-04-17 19:11:06 +00:00
|
|
|
const [visible, setVisible] = useState(false);
|
|
|
|
const [data, setData] = useState({});
|
2020-04-22 16:34:53 +00:00
|
|
|
const { getItem, removeItem } = useAsyncStorage(LOCKED_OUT_TIMER_KEY);
|
2020-04-17 19:11:06 +00:00
|
|
|
|
|
|
|
useDeepCompareEffect(() => {
|
|
|
|
if (!_.isEmpty(data)) {
|
|
|
|
setVisible(true);
|
|
|
|
} else {
|
|
|
|
setVisible(false);
|
|
|
|
}
|
|
|
|
}, [data]);
|
|
|
|
|
2020-04-20 13:09:19 +00:00
|
|
|
const fetchPasscode = async() => {
|
|
|
|
const storedPin = await RNUserDefaults.get(PASSCODE_KEY);
|
|
|
|
setPasscode(storedPin);
|
|
|
|
};
|
|
|
|
|
|
|
|
const showScreenLock = (args) => {
|
|
|
|
setData(args);
|
|
|
|
fetchPasscode();
|
|
|
|
};
|
2020-04-17 19:11:06 +00:00
|
|
|
|
2020-04-22 16:34:53 +00:00
|
|
|
const checkOldSession = async() => {
|
|
|
|
const time = await getItem();
|
|
|
|
const lockedUntil = getLockedUntil(time);
|
|
|
|
const diff = getDiff(lockedUntil);
|
|
|
|
if (diff <= 1) {
|
|
|
|
removeItem(LOCKED_OUT_TIMER_KEY);
|
|
|
|
removeItem(ATTEMPTS_KEY);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-04-17 19:11:06 +00:00
|
|
|
useEffect(() => {
|
|
|
|
EventEmitter.addEventListener(LOCAL_AUTHENTICATE, showScreenLock);
|
2020-04-20 13:09:19 +00:00
|
|
|
fetchPasscode();
|
2020-04-22 16:34:53 +00:00
|
|
|
checkOldSession();
|
2020-04-17 19:11:06 +00:00
|
|
|
return () => EventEmitter.removeListener(LOCAL_AUTHENTICATE);
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const onSubmit = () => {
|
|
|
|
const { submit } = data;
|
|
|
|
if (submit) {
|
2020-04-20 13:09:19 +00:00
|
|
|
submit();
|
2020-04-17 19:11:06 +00:00
|
|
|
}
|
|
|
|
setData({});
|
|
|
|
};
|
|
|
|
|
2020-04-13 20:36:39 +00:00
|
|
|
return (
|
2020-04-17 19:11:06 +00:00
|
|
|
<Modal
|
|
|
|
useNativeDriver
|
|
|
|
isVisible={visible}
|
|
|
|
hideModalContentWhileAnimating
|
|
|
|
style={{ margin: 0 }}
|
|
|
|
>
|
2020-04-20 22:03:56 +00:00
|
|
|
<View style={[styles.container, { backgroundColor: themes[theme].auxiliaryBackground }]}>
|
2020-04-17 19:11:06 +00:00
|
|
|
<PINCode
|
2020-04-20 19:49:09 +00:00
|
|
|
status={PinStatus.enter}
|
2020-04-20 13:09:19 +00:00
|
|
|
passwordLength={PASSCODE_LENGTH}
|
2020-04-17 19:11:06 +00:00
|
|
|
customBackSpaceIcon={() => null}
|
|
|
|
finishProcess={onSubmit}
|
2020-04-20 13:09:19 +00:00
|
|
|
storedPin={passcode}
|
2020-04-22 16:34:53 +00:00
|
|
|
maxAttempts={MAX_ATTEMPTS}
|
2020-04-20 19:49:09 +00:00
|
|
|
touchIDDisabled
|
2020-04-20 20:59:51 +00:00
|
|
|
vibrationEnabled={false}
|
2020-04-22 16:34:53 +00:00
|
|
|
timeLocked={TIME_TO_LOCK}
|
2020-04-20 22:03:56 +00:00
|
|
|
colorCircleButtons={themes[theme].backgroundColor}
|
|
|
|
colorPassword={themes[theme].titleText}
|
|
|
|
colorPasswordEmpty={themes[theme].titleText}
|
|
|
|
colorPasswordError={themes[theme].dangerColor}
|
|
|
|
numbersButtonOverlayColor={themes[theme].bannerBackground}
|
|
|
|
stylePinCodeButtonNumber={themes[theme].bodyText}
|
|
|
|
stylePinCodeButtonNumberPressed={themes[theme].bodyText}
|
|
|
|
stylePinCodeColorTitle={themes[theme].titleText}
|
|
|
|
stylePinCodeColorSubtitle={themes[theme].titleText}
|
|
|
|
stylePinCodeColorSubtitleError={themes[theme].dangerColor}
|
2020-04-22 16:34:53 +00:00
|
|
|
stylePinCodeButtonCircle={[styles.circleButton, { borderColor: themes[theme].borderColor }]}
|
|
|
|
stylePinCodeTextTitle={styles.title}
|
|
|
|
stylePinCodeTextSubtitle={styles.subtitle}
|
|
|
|
stylePinCodeTextButtonCircle={styles.circleButtonText}
|
2020-04-20 22:03:56 +00:00
|
|
|
stylePinCodeHiddenPasswordSizeEmpty={8}
|
|
|
|
stylePinCodeHiddenPasswordSizeFull={12}
|
|
|
|
titleEnter='Enter your passcode'
|
2020-04-22 16:34:53 +00:00
|
|
|
timePinLockedAsyncStorageName={LOCKED_OUT_TIMER_KEY}
|
|
|
|
pinAttemptsAsyncStorageName={ATTEMPTS_KEY}
|
|
|
|
lockedPage={<AppLocked />}
|
2020-04-17 19:11:06 +00:00
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
</Modal>
|
2020-04-13 20:36:39 +00:00
|
|
|
);
|
2020-04-22 16:34:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Timer.propTypes = {
|
|
|
|
time: PropTypes.string,
|
|
|
|
theme: PropTypes.string,
|
|
|
|
changeStatus: PropTypes.func
|
|
|
|
};
|
|
|
|
|
|
|
|
AppLocked.propTypes = {
|
|
|
|
theme: PropTypes.string,
|
|
|
|
changeStatus: PropTypes.func
|
|
|
|
};
|
|
|
|
|
|
|
|
ScreenLockedView.propTypes = {
|
|
|
|
theme: PropTypes.string,
|
|
|
|
// eslint-disable-next-line react/no-unused-prop-types
|
|
|
|
split: PropTypes.bool // TODO: need it?
|
|
|
|
};
|
2020-04-13 20:36:39 +00:00
|
|
|
|
2020-04-17 19:11:06 +00:00
|
|
|
export default withSplit(withTheme(ScreenLockedView));
|