BlueWallet/components/Context/SettingsProvider.tsx

411 lines
16 KiB
TypeScript
Raw Permalink Normal View History

import React, { createContext, useCallback, useEffect, useMemo, useState } from 'react';
2024-05-20 11:54:13 +02:00
import DefaultPreference from 'react-native-default-preference';
2024-11-10 15:25:39 +01:00
import { isReadClipboardAllowed, setReadClipboardAllowed } from '../../blue_modules/clipboard';
2024-11-01 03:25:47 +01:00
import { getPreferredCurrency, GROUP_IO_BLUEWALLET, initCurrencyDaemon, PREFERRED_CURRENCY_STORAGE_KEY } from '../../blue_modules/currency';
2024-04-18 03:05:48 +02:00
import { clearUseURv1, isURv1Enabled, setUseURv1 } from '../../blue_modules/ur';
2024-05-20 11:54:13 +02:00
import { BlueApp } from '../../class';
import { saveLanguage, STORAGE_KEY } from '../../loc';
import { FiatUnit, TFiatUnit } from '../../models/fiatUnit';
2024-11-14 16:30:19 +01:00
import {
getEnabled as getIsDeviceQuickActionsEnabled,
setEnabled as setIsDeviceQuickActionsEnabled,
} from '../../hooks/useDeviceQuickActions';
2024-05-20 11:54:13 +02:00
import { getIsHandOffUseEnabled, setIsHandOffUseEnabled } from '../HandOffComponent';
import { useStorage } from '../../hooks/context/useStorage';
2024-08-22 17:50:12 +02:00
import { BitcoinUnit } from '../../models/bitcoinUnits';
import { TotalWalletsBalanceKey, TotalWalletsBalancePreferredUnit } from '../TotalWalletsBalance';
2024-09-26 08:36:51 +02:00
import { BLOCK_EXPLORERS, getBlockExplorerUrl, saveBlockExplorer, BlockExplorer, normalizeUrl } from '../../models/blockExplorer';
import * as BlueElectrum from '../../blue_modules/BlueElectrum';
2024-11-09 02:07:35 +01:00
import { isBalanceDisplayAllowed, setBalanceDisplayAllowed } from '../../hooks/useWidgetCommunication';
2024-11-10 15:25:39 +01:00
import AsyncStorage from '@react-native-async-storage/async-storage';
2024-08-22 17:50:12 +02:00
2024-10-31 22:48:36 +01:00
const getDoNotTrackStorage = async (): Promise<boolean> => {
2024-11-01 03:25:47 +01:00
try {
await DefaultPreference.setName(GROUP_IO_BLUEWALLET);
const doNotTrack = await DefaultPreference.get(BlueApp.DO_NOT_TRACK);
return doNotTrack === '1';
} catch {
console.error('Error getting DoNotTrack');
return false;
}
2024-10-31 03:55:40 +01:00
};
2024-08-22 17:50:12 +02:00
2024-10-31 22:48:36 +01:00
export const setTotalBalanceViewEnabledStorage = async (value: boolean): Promise<void> => {
2024-10-31 04:31:53 +01:00
try {
await DefaultPreference.setName(GROUP_IO_BLUEWALLET);
await DefaultPreference.set(TotalWalletsBalanceKey, value ? 'true' : 'false');
console.debug('setTotalBalanceViewEnabledStorage value:', value);
} catch (e) {
console.error('Error setting TotalBalanceViewEnabled:', e);
}
2024-08-22 17:50:12 +02:00
};
export const getIsTotalBalanceViewEnabled = async (): Promise<boolean> => {
try {
await DefaultPreference.setName(GROUP_IO_BLUEWALLET);
const isEnabledValue = (await DefaultPreference.get(TotalWalletsBalanceKey)) ?? 'true';
console.debug('getIsTotalBalanceViewEnabled', isEnabledValue);
return isEnabledValue === 'true';
} catch (e) {
2024-10-31 04:31:53 +01:00
console.error('Error getting TotalBalanceViewEnabled:', e);
return true;
2024-08-22 17:50:12 +02:00
}
};
2024-10-31 22:48:36 +01:00
export const setTotalBalancePreferredUnitStorageFunc = async (unit: BitcoinUnit): Promise<void> => {
2024-10-31 04:31:53 +01:00
try {
await DefaultPreference.setName(GROUP_IO_BLUEWALLET);
await DefaultPreference.set(TotalWalletsBalancePreferredUnit, unit);
} catch (e) {
console.error('Error setting TotalBalancePreferredUnit:', e);
}
2024-08-22 17:50:12 +02:00
};
export const getTotalBalancePreferredUnit = async (): Promise<BitcoinUnit> => {
try {
await DefaultPreference.setName(GROUP_IO_BLUEWALLET);
2024-10-31 22:48:36 +01:00
const unit = (await DefaultPreference.get(TotalWalletsBalancePreferredUnit)) as BitcoinUnit | null;
2024-10-31 03:55:40 +01:00
return unit ?? BitcoinUnit.BTC;
2024-08-22 17:50:12 +02:00
} catch (e) {
2024-10-31 04:31:53 +01:00
console.error('Error getting TotalBalancePreferredUnit:', e);
return BitcoinUnit.BTC;
2024-08-22 17:50:12 +02:00
}
};
2024-04-18 03:05:48 +02:00
interface SettingsContextType {
preferredFiatCurrency: TFiatUnit;
setPreferredFiatCurrencyStorage: (currency: TFiatUnit) => Promise<void>;
2024-10-31 03:55:40 +01:00
language: string;
2024-04-18 03:05:48 +02:00
setLanguageStorage: (language: string) => Promise<void>;
isHandOffUseEnabled: boolean;
setIsHandOffUseEnabledAsyncStorage: (value: boolean) => Promise<void>;
isPrivacyBlurEnabled: boolean;
2024-11-09 03:38:05 +01:00
setIsPrivacyBlurEnabled: (value: boolean) => void;
2024-04-18 03:05:48 +02:00
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>;
2024-08-22 17:50:12 +02:00
isTotalBalanceEnabled: boolean;
setIsTotalBalanceEnabledStorage: (value: boolean) => Promise<void>;
totalBalancePreferredUnit: BitcoinUnit;
setTotalBalancePreferredUnitStorage: (unit: BitcoinUnit) => Promise<void>;
isDrawerShouldHide: boolean;
setIsDrawerShouldHide: (value: boolean) => void;
2024-09-26 08:36:51 +02:00
selectedBlockExplorer: BlockExplorer;
setBlockExplorerStorage: (explorer: BlockExplorer) => Promise<boolean>;
isElectrumDisabled: boolean;
setIsElectrumDisabled: (value: boolean) => void;
2024-04-18 03:05:48 +02:00
}
const defaultSettingsContext: SettingsContextType = {
preferredFiatCurrency: FiatUnit.USD,
setPreferredFiatCurrencyStorage: async () => {},
language: 'en',
setLanguageStorage: async () => {},
isHandOffUseEnabled: false,
setIsHandOffUseEnabledAsyncStorage: async () => {},
isPrivacyBlurEnabled: true,
2024-11-09 03:38:05 +01:00
setIsPrivacyBlurEnabled: () => {},
2024-04-18 03:05:48 +02:00
isDoNotTrackEnabled: false,
setDoNotTrackStorage: async () => {},
isWidgetBalanceDisplayAllowed: true,
setIsWidgetBalanceDisplayAllowedStorage: async () => {},
isLegacyURv1Enabled: false,
2024-10-31 03:55:40 +01:00
setIsLegacyURv1EnabledStorage: async () => {},
2024-04-18 03:05:48 +02:00
isClipboardGetContentEnabled: true,
setIsClipboardGetContentEnabledStorage: async () => {},
isQuickActionsEnabled: true,
setIsQuickActionsEnabledStorage: async () => {},
2024-08-22 17:50:12 +02:00
isTotalBalanceEnabled: true,
setIsTotalBalanceEnabledStorage: async () => {},
totalBalancePreferredUnit: BitcoinUnit.BTC,
2024-10-31 22:48:36 +01:00
setTotalBalancePreferredUnitStorage: async () => {},
isDrawerShouldHide: false,
setIsDrawerShouldHide: () => {},
2024-09-26 08:36:51 +02:00
selectedBlockExplorer: BLOCK_EXPLORERS.default,
2024-10-31 22:48:36 +01:00
setBlockExplorerStorage: async () => false,
isElectrumDisabled: false,
setIsElectrumDisabled: () => {},
2024-04-18 03:05:48 +02:00
};
export const SettingsContext = createContext<SettingsContextType>(defaultSettingsContext);
2024-10-31 03:55:40 +01:00
export const SettingsProvider: React.FC<{ children: React.ReactNode }> = React.memo(({ children }) => {
2024-04-18 03:05:48 +02:00
const [preferredFiatCurrency, setPreferredFiatCurrency] = useState<TFiatUnit>(FiatUnit.USD);
2024-10-31 03:55:40 +01:00
const [language, setLanguage] = useState<string>('en');
const [isHandOffUseEnabled, setIsHandOffUseEnabledState] = useState<boolean>(false);
2024-04-18 03:05:48 +02:00
const [isPrivacyBlurEnabled, setIsPrivacyBlurEnabled] = useState<boolean>(true);
const [isDoNotTrackEnabled, setIsDoNotTrackEnabled] = useState<boolean>(false);
const [isWidgetBalanceDisplayAllowed, setIsWidgetBalanceDisplayAllowed] = useState<boolean>(true);
const [isLegacyURv1Enabled, setIsLegacyURv1Enabled] = useState<boolean>(false);
2024-10-31 03:55:40 +01:00
const [isClipboardGetContentEnabled, setIsClipboardGetContentEnabled] = useState<boolean>(true);
2024-04-18 03:05:48 +02:00
const [isQuickActionsEnabled, setIsQuickActionsEnabled] = useState<boolean>(true);
2024-08-22 17:50:12 +02:00
const [isTotalBalanceEnabled, setIsTotalBalanceEnabled] = useState<boolean>(true);
2024-10-31 03:55:40 +01:00
const [totalBalancePreferredUnit, setTotalBalancePreferredUnit] = useState<BitcoinUnit>(BitcoinUnit.BTC);
const [isDrawerShouldHide, setIsDrawerShouldHide] = useState<boolean>(false);
2024-09-26 08:36:51 +02:00
const [selectedBlockExplorer, setSelectedBlockExplorer] = useState<BlockExplorer>(BLOCK_EXPLORERS.default);
const [isElectrumDisabled, setIsElectrumDisabled] = useState<boolean>(true);
2024-04-18 03:05:48 +02:00
const { walletsInitialized } = useStorage();
useEffect(() => {
2024-10-31 03:55:40 +01:00
const loadSettings = async () => {
try {
await DefaultPreference.setName(GROUP_IO_BLUEWALLET);
2024-10-31 04:31:53 +01:00
} catch (e) {
console.error('Error setting preference name:', e);
}
2024-10-31 03:55:40 +01:00
2024-10-31 22:48:36 +01:00
const promises: Promise<void>[] = [
BlueElectrum.isDisabled().then(disabled => {
setIsElectrumDisabled(disabled);
}),
2024-10-31 22:48:36 +01:00
getIsHandOffUseEnabled().then(handOff => {
setIsHandOffUseEnabledState(handOff);
}),
2024-11-10 15:25:39 +01:00
AsyncStorage.getItem(STORAGE_KEY).then(lang => {
2024-10-31 22:48:36 +01:00
setLanguage(lang ?? 'en');
}),
isBalanceDisplayAllowed().then(balanceDisplayAllowed => {
setIsWidgetBalanceDisplayAllowed(balanceDisplayAllowed);
}),
isURv1Enabled().then(urv1Enabled => {
setIsLegacyURv1Enabled(urv1Enabled);
}),
2024-11-10 15:25:39 +01:00
isReadClipboardAllowed().then(clipboardEnabled => {
setIsClipboardGetContentEnabled(clipboardEnabled);
}),
2024-10-31 22:48:36 +01:00
getIsDeviceQuickActionsEnabled().then(quickActionsEnabled => {
setIsQuickActionsEnabled(quickActionsEnabled);
}),
getDoNotTrackStorage().then(doNotTrack => {
setIsDoNotTrackEnabled(doNotTrack);
}),
getIsTotalBalanceViewEnabled().then(totalBalanceEnabled => {
setIsTotalBalanceEnabled(totalBalanceEnabled);
}),
getTotalBalancePreferredUnit().then(preferredUnit => {
setTotalBalancePreferredUnit(preferredUnit);
}),
getBlockExplorerUrl().then(url => {
const predefinedExplorer = Object.values(BLOCK_EXPLORERS).find(explorer => normalizeUrl(explorer.url) === normalizeUrl(url));
setSelectedBlockExplorer(predefinedExplorer ?? ({ key: 'custom', name: 'Custom', url } as BlockExplorer));
}),
];
const results = await Promise.allSettled(promises);
results.forEach((result, index) => {
if (result.status === 'rejected') {
console.error(`Error loading setting ${index}:`, result.reason);
2024-09-26 08:36:51 +02:00
}
2024-10-31 22:48:36 +01:00
});
2024-10-31 03:55:40 +01:00
};
loadSettings();
2024-11-09 02:07:35 +01:00
}, []);
2024-04-18 03:05:48 +02:00
useEffect(() => {
if (walletsInitialized) {
2024-10-31 04:31:53 +01:00
initCurrencyDaemon()
2024-10-31 22:48:36 +01:00
.then(getPreferredCurrency)
2024-10-31 04:31:53 +01:00
.then(currency => {
2024-04-20 00:49:13 +02:00
console.debug('SettingsContext currency:', currency);
2024-10-31 03:55:40 +01:00
setPreferredFiatCurrency(currency as TFiatUnit);
2024-10-31 04:31:53 +01:00
})
.catch(e => {
console.error('Error initializing currency daemon or getting preferred currency:', e);
2024-04-18 03:05:48 +02:00
});
}
}, [walletsInitialized]);
useEffect(() => {
if (walletsInitialized) {
isElectrumDisabled ? BlueElectrum.forceDisconnect() : BlueElectrum.connectMain();
}
}, [isElectrumDisabled, walletsInitialized]);
2024-10-31 22:48:36 +01:00
const setPreferredFiatCurrencyStorage = useCallback(async (currency: TFiatUnit): Promise<void> => {
2024-10-31 04:31:53 +01:00
try {
await DefaultPreference.setName(GROUP_IO_BLUEWALLET);
2024-11-01 03:25:47 +01:00
await DefaultPreference.set(PREFERRED_CURRENCY_STORAGE_KEY, currency.endPointKey);
setPreferredFiatCurrency(currency);
2024-10-31 04:31:53 +01:00
} catch (e) {
console.error('Error setting preferredFiatCurrency:', e);
}
2024-04-18 19:48:22 +02:00
}, []);
2024-04-18 03:05:48 +02:00
2024-10-31 22:48:36 +01:00
const setLanguageStorage = useCallback(async (newLanguage: string): Promise<void> => {
2024-10-31 04:31:53 +01:00
try {
await saveLanguage(newLanguage);
setLanguage(newLanguage);
} catch (e) {
console.error('Error setting language:', e);
}
2024-04-18 19:48:22 +02:00
}, []);
2024-04-18 03:05:48 +02:00
2024-10-31 22:48:36 +01:00
const setDoNotTrackStorage = useCallback(async (value: boolean): Promise<void> => {
2024-10-31 04:31:53 +01:00
try {
await DefaultPreference.setName(GROUP_IO_BLUEWALLET);
if (value) {
await DefaultPreference.set(BlueApp.DO_NOT_TRACK, '1');
} else {
await DefaultPreference.clear(BlueApp.DO_NOT_TRACK);
}
setIsDoNotTrackEnabled(value);
} catch (e) {
console.error('Error setting DoNotTrack:', e);
}
}, []);
2024-10-31 22:48:36 +01:00
const setIsHandOffUseEnabledAsyncStorage = useCallback(async (value: boolean): Promise<void> => {
2024-10-31 04:31:53 +01:00
try {
console.debug('setIsHandOffUseEnabledAsyncStorage', value);
2024-10-31 20:26:35 +01:00
await setIsHandOffUseEnabled(value);
setIsHandOffUseEnabledState(value);
2024-10-31 04:31:53 +01:00
} catch (e) {
console.error('Error setting isHandOffUseEnabled:', e);
}
}, []);
2024-04-18 19:48:22 +02:00
2024-10-31 22:48:36 +01:00
const setIsWidgetBalanceDisplayAllowedStorage = useCallback(async (value: boolean): Promise<void> => {
2024-10-31 04:31:53 +01:00
try {
await setBalanceDisplayAllowed(value);
setIsWidgetBalanceDisplayAllowed(value);
} catch (e) {
console.error('Error setting isWidgetBalanceDisplayAllowed:', e);
}
2024-04-18 19:48:22 +02:00
}, []);
2024-04-18 03:05:48 +02:00
2024-10-31 22:48:36 +01:00
const setIsLegacyURv1EnabledStorage = useCallback(async (value: boolean): Promise<void> => {
2024-10-31 04:31:53 +01:00
try {
if (value) {
await setUseURv1();
} else {
await clearUseURv1();
}
setIsLegacyURv1Enabled(value);
} catch (e) {
console.error('Error setting isLegacyURv1Enabled:', e);
2024-10-31 03:55:40 +01:00
}
2024-04-18 19:48:22 +02:00
}, []);
2024-04-18 03:05:48 +02:00
2024-10-31 22:48:36 +01:00
const setIsClipboardGetContentEnabledStorage = useCallback(async (value: boolean): Promise<void> => {
2024-10-31 04:31:53 +01:00
try {
2024-11-10 15:25:39 +01:00
await setReadClipboardAllowed(value);
2024-10-31 04:31:53 +01:00
setIsClipboardGetContentEnabled(value);
} catch (e) {
console.error('Error setting isClipboardGetContentEnabled:', e);
}
2024-04-18 19:48:22 +02:00
}, []);
2024-04-18 03:05:48 +02:00
2024-10-31 22:48:36 +01:00
const setIsQuickActionsEnabledStorage = useCallback(async (value: boolean): Promise<void> => {
2024-10-31 04:31:53 +01:00
try {
await setIsDeviceQuickActionsEnabled(value);
setIsQuickActionsEnabled(value);
} catch (e) {
console.error('Error setting isQuickActionsEnabled:', e);
}
2024-04-18 19:48:22 +02:00
}, []);
2024-10-31 22:48:36 +01:00
const setIsTotalBalanceEnabledStorage = useCallback(async (value: boolean): Promise<void> => {
2024-10-31 04:31:53 +01:00
try {
await setTotalBalanceViewEnabledStorage(value);
setIsTotalBalanceEnabled(value);
} catch (e) {
console.error('Error setting isTotalBalanceEnabled:', e);
}
2024-08-22 17:50:12 +02:00
}, []);
2024-10-31 22:48:36 +01:00
const setTotalBalancePreferredUnitStorage = useCallback(async (unit: BitcoinUnit): Promise<void> => {
2024-10-31 04:31:53 +01:00
try {
await setTotalBalancePreferredUnitStorageFunc(unit);
setTotalBalancePreferredUnit(unit);
} catch (e) {
console.error('Error setting totalBalancePreferredUnit:', e);
}
2024-08-22 17:50:12 +02:00
}, []);
2024-09-26 08:36:51 +02:00
const setBlockExplorerStorage = useCallback(async (explorer: BlockExplorer): Promise<boolean> => {
2024-10-31 04:31:53 +01:00
try {
const success = await saveBlockExplorer(explorer.url);
if (success) {
setSelectedBlockExplorer(explorer);
}
return success;
} catch (e) {
console.error('Error setting BlockExplorer:', e);
return false;
}
}, []);
2024-10-31 03:55:40 +01:00
2024-04-18 19:48:22 +02:00
const value = useMemo(
() => ({
preferredFiatCurrency,
setPreferredFiatCurrencyStorage,
language,
setLanguageStorage,
isHandOffUseEnabled,
setIsHandOffUseEnabledAsyncStorage,
isPrivacyBlurEnabled,
2024-11-09 03:38:05 +01:00
setIsPrivacyBlurEnabled,
2024-04-18 19:48:22 +02:00
isDoNotTrackEnabled,
setDoNotTrackStorage,
isWidgetBalanceDisplayAllowed,
setIsWidgetBalanceDisplayAllowedStorage,
isLegacyURv1Enabled,
setIsLegacyURv1EnabledStorage,
isClipboardGetContentEnabled,
setIsClipboardGetContentEnabledStorage,
isQuickActionsEnabled,
setIsQuickActionsEnabledStorage,
2024-08-22 17:50:12 +02:00
isTotalBalanceEnabled,
setIsTotalBalanceEnabledStorage,
totalBalancePreferredUnit,
setTotalBalancePreferredUnitStorage,
isDrawerShouldHide,
setIsDrawerShouldHide,
selectedBlockExplorer,
setBlockExplorerStorage,
isElectrumDisabled,
setIsElectrumDisabled,
2024-04-18 19:48:22 +02:00
}),
[
preferredFiatCurrency,
setPreferredFiatCurrencyStorage,
language,
setLanguageStorage,
isHandOffUseEnabled,
setIsHandOffUseEnabledAsyncStorage,
isPrivacyBlurEnabled,
2024-11-09 03:38:05 +01:00
setIsPrivacyBlurEnabled,
2024-04-18 19:48:22 +02:00
isDoNotTrackEnabled,
setDoNotTrackStorage,
isWidgetBalanceDisplayAllowed,
setIsWidgetBalanceDisplayAllowedStorage,
isLegacyURv1Enabled,
setIsLegacyURv1EnabledStorage,
isClipboardGetContentEnabled,
setIsClipboardGetContentEnabledStorage,
isQuickActionsEnabled,
setIsQuickActionsEnabledStorage,
2024-08-22 17:50:12 +02:00
isTotalBalanceEnabled,
setIsTotalBalanceEnabledStorage,
totalBalancePreferredUnit,
setTotalBalancePreferredUnitStorage,
isDrawerShouldHide,
setIsDrawerShouldHide,
selectedBlockExplorer,
setBlockExplorerStorage,
isElectrumDisabled,
2024-04-18 19:48:22 +02:00
],
);
2024-04-18 03:05:48 +02:00
return <SettingsContext.Provider value={value}>{children}</SettingsContext.Provider>;
2024-10-31 03:55:40 +01:00
});