Rocket.Chat.ReactNative/app/views/ScreenLockedView.js

229 lines
6.1 KiB
JavaScript
Raw Normal View History

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-22 19:58:19 +00:00
import {
PASSCODE_KEY, PASSCODE_LENGTH, LOCAL_AUTHENTICATE_EMITTER, LOCKED_OUT_TIMER_KEY, ATTEMPTS_KEY
} from '../constants/localAuthentication';
import { resetAttempts } from '../utils/localAuthentication';
import { isTablet } from '../utils/deviceInfo';
import Orientation from 'react-native-orientation-locker';
2020-04-23 16:08:50 +00:00
import Passcode from '../containers/Passcode';
2020-04-23 18:28:40 +00:00
import { TYPE } from '../containers/Passcode/constants';
2020-04-13 20:36:39 +00:00
2020-04-22 16:34:53 +00:00
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());
useEffect(() => {
setTimeout(() => {
setTimeLeft(calcTimeLeft());
if (timeLeft <= 1) {
2020-04-22 19:58:19 +00:00
resetAttempts();
2020-04-22 16:34:53 +00:00
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 19:58:19 +00:00
const { getItem } = 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();
if (!isTablet) {
Orientation.lockToPortrait();
}
2020-04-20 13:09:19 +00:00
};
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) {
2020-04-22 19:58:19 +00:00
resetAttempts();
2020-04-22 16:34:53 +00:00
}
};
2020-04-17 19:11:06 +00:00
useEffect(() => {
2020-04-22 19:58:19 +00:00
EventEmitter.addEventListener(LOCAL_AUTHENTICATE_EMITTER, showScreenLock);
2020-04-20 13:09:19 +00:00
fetchPasscode();
2020-04-22 16:34:53 +00:00
checkOldSession();
2020-04-22 19:58:19 +00:00
return () => EventEmitter.removeListener(LOCAL_AUTHENTICATE_EMITTER);
2020-04-17 19:11:06 +00:00
}, []);
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-23 16:08:50 +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-23 16:08:50 +00:00
/> */}
2020-04-23 18:28:40 +00:00
<Passcode theme={theme} type={TYPE.ENTER} />
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));