BlueWallet/components/Context/SettingsProvider.tsx

385 lines
15 KiB
TypeScript
Raw Normal View History

2024-04-17 21:05:48 -04:00
import { useAsyncStorage } from '@react-native-async-storage/async-storage';
import React, { createContext, useCallback, useEffect, useMemo, useState } from 'react';
2024-05-20 10:54:13 +01:00
import DefaultPreference from 'react-native-default-preference';
import BlueClipboard from '../../blue_modules/clipboard';
import { getPreferredCurrency, GROUP_IO_BLUEWALLET, initCurrencyDaemon } from '../../blue_modules/currency';
2024-04-17 21:05:48 -04:00
import { clearUseURv1, isURv1Enabled, setUseURv1 } from '../../blue_modules/ur';
2024-05-20 10:54:13 +01:00
import { BlueApp } from '../../class';
import { saveLanguage, STORAGE_KEY } from '../../loc';
import { FiatUnit, TFiatUnit } from '../../models/fiatUnit';
2024-04-29 18:54:19 -04:00
import { getEnabled as getIsDeviceQuickActionsEnabled, setEnabled as setIsDeviceQuickActionsEnabled } from '..//DeviceQuickActions';
2024-05-20 10:54:13 +01:00
import { getIsHandOffUseEnabled, setIsHandOffUseEnabled } from '../HandOffComponent';
import { isBalanceDisplayAllowed, setBalanceDisplayAllowed } from '../WidgetCommunication';
import { useStorage } from '../../hooks/context/useStorage';
2024-08-22 11:50:12 -04:00
import { BitcoinUnit } from '../../models/bitcoinUnits';
import { TotalWalletsBalanceKey, TotalWalletsBalancePreferredUnit } from '../TotalWalletsBalance';
import { LayoutAnimation } from 'react-native';
2024-09-26 02:36:51 -04:00
import { BLOCK_EXPLORERS, getBlockExplorerUrl, saveBlockExplorer, BlockExplorer, normalizeUrl } from '../../models/blockExplorer';
2024-08-22 11:50:12 -04:00
// DefaultPreference and AsyncStorage get/set
// TotalWalletsBalance
export const setTotalBalanceViewEnabled = async (value: boolean) => {
await DefaultPreference.setName(GROUP_IO_BLUEWALLET);
await DefaultPreference.set(TotalWalletsBalanceKey, value ? 'true' : 'false');
console.debug('setTotalBalanceViewEnabled value:', value);
LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut);
};
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) {
console.debug('getIsTotalBalanceViewEnabled error', e);
await setTotalBalanceViewEnabled(true);
}
await setTotalBalanceViewEnabled(true);
return true;
};
export const setTotalBalancePreferredUnit = async (unit: BitcoinUnit) => {
await DefaultPreference.setName(GROUP_IO_BLUEWALLET);
await DefaultPreference.set(TotalWalletsBalancePreferredUnit, unit);
LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut); // Add animation when changing unit
};
//
export const getTotalBalancePreferredUnit = async (): Promise<BitcoinUnit> => {
try {
await DefaultPreference.setName(GROUP_IO_BLUEWALLET);
const unit = ((await DefaultPreference.get(TotalWalletsBalancePreferredUnit)) as BitcoinUnit) ?? BitcoinUnit.BTC;
return unit;
} catch (e) {
console.debug('getPreferredUnit error', e);
}
return BitcoinUnit.BTC;
};
2024-04-17 21:05:48 -04:00
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 13:48:22 -04:00
setIsPrivacyBlurEnabledState: (value: boolean) => void;
2024-04-17 21:05:48 -04: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 11:50:12 -04:00
isTotalBalanceEnabled: boolean;
setIsTotalBalanceEnabledStorage: (value: boolean) => Promise<void>;
totalBalancePreferredUnit: BitcoinUnit;
setTotalBalancePreferredUnitStorage: (unit: BitcoinUnit) => Promise<void>;
isDrawerShouldHide: boolean;
setIsDrawerShouldHide: (value: boolean) => void;
2024-09-26 02:36:51 -04:00
selectedBlockExplorer: BlockExplorer;
setBlockExplorerStorage: (explorer: BlockExplorer) => Promise<boolean>;
2024-04-17 21:05:48 -04:00
}
const defaultSettingsContext: SettingsContextType = {
preferredFiatCurrency: FiatUnit.USD,
setPreferredFiatCurrencyStorage: async () => {},
language: 'en',
setLanguageStorage: async () => {},
isHandOffUseEnabled: false,
setIsHandOffUseEnabledAsyncStorage: async () => {},
isPrivacyBlurEnabled: true,
2024-04-18 13:48:22 -04:00
setIsPrivacyBlurEnabledState: () => {},
2024-04-17 21:05:48 -04:00
isDoNotTrackEnabled: false,
setDoNotTrackStorage: async () => {},
isWidgetBalanceDisplayAllowed: true,
setIsWidgetBalanceDisplayAllowedStorage: async () => {},
setIsLegacyURv1EnabledStorage: async () => {},
isLegacyURv1Enabled: false,
isClipboardGetContentEnabled: true,
setIsClipboardGetContentEnabledStorage: async () => {},
isQuickActionsEnabled: true,
setIsQuickActionsEnabledStorage: async () => {},
2024-08-22 11:50:12 -04:00
isTotalBalanceEnabled: true,
setIsTotalBalanceEnabledStorage: async () => {},
totalBalancePreferredUnit: BitcoinUnit.BTC,
setTotalBalancePreferredUnitStorage: async (unit: BitcoinUnit) => {},
isDrawerShouldHide: false,
setIsDrawerShouldHide: () => {},
2024-09-26 02:36:51 -04:00
selectedBlockExplorer: BLOCK_EXPLORERS.default,
setBlockExplorerStorage: async (explorer: BlockExplorer) => false,
2024-04-17 21:05:48 -04:00
};
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-17 21:05:48 -04:00
// PrivacyBlur
const [isPrivacyBlurEnabled, setIsPrivacyBlurEnabled] = useState<boolean>(true);
// 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);
2024-08-22 11:50:12 -04:00
// Total Balance
const [isTotalBalanceEnabled, setIsTotalBalanceEnabled] = useState<boolean>(true);
const [totalBalancePreferredUnit, setTotalBalancePreferredUnitState] = useState<BitcoinUnit>(BitcoinUnit.BTC);
2024-04-17 21:05:48 -04:00
// Toggle Drawer (for screens like Manage Wallets or ScanQRCode)
const [isDrawerShouldHide, setIsDrawerShouldHide] = useState<boolean>(false);
2024-09-26 02:36:51 -04:00
const [selectedBlockExplorer, setSelectedBlockExplorer] = useState<BlockExplorer>(BLOCK_EXPLORERS.default);
2024-04-17 21:05:48 -04:00
const languageStorage = useAsyncStorage(STORAGE_KEY);
const { walletsInitialized } = useStorage();
useEffect(() => {
2024-05-11 18:08:49 -04:00
getIsHandOffUseEnabled()
.then(handOff => {
console.debug('SettingsContext handOff:', handOff);
setHandOffUseEnabled(handOff);
})
.catch(error => console.error('Error fetching hand-off usage:', error));
languageStorage
.getItem()
.then(lang => {
lang = lang ?? 'en';
console.debug('SettingsContext lang:', lang);
setLanguage(lang);
})
.catch(error => console.error('Error fetching language setting:', error));
isBalanceDisplayAllowed()
.then(isBalanceDisplayAllowedStorage => {
console.debug('SettingsContext isBalanceDisplayAllowed:', isBalanceDisplayAllowedStorage);
setIsWidgetBalanceDisplayAllowed(isBalanceDisplayAllowedStorage);
})
.catch(error => console.error('Error fetching balance display allowance:', error));
isURv1Enabled()
.then(isURv1EnabledStorage => {
console.debug('SettingsContext isURv1Enabled:', isURv1EnabledStorage);
setIsLegacyURv1EnabledStorage(isURv1EnabledStorage);
})
.catch(error => console.error('Error fetching UR v1 enabled status:', error));
BlueClipboard()
.isReadClipboardAllowed()
.then(isClipboardGetContentEnabledStorage => {
console.debug('SettingsContext isClipboardGetContentEnabled:', isClipboardGetContentEnabledStorage);
setIsClipboardGetContentEnabledStorage(isClipboardGetContentEnabledStorage);
})
.catch(error => console.error('Error fetching clipboard content allowance:', error));
getIsDeviceQuickActionsEnabled()
.then(isQuickActionsEnabledStorage => {
console.debug('SettingsContext isQuickActionsEnabled:', isQuickActionsEnabledStorage);
setIsQuickActionsEnabledStorage(isQuickActionsEnabledStorage);
})
.catch(error => console.error('Error fetching device quick actions enabled status:', error));
getDoNotTrackStorage()
.then(value => {
console.debug('SettingsContext doNotTrack:', value);
setDoNotTrackStorage(value ?? false);
})
.catch(error => console.error('Error fetching do not track settings:', error));
2024-08-22 11:50:12 -04:00
getIsTotalBalanceViewEnabled()
.then(value => {
console.debug('SettingsContext totalBalance:', value);
setIsTotalBalanceEnabledStorage(value);
})
.catch(error => console.error('Error fetching total balance settings:', error));
getTotalBalancePreferredUnit()
.then(unit => {
console.debug('SettingsContext totalBalancePreferredUnit:', unit);
setTotalBalancePreferredUnitState(unit);
2024-08-22 11:50:12 -04:00
})
.catch(error => console.error('Error fetching total balance preferred unit:', error));
2024-09-26 02:36:51 -04:00
getBlockExplorerUrl()
.then(url => {
console.debug('SettingsContext blockExplorer:', url);
2024-09-26 02:36:51 -04:00
const predefinedExplorer = Object.values(BLOCK_EXPLORERS).find(explorer => normalizeUrl(explorer.url) === normalizeUrl(url));
if (predefinedExplorer) {
setSelectedBlockExplorer(predefinedExplorer);
} else {
setSelectedBlockExplorer({ key: 'custom', name: 'Custom', url });
}
})
.catch(error => console.error('Error fetching block explorer settings:', error));
2024-05-11 18:08:49 -04:00
// eslint-disable-next-line react-hooks/exhaustive-deps
2024-04-17 21:05:48 -04:00
}, []);
useEffect(() => {
if (walletsInitialized) {
initCurrencyDaemon().finally(() => {
getPreferredCurrency().then(currency => {
2024-04-19 18:49:13 -04:00
console.debug('SettingsContext currency:', currency);
2024-04-17 21:05:48 -04:00
setPreferredFiatCurrency(FiatUnit[currency.endPointKey]);
});
});
}
}, [walletsInitialized]);
2024-04-18 13:48:22 -04:00
const setPreferredFiatCurrencyStorage = useCallback(async (currency: TFiatUnit) => {
2024-04-17 21:05:48 -04:00
await setPreferredFiatCurrency(currency);
setPreferredFiatCurrency(currency);
2024-04-18 13:48:22 -04:00
}, []);
2024-04-17 21:05:48 -04:00
2024-04-18 13:48:22 -04:00
const setLanguageStorage = useCallback(async (newLanguage: string) => {
2024-04-17 21:05:48 -04:00
await saveLanguage(newLanguage);
setLanguage(newLanguage);
2024-04-18 13:48:22 -04:00
}, []);
2024-04-17 21:05:48 -04:00
const setDoNotTrackStorage = useCallback(async (value: boolean) => {
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);
}, []);
const getDoNotTrackStorage = useCallback(async () => {
await DefaultPreference.setName(GROUP_IO_BLUEWALLET);
const doNotTrack = await DefaultPreference.get(BlueApp.DO_NOT_TRACK);
return doNotTrack === '1';
}, []);
2024-04-18 13:48:22 -04:00
const setIsHandOffUseEnabledAsyncStorage = useCallback(async (value: boolean) => {
console.debug('setIsHandOffUseEnabledAsyncStorage', value);
await setIsHandOffUseEnabled(value);
setHandOffUseEnabled(value);
}, []);
2024-04-18 13:48:22 -04:00
const setIsWidgetBalanceDisplayAllowedStorage = useCallback(async (value: boolean) => {
2024-04-17 21:05:48 -04:00
await setBalanceDisplayAllowed(value);
setIsWidgetBalanceDisplayAllowed(value);
2024-04-18 13:48:22 -04:00
}, []);
2024-04-17 21:05:48 -04:00
2024-04-18 13:48:22 -04:00
const setIsLegacyURv1EnabledStorage = useCallback(async (value: boolean) => {
2024-04-17 21:05:48 -04:00
value ? await setUseURv1() : await clearUseURv1();
await setIsLegacyURv1Enabled(value);
2024-04-18 13:48:22 -04:00
}, []);
2024-04-17 21:05:48 -04:00
2024-04-18 13:48:22 -04:00
const setIsClipboardGetContentEnabledStorage = useCallback(async (value: boolean) => {
2024-04-17 21:05:48 -04:00
await BlueClipboard().setReadClipboardAllowed(value);
setIsClipboardGetContentEnabled(value);
2024-04-18 13:48:22 -04:00
}, []);
2024-04-17 21:05:48 -04:00
2024-04-18 13:48:22 -04:00
const setIsQuickActionsEnabledStorage = useCallback(async (value: boolean) => {
2024-04-29 18:54:19 -04:00
await setIsDeviceQuickActionsEnabled(value);
2024-04-17 21:05:48 -04:00
setIsQuickActionsEnabled(value);
2024-04-18 13:48:22 -04:00
}, []);
2024-04-18 13:50:49 -04:00
const setIsPrivacyBlurEnabledState = useCallback(
(value: boolean) => {
setIsPrivacyBlurEnabled(value);
2024-04-19 18:49:13 -04:00
console.debug(`Privacy blur: ${isPrivacyBlurEnabled}`);
2024-04-18 13:50:49 -04:00
},
[isPrivacyBlurEnabled],
);
2024-04-17 21:05:48 -04:00
2024-08-22 11:50:12 -04:00
const setIsTotalBalanceEnabledStorage = useCallback(async (value: boolean) => {
setTotalBalanceViewEnabled(value);
setIsTotalBalanceEnabled(value);
}, []);
const setTotalBalancePreferredUnitStorage = useCallback(async (unit: BitcoinUnit) => {
await setTotalBalancePreferredUnit(unit);
setTotalBalancePreferredUnitState(unit);
}, []);
2024-09-26 02:36:51 -04:00
const setBlockExplorerStorage = useCallback(async (explorer: BlockExplorer): Promise<boolean> => {
const success = await saveBlockExplorer(explorer.url);
if (success) {
2024-09-26 02:36:51 -04:00
setSelectedBlockExplorer(explorer);
}
return success;
}, []);
2024-04-18 13:48:22 -04:00
const value = useMemo(
() => ({
preferredFiatCurrency,
setPreferredFiatCurrencyStorage,
language,
setLanguageStorage,
isHandOffUseEnabled,
setIsHandOffUseEnabledAsyncStorage,
isPrivacyBlurEnabled,
setIsPrivacyBlurEnabledState,
isDoNotTrackEnabled,
setDoNotTrackStorage,
isWidgetBalanceDisplayAllowed,
setIsWidgetBalanceDisplayAllowedStorage,
isLegacyURv1Enabled,
setIsLegacyURv1EnabledStorage,
isClipboardGetContentEnabled,
setIsClipboardGetContentEnabledStorage,
isQuickActionsEnabled,
setIsQuickActionsEnabledStorage,
2024-08-22 11:50:12 -04:00
isTotalBalanceEnabled,
setIsTotalBalanceEnabledStorage,
totalBalancePreferredUnit,
setTotalBalancePreferredUnitStorage,
isDrawerShouldHide,
setIsDrawerShouldHide,
selectedBlockExplorer,
setBlockExplorerStorage,
2024-04-18 13:48:22 -04:00
}),
[
preferredFiatCurrency,
setPreferredFiatCurrencyStorage,
language,
setLanguageStorage,
isHandOffUseEnabled,
setIsHandOffUseEnabledAsyncStorage,
isPrivacyBlurEnabled,
setIsPrivacyBlurEnabledState,
isDoNotTrackEnabled,
setDoNotTrackStorage,
isWidgetBalanceDisplayAllowed,
setIsWidgetBalanceDisplayAllowedStorage,
isLegacyURv1Enabled,
setIsLegacyURv1EnabledStorage,
isClipboardGetContentEnabled,
setIsClipboardGetContentEnabledStorage,
isQuickActionsEnabled,
setIsQuickActionsEnabledStorage,
2024-08-22 11:50:12 -04:00
isTotalBalanceEnabled,
setIsTotalBalanceEnabledStorage,
totalBalancePreferredUnit,
setTotalBalancePreferredUnitStorage,
isDrawerShouldHide,
setIsDrawerShouldHide,
selectedBlockExplorer,
setBlockExplorerStorage,
2024-04-18 13:48:22 -04:00
],
);
2024-04-17 21:05:48 -04:00
return <SettingsContext.Provider value={value}>{children}</SettingsContext.Provider>;
2024-05-31 13:18:01 -04:00
};