This commit is contained in:
Marcos Rodriguez Velez 2024-04-18 13:48:22 -04:00
parent 4ebf0932ab
commit 60990636f0
No known key found for this signature in database
GPG key ID: 6030B2F48CCE86D7
2 changed files with 98 additions and 60 deletions

View file

@ -1,4 +1,4 @@
import React, { createContext, useState, useContext, useEffect } from 'react'; import React, { createContext, useState, useContext, useEffect, useMemo, useCallback } from 'react';
import { useAsyncStorage } from '@react-native-async-storage/async-storage'; import { useAsyncStorage } from '@react-native-async-storage/async-storage';
import { FiatUnit, TFiatUnit } from '../../models/fiatUnit'; import { FiatUnit, TFiatUnit } from '../../models/fiatUnit';
import { getPreferredCurrency, initCurrencyDaemon } from '../../blue_modules/currency'; import { getPreferredCurrency, initCurrencyDaemon } from '../../blue_modules/currency';
@ -19,7 +19,7 @@ interface SettingsContextType {
isHandOffUseEnabled: boolean; isHandOffUseEnabled: boolean;
setIsHandOffUseEnabledAsyncStorage: (value: boolean) => Promise<void>; setIsHandOffUseEnabledAsyncStorage: (value: boolean) => Promise<void>;
isPrivacyBlurEnabled: boolean; isPrivacyBlurEnabled: boolean;
setIsPrivacyBlurEnabled: (value: boolean) => void; setIsPrivacyBlurEnabledState: (value: boolean) => void;
isAdvancedModeEnabled: boolean; isAdvancedModeEnabled: boolean;
setIsAdvancedModeEnabledStorage: (value: boolean) => Promise<void>; setIsAdvancedModeEnabledStorage: (value: boolean) => Promise<void>;
isDoNotTrackEnabled: boolean; isDoNotTrackEnabled: boolean;
@ -42,7 +42,7 @@ const defaultSettingsContext: SettingsContextType = {
isHandOffUseEnabled: false, isHandOffUseEnabled: false,
setIsHandOffUseEnabledAsyncStorage: async () => {}, setIsHandOffUseEnabledAsyncStorage: async () => {},
isPrivacyBlurEnabled: true, isPrivacyBlurEnabled: true,
setIsPrivacyBlurEnabled: () => {}, setIsPrivacyBlurEnabledState: () => {},
isAdvancedModeEnabled: false, isAdvancedModeEnabled: false,
setIsAdvancedModeEnabledStorage: async () => {}, setIsAdvancedModeEnabledStorage: async () => {},
isDoNotTrackEnabled: false, isDoNotTrackEnabled: false,
@ -132,51 +132,67 @@ export const SettingsProvider: React.FC<{ children: React.ReactNode }> = ({ chil
} }
}, [walletsInitialized]); }, [walletsInitialized]);
const setPreferredFiatCurrencyStorage = async (currency: TFiatUnit) => { const setPreferredFiatCurrencyStorage = useCallback(async (currency: TFiatUnit) => {
await setPreferredFiatCurrency(currency); await setPreferredFiatCurrency(currency);
setPreferredFiatCurrency(currency); setPreferredFiatCurrency(currency);
}; }, []);
const setLanguageStorage = async (newLanguage: string) => { const setLanguageStorage = useCallback(async (newLanguage: string) => {
await saveLanguage(newLanguage); await saveLanguage(newLanguage);
setLanguage(newLanguage); setLanguage(newLanguage);
}; }, []);
const setIsAdvancedModeEnabledStorage = async (value: boolean) => { const setIsAdvancedModeEnabledStorage = useCallback(
async (value: boolean) => {
await advancedModeStorage.setItem(JSON.stringify(value)); await advancedModeStorage.setItem(JSON.stringify(value));
setIsAdvancedModeEnabled(value); setIsAdvancedModeEnabled(value);
}; },
[advancedModeStorage],
);
const setDoNotTrackStorage = async (value: boolean) => { const setDoNotTrackStorage = useCallback(
async (value: boolean) => {
await doNotTrackStorage.setItem(JSON.stringify(value)); await doNotTrackStorage.setItem(JSON.stringify(value));
setIsDoNotTrackEnabled(value); setIsDoNotTrackEnabled(value);
}; },
[doNotTrackStorage],
);
const setIsHandOffUseEnabledAsyncStorage = async (value: boolean) => { const setIsHandOffUseEnabledAsyncStorage = useCallback(
async (value: boolean) => {
setIsHandOffUseEnabled(value); setIsHandOffUseEnabled(value);
await isHandOffUseEnabledStorage.setItem; await isHandOffUseEnabledStorage.setItem;
}; },
[isHandOffUseEnabledStorage.setItem],
);
const setIsWidgetBalanceDisplayAllowedStorage = async (value: boolean) => { const setIsWidgetBalanceDisplayAllowedStorage = useCallback(async (value: boolean) => {
await setBalanceDisplayAllowed(value); await setBalanceDisplayAllowed(value);
setIsWidgetBalanceDisplayAllowed(value); setIsWidgetBalanceDisplayAllowed(value);
}; }, []);
const setIsLegacyURv1EnabledStorage = async (value: boolean) => { const setIsLegacyURv1EnabledStorage = useCallback(async (value: boolean) => {
value ? await setUseURv1() : await clearUseURv1(); value ? await setUseURv1() : await clearUseURv1();
await setIsLegacyURv1Enabled(value); await setIsLegacyURv1Enabled(value);
}; }, []);
const setIsClipboardGetContentEnabledStorage = async (value: boolean) => { const setIsClipboardGetContentEnabledStorage = useCallback(async (value: boolean) => {
await BlueClipboard().setReadClipboardAllowed(value); await BlueClipboard().setReadClipboardAllowed(value);
setIsClipboardGetContentEnabled(value); setIsClipboardGetContentEnabled(value);
}; }, []);
const setIsQuickActionsEnabledStorage = async (value: boolean) => { const setIsQuickActionsEnabledStorage = useCallback(async (value: boolean) => {
// @ts-ignore: Fix later // @ts-ignore: Fix later
await DeviceQuickActions.setEnabled(value); await DeviceQuickActions.setEnabled(value);
setIsQuickActionsEnabled(value); setIsQuickActionsEnabled(value);
}; }, []);
const setIsPrivacyBlurEnabledState = useCallback((value: boolean) => {
setIsPrivacyBlurEnabled(value);
if (!value) {
presentAlert({ message: 'Privacy blur has been disabled.' });
}
}, []);
useEffect(() => { useEffect(() => {
console.log(`Privacy blur: ${isPrivacyBlurEnabled}`); console.log(`Privacy blur: ${isPrivacyBlurEnabled}`);
@ -185,7 +201,8 @@ export const SettingsProvider: React.FC<{ children: React.ReactNode }> = ({ chil
} }
}, [isPrivacyBlurEnabled]); }, [isPrivacyBlurEnabled]);
const value: SettingsContextType = { const value = useMemo(
() => ({
preferredFiatCurrency, preferredFiatCurrency,
setPreferredFiatCurrencyStorage, setPreferredFiatCurrencyStorage,
language, language,
@ -193,7 +210,7 @@ export const SettingsProvider: React.FC<{ children: React.ReactNode }> = ({ chil
isHandOffUseEnabled, isHandOffUseEnabled,
setIsHandOffUseEnabledAsyncStorage, setIsHandOffUseEnabledAsyncStorage,
isPrivacyBlurEnabled, isPrivacyBlurEnabled,
setIsPrivacyBlurEnabled, setIsPrivacyBlurEnabledState,
isAdvancedModeEnabled, isAdvancedModeEnabled,
setIsAdvancedModeEnabledStorage, setIsAdvancedModeEnabledStorage,
isDoNotTrackEnabled, isDoNotTrackEnabled,
@ -206,7 +223,30 @@ export const SettingsProvider: React.FC<{ children: React.ReactNode }> = ({ chil
setIsClipboardGetContentEnabledStorage, setIsClipboardGetContentEnabledStorage,
isQuickActionsEnabled, isQuickActionsEnabled,
setIsQuickActionsEnabledStorage, setIsQuickActionsEnabledStorage,
}; }),
[
preferredFiatCurrency,
setPreferredFiatCurrencyStorage,
language,
setLanguageStorage,
isHandOffUseEnabled,
setIsHandOffUseEnabledAsyncStorage,
isPrivacyBlurEnabled,
setIsPrivacyBlurEnabledState,
isAdvancedModeEnabled,
setIsAdvancedModeEnabledStorage,
isDoNotTrackEnabled,
setDoNotTrackStorage,
isWidgetBalanceDisplayAllowed,
setIsWidgetBalanceDisplayAllowedStorage,
isLegacyURv1Enabled,
setIsLegacyURv1EnabledStorage,
isClipboardGetContentEnabled,
setIsClipboardGetContentEnabledStorage,
isQuickActionsEnabled,
setIsQuickActionsEnabledStorage,
],
);
return <SettingsContext.Provider value={value}>{children}</SettingsContext.Provider>; return <SettingsContext.Provider value={value}>{children}</SettingsContext.Provider>;
}; };

View file

@ -5,7 +5,6 @@ import { openSettings } from 'react-native-permissions';
import navigationStyle from '../../components/navigationStyle'; import navigationStyle from '../../components/navigationStyle';
import { BlueText, BlueSpacing20, BlueCard, BlueHeaderDefaultSub, BlueSpacing40 } from '../../BlueComponents'; import { BlueText, BlueSpacing20, BlueCard, BlueHeaderDefaultSub, BlueSpacing40 } from '../../BlueComponents';
import loc from '../../loc'; import loc from '../../loc';
import DeviceQuickActions from '../../class/quick-actions';
import { BlueStorageContext } from '../../blue_modules/storage-context'; import { BlueStorageContext } from '../../blue_modules/storage-context';
import { useTheme } from '../../components/themes'; import { useTheme } from '../../components/themes';
import ListItem from '../../components/ListItem'; import ListItem from '../../components/ListItem';
@ -18,16 +17,17 @@ const SettingsPrivacy = () => {
const { const {
isDoNotTrackEnabled, isDoNotTrackEnabled,
setDoNotTrackStorage, setDoNotTrackStorage,
setIsPrivacyBlurEnabled, setIsPrivacyBlurEnabledState,
isWidgetBalanceDisplayAllowed, isWidgetBalanceDisplayAllowed,
setIsWidgetBalanceDisplayAllowedStorage, setIsWidgetBalanceDisplayAllowedStorage,
isClipboardGetContentEnabled, isClipboardGetContentEnabled,
setIsClipboardGetContentEnabledStorage, setIsClipboardGetContentEnabledStorage,
isQuickActionsEnabled,
setIsQuickActionsEnabledStorage,
} = useSettings(); } = useSettings();
const sections = Object.freeze({ ALL: 0, CLIPBOARDREAD: 1, QUICKACTION: 2, WIDGETS: 3 }); const sections = Object.freeze({ ALL: 0, CLIPBOARDREAD: 1, QUICKACTION: 2, WIDGETS: 3 });
const [isLoading, setIsLoading] = useState(sections.ALL); const [isLoading, setIsLoading] = useState(sections.ALL);
const [isQuickActionsEnabled, setIsQuickActionsEnabled] = useState(false);
const [storageIsEncrypted, setStorageIsEncrypted] = useState(true); const [storageIsEncrypted, setStorageIsEncrypted] = useState(true);
const [isPrivacyBlurEnabledTapped, setIsPrivacyBlurEnabledTapped] = useState(0); const [isPrivacyBlurEnabledTapped, setIsPrivacyBlurEnabledTapped] = useState(0);
const styleHooks = StyleSheet.create({ const styleHooks = StyleSheet.create({
@ -40,7 +40,6 @@ const SettingsPrivacy = () => {
(async () => { (async () => {
try { try {
setStorageIsEncrypted(await isStorageEncrypted()); setStorageIsEncrypted(await isStorageEncrypted());
setIsQuickActionsEnabled(await DeviceQuickActions.getEnabled());
} catch (e) { } catch (e) {
console.log(e); console.log(e);
} }
@ -63,8 +62,7 @@ const SettingsPrivacy = () => {
const onQuickActionsValueChange = async value => { const onQuickActionsValueChange = async value => {
setIsLoading(sections.QUICKACTION); setIsLoading(sections.QUICKACTION);
try { try {
await DeviceQuickActions.setEnabled(value); setIsQuickActionsEnabledStorage(value);
setIsQuickActionsEnabled(value);
} catch (e) { } catch (e) {
console.log(e); console.log(e);
} }
@ -92,15 +90,13 @@ const SettingsPrivacy = () => {
}; };
const onDisablePrivacyTapped = () => { const onDisablePrivacyTapped = () => {
setIsPrivacyBlurEnabled(!(isPrivacyBlurEnabledTapped >= 10)); setIsPrivacyBlurEnabledState(!(isPrivacyBlurEnabledTapped >= 10));
setIsPrivacyBlurEnabledTapped(prev => prev + 1); setIsPrivacyBlurEnabledTapped(prev => prev + 1);
}; };
return ( return (
<ScrollView style={[styles.root, stylesWithThemeHook.root]} contentInsetAdjustmentBehavior="automatic" automaticallyAdjustContentInsets> <ScrollView style={[styles.root, stylesWithThemeHook.root]} contentInsetAdjustmentBehavior="automatic" automaticallyAdjustContentInsets>
<Pressable accessibilityRole="button" onPress={onDisablePrivacyTapped}>
{Platform.OS === 'android' ? <BlueHeaderDefaultSub leftText={loc.settings.general} /> : <></>} {Platform.OS === 'android' ? <BlueHeaderDefaultSub leftText={loc.settings.general} /> : <></>}
</Pressable>
<ListItem <ListItem
hideChevron hideChevron
title={loc.settings.privacy_read_clipboard} title={loc.settings.privacy_read_clipboard}
@ -113,7 +109,9 @@ const SettingsPrivacy = () => {
}} }}
/> />
<BlueCard> <BlueCard>
<Pressable accessibilityRole="button" onPress={onDisablePrivacyTapped}>
<BlueText>{loc.settings.privacy_clipboard_explanation}</BlueText> <BlueText>{loc.settings.privacy_clipboard_explanation}</BlueText>
</Pressable>
</BlueCard> </BlueCard>
<BlueSpacing20 /> <BlueSpacing20 />
{!storageIsEncrypted && ( {!storageIsEncrypted && (