Update encryptStorage.js (#6218)

This commit is contained in:
Marcos Rodriguez Vélez 2024-03-02 12:50:26 -04:00 committed by GitHub
parent df4f93a554
commit 2aa737942a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,8 +1,8 @@
import React, { useEffect, useState, useContext } from 'react'; import React, { useEffect, useState, useCallback, useContext } from 'react';
import { View, ScrollView, Alert, TouchableOpacity, TouchableWithoutFeedback, Text, StyleSheet, Platform } from 'react-native'; import { View, ScrollView, Alert, TouchableOpacity, TouchableWithoutFeedback, Text, StyleSheet, Platform } from 'react-native';
import { useNavigation } from '@react-navigation/native'; import { useNavigation } from '@react-navigation/native';
import navigationStyle from '../../components/navigationStyle'; import navigationStyle from '../../components/navigationStyle';
import { BlueSpacing20, BlueCard, BlueText } from '../../BlueComponents'; import { BlueLoading, BlueSpacing20, BlueCard, BlueText } from '../../BlueComponents';
import Biometric from '../../class/biometrics'; import Biometric from '../../class/biometrics';
import loc from '../../loc'; import loc from '../../loc';
import { BlueStorageContext } from '../../blue_modules/storage-context'; import { BlueStorageContext } from '../../blue_modules/storage-context';
@ -14,11 +14,11 @@ const prompt = require('../../helpers/prompt');
const EncryptStorage = () => { const EncryptStorage = () => {
const { isStorageEncrypted, encryptStorage, decryptStorage, saveToDisk } = useContext(BlueStorageContext); const { isStorageEncrypted, encryptStorage, decryptStorage, saveToDisk } = useContext(BlueStorageContext);
const [isLoading, setIsLoading] = useState(true);
const [biometrics, setBiometrics] = useState({ isDeviceBiometricCapable: false, isBiometricsEnabled: false, biometricsType: '' }); const [biometrics, setBiometrics] = useState({ isDeviceBiometricCapable: false, isBiometricsEnabled: false, biometricsType: '' });
const [storageIsEncryptedSwitchEnabled, setStorageIsEncryptedSwitchEnabled] = useState(false); const [storageIsEncryptedSwitchEnabled, setStorageIsEncryptedSwitchEnabled] = useState(false);
const { navigate, popToTop } = useNavigation(); const { navigate, popToTop } = useNavigation();
const { colors } = useTheme(); const { colors } = useTheme();
const [isLoading, setIsLoading] = useState({ encryptStorage: false, biometrics: false });
const styleHooks = StyleSheet.create({ const styleHooks = StyleSheet.create({
root: { root: {
backgroundColor: colors.background, backgroundColor: colors.background,
@ -28,15 +28,16 @@ const EncryptStorage = () => {
}, },
}); });
const initialState = async () => { const initialState = useCallback(async () => {
const isBiometricsEnabled = await Biometric.isBiometricUseEnabled(); const isBiometricsEnabled = await Biometric.isBiometricUseEnabled();
const isDeviceBiometricCapable = await Biometric.isDeviceBiometricCapable(); const isDeviceBiometricCapable = await Biometric.isDeviceBiometricCapable();
const biometricsType = (await Biometric.biometricType()) || loc.settings.biometrics; const biometricsType = (await Biometric.biometricType()) || loc.settings.biometrics;
const isStorageEncryptedSwitchEnabled = await isStorageEncrypted(); const isStorageEncryptedSwitchEnabled = await isStorageEncrypted();
setStorageIsEncryptedSwitchEnabled(isStorageEncryptedSwitchEnabled); setStorageIsEncryptedSwitchEnabled(isStorageEncryptedSwitchEnabled);
setBiometrics({ isBiometricsEnabled, isDeviceBiometricCapable, biometricsType }); setBiometrics({ isBiometricsEnabled, isDeviceBiometricCapable, biometricsType });
}; setIsLoading(false);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
useEffect(() => { useEffect(() => {
initialState(); initialState();
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
@ -62,26 +63,26 @@ const EncryptStorage = () => {
}; };
const onEncryptStorageSwitch = async value => { const onEncryptStorageSwitch = async value => {
setIsLoading(prev => ({ ...prev, encryptStorage: true })); setIsLoading(true);
if (value === true) { if (value === true) {
let p1 = await prompt(loc.settings.password, loc.settings.password_explain).catch(() => { let p1 = await prompt(loc.settings.password, loc.settings.password_explain).catch(() => {
setIsLoading(prev => ({ ...prev, encryptStorage: false })); setIsLoading(false);
p1 = undefined; p1 = undefined;
}); });
if (!p1) { if (!p1) {
setIsLoading(prev => ({ ...prev, encryptStorage: false })); setIsLoading(false);
return; return;
} }
const p2 = await prompt(loc.settings.password, loc.settings.retype_password).catch(() => { const p2 = await prompt(loc.settings.password, loc.settings.retype_password).catch(() => {
setIsLoading(prev => ({ ...prev, encryptStorage: false })); setIsLoading(false);
}); });
if (p1 === p2) { if (p1 === p2) {
await encryptStorage(p1); await encryptStorage(p1);
setIsLoading(false);
setStorageIsEncryptedSwitchEnabled(await isStorageEncrypted()); setStorageIsEncryptedSwitchEnabled(await isStorageEncrypted());
saveToDisk(); saveToDisk();
setIsLoading(prev => ({ ...prev, encryptStorage: false }));
} else { } else {
setIsLoading(prev => ({ ...prev, encryptStorage: false })); setIsLoading(false);
triggerHapticFeedback(HapticFeedbackTypes.NotificationError); triggerHapticFeedback(HapticFeedbackTypes.NotificationError);
presentAlert({ message: loc.settings.passwords_do_not_match }); presentAlert({ message: loc.settings.passwords_do_not_match });
} }
@ -93,7 +94,7 @@ const EncryptStorage = () => {
{ {
text: loc._.cancel, text: loc._.cancel,
style: 'cancel', style: 'cancel',
onPress: () => setIsLoading(prev => ({ ...prev, encryptStorage: false })), onPress: () => setIsLoading(false),
}, },
{ {
text: loc._.ok, text: loc._.ok,
@ -107,7 +108,6 @@ const EncryptStorage = () => {
}; };
const onUseBiometricSwitch = async value => { const onUseBiometricSwitch = async value => {
setIsLoading(prev => ({ ...prev, biometrics: true }));
const isBiometricsEnabled = { const isBiometricsEnabled = {
isDeviceBiometricCapable: biometrics.isDeviceBiometricCapable, isDeviceBiometricCapable: biometrics.isDeviceBiometricCapable,
isBiometricsEnabled: biometrics.isBiometricsEnabled, isBiometricsEnabled: biometrics.isBiometricsEnabled,
@ -118,7 +118,6 @@ const EncryptStorage = () => {
await Biometric.setBiometricUseEnabled(value); await Biometric.setBiometricUseEnabled(value);
setBiometrics(isBiometricsEnabled); setBiometrics(isBiometricsEnabled);
} }
setIsLoading(prev => ({ ...prev, biometrics: false }));
}; };
const navigateToPlausibleDeniability = () => { const navigateToPlausibleDeniability = () => {
@ -127,26 +126,28 @@ const EncryptStorage = () => {
const renderPasscodeExplanation = () => { const renderPasscodeExplanation = () => {
let isCapable = true; let isCapable = true;
if (Platform.OS === 'android') { if (Platform.OS === 'android') {
if (Platform.Version < 30) { if (Platform.Version < 30) {
isCapable = false; isCapable = false;
} }
} }
return isCapable ? ( return isCapable ? (
<> <>
<BlueText /> <BlueText />
<BlueText>{loc.formatString(loc.settings.biometrics_fail, { type: biometrics.biometricsType })}</BlueText> <BlueText>{loc.formatString(loc.settings.biometrics_fail, { type: biometrics.biometricsType })}</BlueText>
</> </>
) : null; ) : null;
}; };
return ( return isLoading ? (
<ScrollView <ScrollView centerContent>
contentContainerStyle={styleHooks.root} <BlueLoading />
automaticallyAdjustContentInsets </ScrollView>
contentInsetAdjustmentBehavior="automatic" ) : (
centerContent={isLoading} <ScrollView contentContainerStyle={styles.root} automaticallyAdjustContentInsets contentInsetAdjustmentBehavior="automatic">
>
<View style={styles.paddingTop} /> <View style={styles.paddingTop} />
{biometrics.isDeviceBiometricCapable && ( {biometrics.isDeviceBiometricCapable && (
<> <>
@ -156,13 +157,7 @@ const EncryptStorage = () => {
<ListItem <ListItem
title={loc.formatString(loc.settings.encrypt_use, { type: biometrics.biometricsType })} title={loc.formatString(loc.settings.encrypt_use, { type: biometrics.biometricsType })}
Component={TouchableWithoutFeedback} Component={TouchableWithoutFeedback}
isLoading={isLoading.biometrics} switch={{ value: biometrics.isBiometricsEnabled, onValueChange: onUseBiometricSwitch }}
containerStyle={[styles.rowItemContainerStyle, styleHooks.root]}
switch={{
value: biometrics.isBiometricsEnabled,
onValueChange: onUseBiometricSwitch,
disabled: isLoading.encryptStorage || isLoading.biometrics,
}}
/> />
<BlueCard> <BlueCard>
<BlueText>{loc.formatString(loc.settings.encrypt_use_expl, { type: biometrics.biometricsType })}</BlueText> <BlueText>{loc.formatString(loc.settings.encrypt_use_expl, { type: biometrics.biometricsType })}</BlueText>
@ -175,17 +170,11 @@ const EncryptStorage = () => {
{loc.settings.encrypt_tstorage} {loc.settings.encrypt_tstorage}
</Text> </Text>
<ListItem <ListItem
testID="EncryptedAndPasswordProtected" testID="EncyptedAndPasswordProtected"
hideChevron hideChevron
isLoading={isLoading.encryptStorage}
title={loc.settings.encrypt_enc_and_pass} title={loc.settings.encrypt_enc_and_pass}
Component={TouchableWithoutFeedback} Component={TouchableWithoutFeedback}
containerStyle={[styles.rowItemContainerStyle, styleHooks.root]} switch={{ onValueChange: onEncryptStorageSwitch, value: storageIsEncryptedSwitchEnabled }}
switch={{
onValueChange: onEncryptStorageSwitch,
value: storageIsEncryptedSwitchEnabled,
disabled: isLoading.encryptStorage || isLoading.biometrics,
}}
/> />
{storageIsEncryptedSwitchEnabled && ( {storageIsEncryptedSwitchEnabled && (
<ListItem <ListItem
@ -201,15 +190,15 @@ const EncryptStorage = () => {
}; };
const styles = StyleSheet.create({ const styles = StyleSheet.create({
paddingTop: { paddingTop: 36 }, root: {
flex: 1,
},
paddingTop: { paddingTop: 19 },
headerText: { headerText: {
fontWeight: 'bold', fontWeight: 'bold',
fontSize: 30, fontSize: 30,
marginLeft: 17, marginLeft: 17,
}, },
rowItemContainerStyle: {
minHeight: 60,
},
}); });
export default EncryptStorage; export default EncryptStorage;