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

318 lines
7.2 KiB
JavaScript
Raw Normal View History

2020-04-15 20:32:38 +00:00
import React from 'react';
import PropTypes from 'prop-types';
import {
2020-04-16 20:13:20 +00:00
StyleSheet, View, Switch, ScrollView, Text
2020-04-15 20:32:38 +00:00
} from 'react-native';
import { SafeAreaView } from 'react-navigation';
2020-04-16 13:52:56 +00:00
import { connect } from 'react-redux';
2020-04-20 17:36:11 +00:00
import RNUserDefaults from 'rn-user-defaults';
2020-04-15 20:32:38 +00:00
import I18n from '../i18n';
import { themedHeader } from '../utils/navigation';
import { withTheme } from '../theme';
import { themes, SWITCH_TRACK_COLOR } from '../constants/colors';
import sharedStyles from './Styles';
import StatusBar from '../containers/StatusBar';
import Separator from '../containers/Separator';
import ListItem from '../containers/ListItem';
import { CustomIcon } from '../lib/Icons';
import database from '../lib/database';
2020-04-20 17:36:11 +00:00
import { supportedBiometryLabel } from '../utils/localAuthentication';
2020-04-20 13:09:19 +00:00
import { DisclosureImage } from '../containers/DisclosureIndicator';
2020-04-22 19:58:19 +00:00
import { PASSCODE_KEY } from '../constants/localAuthentication';
2020-04-20 13:09:19 +00:00
2020-04-15 20:32:38 +00:00
const DEFAULT_AUTO_LOCK = [
{
title: 'After 1 minute',
2020-04-20 13:09:19 +00:00
value: 5
2020-04-15 20:32:38 +00:00
},
{
title: 'After 5 minutes',
value: 300
},
{
title: 'After 15 minutes',
value: 900
},
{
title: 'After 30 minutes',
value: 1800
},
{
title: 'After 1 hour',
value: 3600
}
];
const styles = StyleSheet.create({
listPadding: {
paddingVertical: 36
},
sectionSeparatorBorder: {
...sharedStyles.separatorVertical,
height: 36
2020-04-16 20:13:20 +00:00
},
infoContainer: {
padding: 15
},
infoText: {
fontSize: 14,
...sharedStyles.textRegular
2020-04-16 13:52:56 +00:00
}
2020-04-15 20:32:38 +00:00
});
2020-04-16 20:13:20 +00:00
const ItemInfo = React.memo(({ info, theme }) => (
<View style={[styles.infoContainer, { backgroundColor: themes[theme].auxiliaryBackground }]}>
<Text style={[styles.infoText, { color: themes[theme].infoText }]}>{info}</Text>
</View>
));
ItemInfo.propTypes = {
info: PropTypes.string,
theme: PropTypes.string
};
2020-04-15 20:32:38 +00:00
const SectionSeparator = React.memo(({ theme }) => (
<View
style={[
styles.sectionSeparatorBorder,
{
borderColor: themes[theme].separatorColor,
backgroundColor: themes[theme].auxiliaryBackground
}
]}
/>
));
SectionSeparator.propTypes = {
theme: PropTypes.string
};
class ScreenLockConfigView extends React.Component {
static navigationOptions = ({ screenProps }) => ({
title: I18n.t('Screen_lock'),
...themedHeader(screenProps.theme)
})
static propTypes = {
2020-04-20 17:36:11 +00:00
theme: PropTypes.string,
server: PropTypes.string,
navigation: PropTypes.object
2020-04-15 20:32:38 +00:00
}
constructor(props) {
super(props);
this.state = {
autoLock: false,
autoLockTime: null,
2020-04-16 14:30:00 +00:00
supported: [],
2020-04-20 17:36:11 +00:00
biometry: true,
biometryLabel: null
2020-04-15 20:32:38 +00:00
};
this.init();
}
init = async() => {
const { server } = this.props;
const serversDB = database.servers;
const serversCollection = serversDB.collections.get('servers');
try {
this.serverRecord = await serversCollection.find(server);
2020-04-20 17:36:11 +00:00
this.setState({
autoLock: this.serverRecord?.autoLock,
autoLockTime: this.serverRecord?.autoLockTime || 1800,
biometry: this.serverRecord?.biometry || true
});
2020-04-15 20:32:38 +00:00
} catch (error) {
// TODO: raise error in case server wasn't found and pop?
}
2020-04-16 14:30:00 +00:00
2020-04-20 17:36:11 +00:00
const biometryLabel = await supportedBiometryLabel();
this.setState({ biometryLabel });
2020-04-15 20:32:38 +00:00
}
save = async() => {
2020-04-20 17:36:11 +00:00
const { autoLock, autoLockTime, biometry } = this.state;
2020-04-15 20:32:38 +00:00
const serversDB = database.servers;
await serversDB.action(async() => {
await this.serverRecord?.update((record) => {
record.autoLock = autoLock;
record.autoLockTime = autoLockTime;
2020-04-20 17:36:11 +00:00
record.biometry = biometry;
2020-04-15 20:32:38 +00:00
});
});
}
2020-04-20 13:09:19 +00:00
setPasscode = async() => {
const { autoLock } = this.state;
const { navigation } = this.props;
if (autoLock) {
const storedPasscode = await RNUserDefaults.get(PASSCODE_KEY);
if (!storedPasscode) {
navigation.navigate('ChangePasscodeView', { forceSetPasscode: true });
}
}
}
2020-04-15 20:32:38 +00:00
autoLock = () => {
2020-04-20 13:09:19 +00:00
this.setState(({ autoLock }) => ({ autoLock: !autoLock }), () => {
this.save();
this.setPasscode();
});
2020-04-15 20:32:38 +00:00
}
2020-04-20 17:36:11 +00:00
toggleBiometry = () => {
this.setState(({ biometry }) => ({ biometry: !biometry }), () => this.save());
}
2020-04-15 20:32:38 +00:00
isSelected = (value) => {
const { autoLockTime } = this.state;
return autoLockTime === value;
}
changeAutoLockTime = (autoLockTime) => {
this.setState({ autoLockTime }, () => this.save());
}
2020-04-20 13:09:19 +00:00
changePasscode = () => {
const { navigation } = this.props;
navigation.navigate('ChangePasscodeView');
}
2020-04-15 20:32:38 +00:00
renderSeparator = () => {
const { theme } = this.props;
return <Separator theme={theme} />;
}
renderIcon = () => {
const { theme } = this.props;
return <CustomIcon name='check' size={20} color={themes[theme].tintColor} />;
}
renderItem = ({ item }) => {
const { theme } = this.props;
const { title, value } = item;
return (
<>
<ListItem
title={title}
onPress={() => this.changeAutoLockTime(value)}
right={this.isSelected(value) ? this.renderIcon : null}
theme={theme}
/>
<Separator theme={theme} />
</>
);
}
2020-04-20 17:36:11 +00:00
renderAutoLockSwitch = () => {
2020-04-15 20:32:38 +00:00
const { autoLock } = this.state;
return (
<Switch
value={autoLock}
trackColor={SWITCH_TRACK_COLOR}
onValueChange={this.autoLock}
/>
);
}
2020-04-20 17:36:11 +00:00
renderBiometrySwitch = () => {
const { biometry } = this.state;
return (
<Switch
value={biometry}
trackColor={SWITCH_TRACK_COLOR}
onValueChange={this.toggleBiometry}
/>
);
}
2020-04-15 20:32:38 +00:00
renderAutoLockItems = () => {
const { autoLock, supported } = this.state;
const { theme } = this.props;
if (!autoLock) {
return null;
}
return (
<>
<View style={{ height: 36 }} />
<Separator theme={theme} />
{autoLock ? DEFAULT_AUTO_LOCK.concat(supported).map(item => this.renderItem({ item })) : null}
</>
);
}
2020-04-20 13:09:19 +00:00
renderDisclosure = () => {
const { theme } = this.props;
return <DisclosureImage theme={theme} />;
}
2020-04-20 17:36:11 +00:00
renderBiometry = () => {
const { autoLock, biometryLabel } = this.state;
const { theme } = this.props;
if (!autoLock || !biometryLabel) {
return null;
}
return (
<>
<Separator theme={theme} />
<ListItem
title={`Unlock with ${ biometryLabel }`}
right={() => this.renderBiometrySwitch()}
theme={theme}
/>
<Separator theme={theme} />
</>
);
}
2020-04-15 20:32:38 +00:00
render() {
2020-04-20 17:36:11 +00:00
const { autoLock } = this.state;
2020-04-15 20:32:38 +00:00
const { theme } = this.props;
return (
<SafeAreaView
style={[sharedStyles.container, { backgroundColor: themes[theme].auxiliaryBackground }]}
forceInset={{ vertical: 'never' }}
>
<StatusBar theme={theme} />
<ScrollView
keyExtractor={item => item.value}
contentContainerStyle={styles.listPadding}
>
<Separator theme={theme} />
<ListItem
2020-04-16 20:13:20 +00:00
title='Unlock with Passcode'
2020-04-20 17:36:11 +00:00
right={() => this.renderAutoLockSwitch()}
2020-04-15 20:32:38 +00:00
theme={theme}
/>
2020-04-20 13:09:19 +00:00
{autoLock
? (
<>
<Separator theme={theme} />
<ListItem
title='Change Passcode'
theme={theme}
right={this.renderDisclosure}
onPress={this.changePasscode}
/>
</>
)
: null
}
2020-04-16 20:13:20 +00:00
<Separator theme={theme} />
<ItemInfo
info={'Note: if you forget the passcode, you\'ll need to delete and reinstall the app.'}
theme={theme}
/>
2020-04-20 17:36:11 +00:00
{this.renderBiometry()}
2020-04-15 20:32:38 +00:00
{this.renderAutoLockItems()}
</ScrollView>
</SafeAreaView>
);
}
}
const mapStateToProps = state => ({
server: state.server.server
});
export default connect(mapStateToProps)(withTheme(ScreenLockConfigView));