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

95 lines
2.2 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-20 13:09:19 +00:00
View, StyleSheet
2020-04-13 20:36:39 +00:00
} from 'react-native';
2020-04-17 19:11:06 +00:00
import PINCode from '@haskkor/react-native-pincode';
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-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
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center'
},
text: {
fontSize: 16,
paddingTop: 10,
...sharedStyles.textRegular,
...sharedStyles.textAlignCenter
}
});
2020-04-17 19:11:06 +00:00
const ScreenLockedView = React.memo(withTheme(({ theme, split }) => {
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({});
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
useEffect(() => {
EventEmitter.addEventListener(LOCAL_AUTHENTICATE, showScreenLock);
2020-04-20 13:09:19 +00:00
fetchPasscode();
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 }}
>
<View style={[styles.container, { backgroundColor: themes[theme].backgroundColor }]}>
<PINCode
2020-04-20 13:09:19 +00:00
status='enter'
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}
// maxAttempts={3}
2020-04-17 19:11:06 +00:00
/>
</View>
</Modal>
2020-04-13 20:36:39 +00:00
);
}));
2020-04-17 19:11:06 +00:00
export default withSplit(withTheme(ScreenLockedView));