BlueWallet/components/Context/SettingsContext.tsx

250 lines
9.7 KiB
TypeScript
Raw Normal View History

2024-04-18 19:48:22 +02:00
import React, { createContext, useState, useContext, useEffect, useMemo, useCallback } from 'react';
2024-04-18 03:05:48 +02:00
import { useAsyncStorage } from '@react-native-async-storage/async-storage';
import { FiatUnit, TFiatUnit } from '../../models/fiatUnit';
import { getPreferredCurrency, initCurrencyDaemon } from '../../blue_modules/currency';
import { BlueApp } from '../../class';
import presentAlert from '../Alert';
import { STORAGE_KEY, saveLanguage } from '../../loc';
import { useStorage } from '../../blue_modules/storage-context';
import { isBalanceDisplayAllowed, setBalanceDisplayAllowed } from '../WidgetCommunication';
import { clearUseURv1, isURv1Enabled, setUseURv1 } from '../../blue_modules/ur';
import BlueClipboard from '../../blue_modules/clipboard';
import { getIsHandOffUseEnabled, setIsHandOffUseEnabled } from '../HandOffComponent';
2024-04-18 03:05:48 +02:00
import DeviceQuickActions from '../../class/quick-actions';
interface SettingsContextType {
preferredFiatCurrency: TFiatUnit;
setPreferredFiatCurrencyStorage: (currency: TFiatUnit) => Promise<void>;
language: string | undefined;
setLanguageStorage: (language: string) => Promise<void>;
isHandOffUseEnabled: boolean;
setIsHandOffUseEnabledAsyncStorage: (value: boolean) => Promise<void>;
isPrivacyBlurEnabled: boolean;
2024-04-18 19:48:22 +02:00
setIsPrivacyBlurEnabledState: (value: boolean) => void;
2024-04-18 03:05:48 +02:00
isAdvancedModeEnabled: boolean;
setIsAdvancedModeEnabledStorage: (value: boolean) => Promise<void>;
isDoNotTrackEnabled: boolean;
setDoNotTrackStorage: (value: boolean) => Promise<void>;
isWidgetBalanceDisplayAllowed: boolean;
setIsWidgetBalanceDisplayAllowedStorage: (value: boolean) => Promise<void>;
isLegacyURv1Enabled: boolean;
setIsLegacyURv1EnabledStorage: (value: boolean) => Promise<void>;
isClipboardGetContentEnabled: boolean;
setIsClipboardGetContentEnabledStorage: (value: boolean) => Promise<void>;
isQuickActionsEnabled: boolean;
setIsQuickActionsEnabledStorage: (value: boolean) => Promise<void>;
}
const defaultSettingsContext: SettingsContextType = {
preferredFiatCurrency: FiatUnit.USD,
setPreferredFiatCurrencyStorage: async () => {},
language: 'en',
setLanguageStorage: async () => {},
isHandOffUseEnabled: false,
setIsHandOffUseEnabledAsyncStorage: async () => {},
isPrivacyBlurEnabled: true,
2024-04-18 19:48:22 +02:00
setIsPrivacyBlurEnabledState: () => {},
2024-04-18 03:05:48 +02:00
isAdvancedModeEnabled: false,
setIsAdvancedModeEnabledStorage: async () => {},
isDoNotTrackEnabled: false,
setDoNotTrackStorage: async () => {},
isWidgetBalanceDisplayAllowed: true,
setIsWidgetBalanceDisplayAllowedStorage: async () => {},
setIsLegacyURv1EnabledStorage: async () => {},
isLegacyURv1Enabled: false,
isClipboardGetContentEnabled: true,
setIsClipboardGetContentEnabledStorage: async () => {},
isQuickActionsEnabled: true,
setIsQuickActionsEnabledStorage: async () => {},
};
export const SettingsContext = createContext<SettingsContextType>(defaultSettingsContext);
export const SettingsProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
// FiatCurrency
const [preferredFiatCurrency, setPreferredFiatCurrency] = useState<TFiatUnit>(FiatUnit.USD);
// Language
const [language, setLanguage] = useState<string>();
// HandOff
const [isHandOffUseEnabled, setHandOffUseEnabled] = useState<boolean>(false);
2024-04-18 03:05:48 +02:00
// PrivacyBlur
const [isPrivacyBlurEnabled, setIsPrivacyBlurEnabled] = useState<boolean>(true);
// AdvancedMode
const [isAdvancedModeEnabled, setIsAdvancedModeEnabled] = useState<boolean>(false);
// DoNotTrack
const [isDoNotTrackEnabled, setIsDoNotTrackEnabled] = useState<boolean>(false);
// WidgetCommunication
const [isWidgetBalanceDisplayAllowed, setIsWidgetBalanceDisplayAllowed] = useState<boolean>(true);
// LegacyURv1
const [isLegacyURv1Enabled, setIsLegacyURv1Enabled] = useState<boolean>(false);
// Clipboard
const [isClipboardGetContentEnabled, setIsClipboardGetContentEnabled] = useState<boolean>(false);
// Quick Actions
const [isQuickActionsEnabled, setIsQuickActionsEnabled] = useState<boolean>(true);
const advancedModeStorage = useAsyncStorage(BlueApp.ADVANCED_MODE_ENABLED);
const doNotTrackStorage = useAsyncStorage(BlueApp.DO_NOT_TRACK);
const languageStorage = useAsyncStorage(STORAGE_KEY);
const { walletsInitialized } = useStorage();
useEffect(() => {
const fetchSettings = async () => {
const advMode = await advancedModeStorage.getItem();
2024-04-20 00:49:13 +02:00
console.debug('SettingsContext advMode:', advMode);
const handOff = await getIsHandOffUseEnabled();
2024-04-20 00:49:13 +02:00
console.debug('SettingsContext handOff:', handOff);
setHandOffUseEnabled(handOff);
2024-04-18 03:05:48 +02:00
const lang = (await languageStorage.getItem()) ?? 'en';
2024-04-20 00:49:13 +02:00
console.debug('SettingsContext lang:', lang);
2024-04-18 03:05:48 +02:00
setIsAdvancedModeEnabled(advMode ? JSON.parse(advMode) : false);
const isBalanceDisplayAllowedStorage = await isBalanceDisplayAllowed();
2024-04-20 00:49:13 +02:00
console.debug('SettingsContext isBalanceDisplayAllowed:', isBalanceDisplayAllowedStorage);
2024-04-18 03:05:48 +02:00
setIsWidgetBalanceDisplayAllowed(isBalanceDisplayAllowedStorage);
setLanguage(lang);
const isURv1EnabledStorage = await isURv1Enabled();
2024-04-20 00:49:13 +02:00
console.debug('SettingsContext isURv1Enabled:', isURv1EnabledStorage);
2024-04-18 03:05:48 +02:00
setIsLegacyURv1EnabledStorage(isURv1EnabledStorage);
const isClipboardGetContentEnabledStorage = await BlueClipboard().isReadClipboardAllowed();
2024-04-20 00:49:13 +02:00
console.debug('SettingsContext isClipboardGetContentEnabled:', isClipboardGetContentEnabledStorage);
2024-04-18 03:05:48 +02:00
setIsClipboardGetContentEnabledStorage(isClipboardGetContentEnabledStorage);
// @ts-ignore: Fix later
const isQuickActionsEnabledStorage = await DeviceQuickActions.getEnabled();
2024-04-20 00:49:13 +02:00
console.debug('SettingsContext isQuickActionsEnabled:', isQuickActionsEnabledStorage);
2024-04-18 03:05:48 +02:00
setIsQuickActionsEnabledStorage(isQuickActionsEnabledStorage);
};
fetchSettings();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
useEffect(() => {
if (walletsInitialized) {
initCurrencyDaemon().finally(() => {
getPreferredCurrency().then(currency => {
2024-04-20 00:49:13 +02:00
console.debug('SettingsContext currency:', currency);
2024-04-18 03:05:48 +02:00
setPreferredFiatCurrency(FiatUnit[currency.endPointKey]);
});
});
}
}, [walletsInitialized]);
2024-04-18 19:48:22 +02:00
const setPreferredFiatCurrencyStorage = useCallback(async (currency: TFiatUnit) => {
2024-04-18 03:05:48 +02:00
await setPreferredFiatCurrency(currency);
setPreferredFiatCurrency(currency);
2024-04-18 19:48:22 +02:00
}, []);
2024-04-18 03:05:48 +02:00
2024-04-18 19:48:22 +02:00
const setLanguageStorage = useCallback(async (newLanguage: string) => {
2024-04-18 03:05:48 +02:00
await saveLanguage(newLanguage);
setLanguage(newLanguage);
2024-04-18 19:48:22 +02:00
}, []);
2024-04-18 03:05:48 +02:00
2024-04-18 19:48:22 +02:00
const setIsAdvancedModeEnabledStorage = useCallback(
async (value: boolean) => {
await advancedModeStorage.setItem(JSON.stringify(value));
setIsAdvancedModeEnabled(value);
},
[advancedModeStorage],
);
const setDoNotTrackStorage = useCallback(
async (value: boolean) => {
await doNotTrackStorage.setItem(JSON.stringify(value));
setIsDoNotTrackEnabled(value);
},
[doNotTrackStorage],
);
const setIsHandOffUseEnabledAsyncStorage = useCallback(async (value: boolean) => {
console.debug('setIsHandOffUseEnabledAsyncStorage', value);
await setIsHandOffUseEnabled(value);
setHandOffUseEnabled(value);
}, []);
2024-04-18 19:48:22 +02:00
const setIsWidgetBalanceDisplayAllowedStorage = useCallback(async (value: boolean) => {
2024-04-18 03:05:48 +02:00
await setBalanceDisplayAllowed(value);
setIsWidgetBalanceDisplayAllowed(value);
2024-04-18 19:48:22 +02:00
}, []);
2024-04-18 03:05:48 +02:00
2024-04-18 19:48:22 +02:00
const setIsLegacyURv1EnabledStorage = useCallback(async (value: boolean) => {
2024-04-18 03:05:48 +02:00
value ? await setUseURv1() : await clearUseURv1();
await setIsLegacyURv1Enabled(value);
2024-04-18 19:48:22 +02:00
}, []);
2024-04-18 03:05:48 +02:00
2024-04-18 19:48:22 +02:00
const setIsClipboardGetContentEnabledStorage = useCallback(async (value: boolean) => {
2024-04-18 03:05:48 +02:00
await BlueClipboard().setReadClipboardAllowed(value);
setIsClipboardGetContentEnabled(value);
2024-04-18 19:48:22 +02:00
}, []);
2024-04-18 03:05:48 +02:00
2024-04-18 19:48:22 +02:00
const setIsQuickActionsEnabledStorage = useCallback(async (value: boolean) => {
2024-04-18 03:05:48 +02:00
// @ts-ignore: Fix later
await DeviceQuickActions.setEnabled(value);
setIsQuickActionsEnabled(value);
2024-04-18 19:48:22 +02:00
}, []);
2024-04-18 19:50:49 +02:00
const setIsPrivacyBlurEnabledState = useCallback(
(value: boolean) => {
setIsPrivacyBlurEnabled(value);
2024-04-20 00:49:13 +02:00
console.debug(`Privacy blur: ${isPrivacyBlurEnabled}`);
2024-04-18 19:50:49 +02:00
if (!value) {
presentAlert({ message: 'Privacy blur has been disabled.' });
}
},
[isPrivacyBlurEnabled],
);
2024-04-18 03:05:48 +02:00
2024-04-18 19:48:22 +02:00
const value = useMemo(
() => ({
preferredFiatCurrency,
setPreferredFiatCurrencyStorage,
language,
setLanguageStorage,
isHandOffUseEnabled,
setIsHandOffUseEnabledAsyncStorage,
isPrivacyBlurEnabled,
setIsPrivacyBlurEnabledState,
isAdvancedModeEnabled,
setIsAdvancedModeEnabledStorage,
isDoNotTrackEnabled,
setDoNotTrackStorage,
isWidgetBalanceDisplayAllowed,
setIsWidgetBalanceDisplayAllowedStorage,
isLegacyURv1Enabled,
setIsLegacyURv1EnabledStorage,
isClipboardGetContentEnabled,
setIsClipboardGetContentEnabledStorage,
isQuickActionsEnabled,
setIsQuickActionsEnabledStorage,
}),
[
preferredFiatCurrency,
setPreferredFiatCurrencyStorage,
language,
setLanguageStorage,
isHandOffUseEnabled,
setIsHandOffUseEnabledAsyncStorage,
isPrivacyBlurEnabled,
setIsPrivacyBlurEnabledState,
isAdvancedModeEnabled,
setIsAdvancedModeEnabledStorage,
isDoNotTrackEnabled,
setDoNotTrackStorage,
isWidgetBalanceDisplayAllowed,
setIsWidgetBalanceDisplayAllowedStorage,
isLegacyURv1Enabled,
setIsLegacyURv1EnabledStorage,
isClipboardGetContentEnabled,
setIsClipboardGetContentEnabledStorage,
isQuickActionsEnabled,
setIsQuickActionsEnabledStorage,
],
);
2024-04-18 03:05:48 +02:00
return <SettingsContext.Provider value={value}>{children}</SettingsContext.Provider>;
};
export const useSettings = () => useContext(SettingsContext);