mirror of
https://github.com/BlueWallet/BlueWallet.git
synced 2025-02-22 23:08:07 +01:00
REF: DefaultView to TSX and hook
This commit is contained in:
parent
52ff33ad2b
commit
b14fcb70f0
6 changed files with 163 additions and 133 deletions
|
@ -11,7 +11,7 @@ import Licensing from './screen/settings/Licensing';
|
|||
import NetworkSettings from './screen/settings/NetworkSettings';
|
||||
import Settings from './screen/settings/Settings';
|
||||
import About from './screen/settings/about';
|
||||
import DefaultView from './screen/settings/defaultView';
|
||||
import DefaultView from './screen/settings/DefaultView';
|
||||
import ElectrumSettings from './screen/settings/electrumSettings';
|
||||
import EncryptStorage from './screen/settings/encryptStorage';
|
||||
import Language from './screen/settings/language';
|
||||
|
|
|
@ -1,48 +0,0 @@
|
|||
import AsyncStorage from '@react-native-async-storage/async-storage';
|
||||
import { AbstractWallet } from './wallets/abstract-wallet';
|
||||
const BlueApp = require('../BlueApp');
|
||||
|
||||
export default class OnAppLaunch {
|
||||
static STORAGE_KEY = 'ONAPP_LAUNCH_SELECTED_DEFAULT_WALLET_KEY';
|
||||
|
||||
static async isViewAllWalletsEnabled(): Promise<boolean> {
|
||||
try {
|
||||
const selectedDefaultWallet = await AsyncStorage.getItem(OnAppLaunch.STORAGE_KEY);
|
||||
return selectedDefaultWallet === '' || selectedDefaultWallet === null;
|
||||
} catch (_e) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
static async setViewAllWalletsEnabled(value: boolean) {
|
||||
if (!value) {
|
||||
const selectedDefaultWallet = await OnAppLaunch.getSelectedDefaultWallet();
|
||||
if (!selectedDefaultWallet) {
|
||||
const firstWallet = BlueApp.getWallets()[0];
|
||||
await OnAppLaunch.setSelectedDefaultWallet(firstWallet.getID());
|
||||
}
|
||||
} else {
|
||||
await AsyncStorage.setItem(OnAppLaunch.STORAGE_KEY, '');
|
||||
}
|
||||
}
|
||||
|
||||
static async getSelectedDefaultWallet(): Promise<AbstractWallet | boolean> {
|
||||
let selectedWallet: AbstractWallet | false = false;
|
||||
try {
|
||||
const selectedWalletID = JSON.parse((await AsyncStorage.getItem(OnAppLaunch.STORAGE_KEY)) || 'null');
|
||||
if (selectedWalletID) {
|
||||
selectedWallet = BlueApp.getWallets().find((wallet: AbstractWallet) => wallet.getID() === selectedWalletID);
|
||||
if (!selectedWallet) {
|
||||
await AsyncStorage.setItem(OnAppLaunch.STORAGE_KEY, '');
|
||||
}
|
||||
}
|
||||
} catch (_e) {
|
||||
return false;
|
||||
}
|
||||
return selectedWallet;
|
||||
}
|
||||
|
||||
static async setSelectedDefaultWallet(value: string) {
|
||||
await AsyncStorage.setItem(OnAppLaunch.STORAGE_KEY, JSON.stringify(value));
|
||||
}
|
||||
}
|
|
@ -5,16 +5,17 @@ import { formatBalance } from '../loc';
|
|||
import AsyncStorage from '@react-native-async-storage/async-storage';
|
||||
import { BlueStorageContext } from '../blue_modules/storage-context';
|
||||
import DeeplinkSchemaMatch from './deeplink-schema-match';
|
||||
import OnAppLaunch from './on-app-launch';
|
||||
import * as NavigationService from '../NavigationService';
|
||||
import { CommonActions } from '@react-navigation/native';
|
||||
import { AbstractWallet } from './wallets/abstract-wallet';
|
||||
import useOnAppLaunch from '../hooks/useOnAppLaunch';
|
||||
|
||||
const DeviceQuickActionsStorageKey = 'DeviceQuickActionsEnabled';
|
||||
|
||||
function DeviceQuickActions(): JSX.Element | null {
|
||||
const { wallets, walletsInitialized, isStorageEncrypted, preferredFiatCurrency, addWallet, saveToDisk, setSharedCosigner } =
|
||||
useContext(BlueStorageContext);
|
||||
const { isViewAllWalletsEnabled, getSelectedDefaultWallet } = useOnAppLaunch();
|
||||
|
||||
useEffect(() => {
|
||||
if (walletsInitialized) {
|
||||
|
@ -91,9 +92,8 @@ function DeviceQuickActions(): JSX.Element | null {
|
|||
handleOpenURL({ url });
|
||||
}
|
||||
} else {
|
||||
const isViewAllWalletsEnabled = await OnAppLaunch.isViewAllWalletsEnabled();
|
||||
if (!isViewAllWalletsEnabled) {
|
||||
const selectedDefaultWallet: AbstractWallet = (await OnAppLaunch.getSelectedDefaultWallet()) as AbstractWallet;
|
||||
if (!(await isViewAllWalletsEnabled())) {
|
||||
const selectedDefaultWallet: AbstractWallet = (await getSelectedDefaultWallet()) as AbstractWallet;
|
||||
if (selectedDefaultWallet) {
|
||||
const wallet = wallets.find((w: AbstractWallet) => w.getID() === selectedDefaultWallet.getID());
|
||||
if (wallet) {
|
||||
|
|
64
hooks/useOnAppLaunch.ts
Normal file
64
hooks/useOnAppLaunch.ts
Normal file
|
@ -0,0 +1,64 @@
|
|||
import { useCallback } from 'react';
|
||||
import AsyncStorage from '@react-native-async-storage/async-storage';
|
||||
import { AbstractWallet } from '../class';
|
||||
const BlueApp = require('../BlueApp');
|
||||
|
||||
const useOnAppLaunch = () => {
|
||||
const STORAGE_KEY = 'ONAPP_LAUNCH_SELECTED_DEFAULT_WALLET_KEY';
|
||||
|
||||
const getSelectedDefaultWallet = useCallback(async (): Promise<AbstractWallet | false> => {
|
||||
let selectedWallet: AbstractWallet | false = false;
|
||||
try {
|
||||
const selectedWalletID = JSON.parse((await AsyncStorage.getItem(STORAGE_KEY)) || 'null');
|
||||
if (selectedWalletID) {
|
||||
selectedWallet = BlueApp.getWallets().find((wallet: AbstractWallet) => wallet.getID() === selectedWalletID);
|
||||
if (!selectedWallet) {
|
||||
await AsyncStorage.setItem(STORAGE_KEY, '');
|
||||
}
|
||||
}
|
||||
} catch (_e) {
|
||||
return false;
|
||||
}
|
||||
return selectedWallet;
|
||||
}, [STORAGE_KEY]); // No external dependencies
|
||||
|
||||
const setSelectedDefaultWallet = useCallback(
|
||||
async (value: string): Promise<void> => {
|
||||
await AsyncStorage.setItem(STORAGE_KEY, JSON.stringify(value));
|
||||
},
|
||||
[STORAGE_KEY],
|
||||
); // No external dependencies
|
||||
|
||||
const isViewAllWalletsEnabled = useCallback(async (): Promise<boolean> => {
|
||||
try {
|
||||
const selectedDefaultWallet = await AsyncStorage.getItem(STORAGE_KEY);
|
||||
return selectedDefaultWallet === '' || selectedDefaultWallet === null;
|
||||
} catch (_e) {
|
||||
return true;
|
||||
}
|
||||
}, [STORAGE_KEY]); // No external dependencies
|
||||
|
||||
const setViewAllWalletsEnabled = useCallback(
|
||||
async (value: boolean): Promise<void> => {
|
||||
if (!value) {
|
||||
const selectedDefaultWallet = await getSelectedDefaultWallet();
|
||||
if (!selectedDefaultWallet) {
|
||||
const firstWallet = BlueApp.getWallets()[0];
|
||||
await setSelectedDefaultWallet(firstWallet.getID());
|
||||
}
|
||||
} else {
|
||||
await AsyncStorage.setItem(STORAGE_KEY, '');
|
||||
}
|
||||
},
|
||||
[STORAGE_KEY, getSelectedDefaultWallet, setSelectedDefaultWallet],
|
||||
); // Include dependencies here
|
||||
|
||||
return {
|
||||
isViewAllWalletsEnabled,
|
||||
setViewAllWalletsEnabled,
|
||||
getSelectedDefaultWallet,
|
||||
setSelectedDefaultWallet,
|
||||
};
|
||||
};
|
||||
|
||||
export default useOnAppLaunch;
|
94
screen/settings/DefaultView.tsx
Normal file
94
screen/settings/DefaultView.tsx
Normal file
|
@ -0,0 +1,94 @@
|
|||
import React, { useContext, useEffect, useState } from 'react';
|
||||
import { View, TouchableWithoutFeedback, ScrollView } from 'react-native';
|
||||
import { useNavigation } from '@react-navigation/native';
|
||||
import navigationStyle from '../../components/navigationStyle';
|
||||
import { BlueCard, BlueText } from '../../BlueComponents';
|
||||
import loc from '../../loc';
|
||||
import { BlueStorageContext } from '../../blue_modules/storage-context';
|
||||
import ListItem from '../../components/ListItem';
|
||||
import useOnAppLaunch from '../../hooks/useOnAppLaunch';
|
||||
import { NativeStackNavigationProp } from '@react-navigation/native-stack';
|
||||
|
||||
type RootStackParamList = {
|
||||
SelectWallet: { onWalletSelect: (wallet: { getID: () => string; getLabel: () => string }) => void };
|
||||
// Add other screens here as needed
|
||||
};
|
||||
|
||||
type DefaultViewNavigationProp = NativeStackNavigationProp<RootStackParamList, 'SelectWallet'>;
|
||||
|
||||
const DefaultView: React.FC = () => {
|
||||
const [defaultWalletLabel, setDefaultWalletLabel] = useState<string>('');
|
||||
const [isViewAllWalletsSwitchEnabled, setIsViewAllWalletsSwitchEnabled] = useState<boolean>(true);
|
||||
const { navigate, pop } = useNavigation<DefaultViewNavigationProp>();
|
||||
const { wallets } = useContext(BlueStorageContext);
|
||||
const { isViewAllWalletsEnabled, getSelectedDefaultWallet, setSelectedDefaultWallet, setViewAllWalletsEnabled } = useOnAppLaunch();
|
||||
|
||||
useEffect(() => {
|
||||
(async () => {
|
||||
const newViewAllWalletsEnabled: boolean = await isViewAllWalletsEnabled();
|
||||
let newDefaultWalletLabel: string = '';
|
||||
const wallet = await getSelectedDefaultWallet();
|
||||
|
||||
if (wallet) {
|
||||
newDefaultWalletLabel = wallet.getLabel();
|
||||
}
|
||||
setDefaultWalletLabel(newDefaultWalletLabel);
|
||||
setIsViewAllWalletsSwitchEnabled(newViewAllWalletsEnabled);
|
||||
})();
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, []);
|
||||
|
||||
const onViewAllWalletsSwitchValueChanged = async (value: boolean) => {
|
||||
await setViewAllWalletsEnabled(value);
|
||||
if (value) {
|
||||
setIsViewAllWalletsSwitchEnabled(true);
|
||||
setDefaultWalletLabel('');
|
||||
} else {
|
||||
const selectedWallet = await getSelectedDefaultWallet();
|
||||
if (selectedWallet) {
|
||||
setDefaultWalletLabel(selectedWallet.getLabel());
|
||||
setIsViewAllWalletsSwitchEnabled(false);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const selectWallet = () => {
|
||||
navigate('SelectWallet', { onWalletSelect: onWalletSelectValueChanged });
|
||||
};
|
||||
|
||||
const onWalletSelectValueChanged = async (wallet: { getID: () => string; getLabel: () => string; }) => {
|
||||
await setViewAllWalletsEnabled(false);
|
||||
await setSelectedDefaultWallet(wallet.getID());
|
||||
setDefaultWalletLabel(wallet.getLabel());
|
||||
setIsViewAllWalletsSwitchEnabled(false);
|
||||
pop();
|
||||
};
|
||||
|
||||
return (
|
||||
<ScrollView automaticallyAdjustContentInsets={false} contentInsetAdjustmentBehavior="automatic">
|
||||
<View>
|
||||
<ListItem
|
||||
title={loc.settings.default_wallets}
|
||||
Component={TouchableWithoutFeedback}
|
||||
switch={{
|
||||
onValueChange: onViewAllWalletsSwitchValueChanged,
|
||||
value: isViewAllWalletsSwitchEnabled,
|
||||
disabled: wallets.length <= 0,
|
||||
}}
|
||||
/>
|
||||
<BlueCard>
|
||||
<BlueText>{loc.settings.default_desc}</BlueText>
|
||||
</BlueCard>
|
||||
{!isViewAllWalletsSwitchEnabled && (
|
||||
<ListItem title={loc.settings.default_info} onPress={selectWallet} rightTitle={defaultWalletLabel} chevron />
|
||||
)}
|
||||
</View>
|
||||
</ScrollView>
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
// @ts-ignore: Property 'navigationOptions' does not exist on type 'DefaultView'
|
||||
DefaultView.navigationOptions = navigationStyle({}, (opts: any) => ({ ...opts, title: loc.settings.default_title }));
|
||||
|
||||
export default DefaultView;
|
|
@ -1,80 +0,0 @@
|
|||
import React, { useContext, useEffect, useState } from 'react';
|
||||
import { View, TouchableWithoutFeedback, ScrollView } from 'react-native';
|
||||
import { useNavigation } from '@react-navigation/native';
|
||||
|
||||
import navigationStyle from '../../components/navigationStyle';
|
||||
import { BlueCard, BlueText } from '../../BlueComponents';
|
||||
import OnAppLaunch from '../../class/on-app-launch';
|
||||
import loc from '../../loc';
|
||||
import { BlueStorageContext } from '../../blue_modules/storage-context';
|
||||
import ListItem from '../../components/ListItem';
|
||||
|
||||
const DefaultView = () => {
|
||||
const [defaultWalletLabel, setDefaultWalletLabel] = useState('');
|
||||
const [viewAllWalletsEnabled, setViewAllWalletsEnabled] = useState(true);
|
||||
const { navigate, pop } = useNavigation();
|
||||
const { wallets } = useContext(BlueStorageContext);
|
||||
|
||||
useEffect(() => {
|
||||
(async () => {
|
||||
const newViewAllWalletsEnabled = await OnAppLaunch.isViewAllWalletsEnabled();
|
||||
let newDefaultWalletLabel = '';
|
||||
const wallet = await OnAppLaunch.getSelectedDefaultWallet();
|
||||
if (wallet) {
|
||||
newDefaultWalletLabel = wallet.getLabel();
|
||||
}
|
||||
setDefaultWalletLabel(newDefaultWalletLabel);
|
||||
setViewAllWalletsEnabled(newViewAllWalletsEnabled);
|
||||
})();
|
||||
});
|
||||
|
||||
const onViewAllWalletsSwitchValueChanged = async value => {
|
||||
await OnAppLaunch.setViewAllWalletsEnabled(value);
|
||||
if (value) {
|
||||
setViewAllWalletsEnabled(true);
|
||||
setDefaultWalletLabel('');
|
||||
} else {
|
||||
const selectedWallet = await OnAppLaunch.getSelectedDefaultWallet();
|
||||
setDefaultWalletLabel(selectedWallet.getLabel());
|
||||
setViewAllWalletsEnabled(false);
|
||||
}
|
||||
};
|
||||
|
||||
const selectWallet = () => {
|
||||
navigate('SelectWallet', { onWalletSelect: onWalletSelectValueChanged });
|
||||
};
|
||||
|
||||
const onWalletSelectValueChanged = async wallet => {
|
||||
await OnAppLaunch.setViewAllWalletsEnabled(false);
|
||||
await OnAppLaunch.setSelectedDefaultWallet(wallet.getID());
|
||||
setDefaultWalletLabel(wallet.getLabel());
|
||||
setViewAllWalletsEnabled(false);
|
||||
pop();
|
||||
};
|
||||
|
||||
return (
|
||||
<ScrollView automaticallyAdjustContentInsets contentInsetAdjustmentBehavior="automatic">
|
||||
<View>
|
||||
<ListItem
|
||||
title={loc.settings.default_wallets}
|
||||
Component={TouchableWithoutFeedback}
|
||||
switch={{
|
||||
onValueChange: onViewAllWalletsSwitchValueChanged,
|
||||
value: viewAllWalletsEnabled,
|
||||
disabled: wallets.length <= 0,
|
||||
}}
|
||||
/>
|
||||
<BlueCard>
|
||||
<BlueText>{loc.settings.default_desc}</BlueText>
|
||||
</BlueCard>
|
||||
{!viewAllWalletsEnabled && (
|
||||
<ListItem title={loc.settings.default_info} onPress={selectWallet} rightTitle={defaultWalletLabel} chevron />
|
||||
)}
|
||||
</View>
|
||||
</ScrollView>
|
||||
);
|
||||
};
|
||||
|
||||
DefaultView.navigationOptions = navigationStyle({}, opts => ({ ...opts, title: loc.settings.default_title }));
|
||||
|
||||
export default DefaultView;
|
Loading…
Add table
Reference in a new issue