2020-05-08 17:04:37 +00:00
|
|
|
import React, { useEffect, useState } from 'react';
|
|
|
|
import { StyleSheet } from 'react-native';
|
|
|
|
import Orientation from 'react-native-orientation-locker';
|
|
|
|
import useDeepCompareEffect from 'use-deep-compare-effect';
|
2021-02-01 17:18:55 +00:00
|
|
|
import isEmpty from 'lodash/isEmpty';
|
2020-05-08 17:04:37 +00:00
|
|
|
import Modal from 'react-native-modal';
|
|
|
|
import Touchable from 'react-native-platform-touchable';
|
|
|
|
|
2021-11-10 20:50:38 +00:00
|
|
|
import { useTheme } from '../theme';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { hasNotch, isTablet } from '../utils/deviceInfo';
|
2020-05-08 17:04:37 +00:00
|
|
|
import { PasscodeChoose } from '../containers/Passcode';
|
|
|
|
import EventEmitter from '../utils/events';
|
|
|
|
import { CustomIcon } from '../lib/Icons';
|
|
|
|
import { CHANGE_PASSCODE_EMITTER } from '../constants/localAuthentication';
|
|
|
|
import { themes } from '../constants/colors';
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
modal: {
|
|
|
|
margin: 0
|
|
|
|
},
|
|
|
|
close: {
|
|
|
|
position: 'absolute',
|
|
|
|
top: hasNotch ? 50 : 30,
|
|
|
|
left: 15
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-11-10 20:50:38 +00:00
|
|
|
interface IArgs {
|
|
|
|
submit(passcode: string): void;
|
|
|
|
cancel(): void;
|
|
|
|
force: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ChangePasscodeView = React.memo(() => {
|
2020-05-08 17:04:37 +00:00
|
|
|
const [visible, setVisible] = useState(false);
|
2021-11-10 20:50:38 +00:00
|
|
|
const [data, setData] = useState<Partial<IArgs>>({});
|
|
|
|
|
|
|
|
const { theme } = useTheme();
|
2020-05-08 17:04:37 +00:00
|
|
|
|
|
|
|
useDeepCompareEffect(() => {
|
2021-02-01 17:18:55 +00:00
|
|
|
if (!isEmpty(data)) {
|
2020-05-08 17:04:37 +00:00
|
|
|
setVisible(true);
|
|
|
|
} else {
|
|
|
|
setVisible(false);
|
|
|
|
}
|
|
|
|
}, [data]);
|
|
|
|
|
2021-11-10 20:50:38 +00:00
|
|
|
const showChangePasscode = (args: IArgs) => {
|
2020-05-08 17:04:37 +00:00
|
|
|
setData(args);
|
|
|
|
};
|
|
|
|
|
2021-11-10 20:50:38 +00:00
|
|
|
const onSubmit = (passcode: string) => {
|
2020-05-08 17:04:37 +00:00
|
|
|
const { submit } = data;
|
|
|
|
if (submit) {
|
|
|
|
submit(passcode);
|
|
|
|
}
|
|
|
|
setData({});
|
|
|
|
};
|
|
|
|
|
|
|
|
const onCancel = () => {
|
|
|
|
const { cancel } = data;
|
|
|
|
if (cancel) {
|
|
|
|
cancel();
|
|
|
|
}
|
|
|
|
setData({});
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!isTablet) {
|
|
|
|
Orientation.lockToPortrait();
|
|
|
|
}
|
2021-02-23 19:00:39 +00:00
|
|
|
const listener = EventEmitter.addEventListener(CHANGE_PASSCODE_EMITTER, showChangePasscode);
|
2021-09-13 20:41:05 +00:00
|
|
|
return () => {
|
2020-05-08 17:04:37 +00:00
|
|
|
if (!isTablet) {
|
|
|
|
Orientation.unlockAllOrientations();
|
|
|
|
}
|
2021-02-23 19:00:39 +00:00
|
|
|
EventEmitter.removeListener(CHANGE_PASSCODE_EMITTER, listener);
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-05-08 17:04:37 +00:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
return (
|
2021-09-13 20:41:05 +00:00
|
|
|
<Modal useNativeDriver isVisible={visible} hideModalContentWhileAnimating style={styles.modal}>
|
2022-01-17 14:18:32 +00:00
|
|
|
<PasscodeChoose theme={theme!} finishProcess={onSubmit} force={data?.force} />
|
2021-09-13 20:41:05 +00:00
|
|
|
{!data?.force ? (
|
|
|
|
<Touchable onPress={onCancel} style={styles.close}>
|
2022-01-17 14:18:32 +00:00
|
|
|
<CustomIcon name='close' color={themes[theme!].passcodePrimary} size={30} />
|
2021-09-13 20:41:05 +00:00
|
|
|
</Touchable>
|
|
|
|
) : null}
|
2020-05-08 17:04:37 +00:00
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-11-10 20:50:38 +00:00
|
|
|
export default ChangePasscodeView;
|