BlueWallet/screen/settings/DefaultView.tsx

127 lines
4.7 KiB
TypeScript
Raw Normal View History

2024-03-23 00:11:31 +01:00
import { useNavigation } from '@react-navigation/native';
2024-05-20 11:54:13 +02:00
import { NativeStackNavigationProp } from '@react-navigation/native-stack';
import React, { useEffect, useReducer } from 'react';
import { ScrollView, TouchableWithoutFeedback, View } from 'react-native';
import { BlueCard, BlueText } from '../../BlueComponents';
import { TWallet } from '../../class/wallets/types';
2024-03-23 00:11:31 +01:00
import ListItem from '../../components/ListItem';
import useOnAppLaunch from '../../hooks/useOnAppLaunch';
2024-05-20 11:54:13 +02:00
import loc from '../../loc';
2024-05-31 19:18:01 +02:00
import { useStorage } from '../../hooks/context/useStorage';
2024-03-23 00:11:31 +01:00
type RootStackParamList = {
2024-03-23 17:33:00 +01:00
SelectWallet: { onWalletSelect: (wallet: TWallet) => void; onChainRequireSend: boolean };
2024-03-23 00:11:31 +01:00
};
type DefaultViewNavigationProp = NativeStackNavigationProp<RootStackParamList, 'SelectWallet'>;
2024-03-31 19:06:15 +02:00
const enum ActionType {
SetDefaultWalletLabel = 'SET_DEFAULT_WALLET_LABEL',
SetViewAllWalletsSwitch = 'SET_VIEW_ALL_WALLETS_SWITCH',
}
type State = {
defaultWalletLabel: string;
isViewAllWalletsSwitchEnabled: boolean;
};
type Action = { type: ActionType.SetDefaultWalletLabel; payload: string } | { type: ActionType.SetViewAllWalletsSwitch; payload: boolean };
const reducer = (state: State, action: Action): State => {
switch (action.type) {
case ActionType.SetDefaultWalletLabel:
return { ...state, defaultWalletLabel: action.payload };
case ActionType.SetViewAllWalletsSwitch:
return { ...state, isViewAllWalletsSwitchEnabled: action.payload };
default:
return state;
}
};
2024-03-23 00:11:31 +01:00
const DefaultView: React.FC = () => {
2024-03-31 19:06:15 +02:00
const [state, dispatch] = useReducer(reducer, {
defaultWalletLabel: '',
isViewAllWalletsSwitchEnabled: true,
});
2024-03-23 00:11:31 +01:00
const { navigate, pop } = useNavigation<DefaultViewNavigationProp>();
const { wallets } = useStorage();
2024-03-23 00:11:31 +01:00
const { isViewAllWalletsEnabled, getSelectedDefaultWallet, setSelectedDefaultWallet, setViewAllWalletsEnabled } = useOnAppLaunch();
useEffect(() => {
(async () => {
const newViewAllWalletsEnabled: boolean = await isViewAllWalletsEnabled();
let newDefaultWalletLabel: string = '';
2024-03-23 17:33:00 +01:00
const walletID = await getSelectedDefaultWallet();
2024-03-23 00:11:31 +01:00
2024-03-23 17:33:00 +01:00
if (walletID) {
const w = wallets.find(wallet => wallet.getID() === walletID);
if (w) newDefaultWalletLabel = w.getLabel();
2024-03-23 00:11:31 +01:00
}
2024-03-31 19:06:15 +02:00
dispatch({ type: ActionType.SetDefaultWalletLabel, payload: newDefaultWalletLabel });
dispatch({ type: ActionType.SetViewAllWalletsSwitch, payload: newViewAllWalletsEnabled });
2024-03-23 00:11:31 +01:00
})();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const onViewAllWalletsSwitchValueChanged = async (value: boolean) => {
await setViewAllWalletsEnabled(value);
2024-03-31 19:06:15 +02:00
dispatch({ type: ActionType.SetViewAllWalletsSwitch, payload: value });
if (!value && wallets.length === 1) {
// Automatically select the wallet if there is only one
const selectedWallet = wallets[0];
await setSelectedDefaultWallet(selectedWallet.getID());
dispatch({ type: ActionType.SetDefaultWalletLabel, payload: selectedWallet.getLabel() });
} else if (!value) {
2024-03-23 17:33:00 +01:00
const selectedWalletID = await getSelectedDefaultWallet();
const selectedWallet = wallets.find(wallet => wallet.getID() === selectedWalletID);
2024-03-23 00:11:31 +01:00
if (selectedWallet) {
2024-03-31 19:06:15 +02:00
dispatch({ type: ActionType.SetDefaultWalletLabel, payload: selectedWallet.getLabel() });
2024-03-23 00:11:31 +01:00
}
}
};
const selectWallet = () => {
2024-03-23 17:33:00 +01:00
navigate('SelectWallet', { onWalletSelect: onWalletSelectValueChanged, onChainRequireSend: false });
2024-03-23 00:11:31 +01:00
};
2024-03-23 17:11:08 +01:00
const onWalletSelectValueChanged = async (wallet: TWallet) => {
2024-03-23 00:11:31 +01:00
await setViewAllWalletsEnabled(false);
await setSelectedDefaultWallet(wallet.getID());
2024-03-31 19:06:15 +02:00
dispatch({ type: ActionType.SetDefaultWalletLabel, payload: wallet.getLabel() });
dispatch({ type: ActionType.SetViewAllWalletsSwitch, payload: false });
2024-03-23 00:11:31 +01:00
pop();
};
return (
<ScrollView automaticallyAdjustContentInsets={false} contentInsetAdjustmentBehavior="automatic">
<View>
<ListItem
title={loc.settings.default_wallets}
Component={TouchableWithoutFeedback}
switch={{
onValueChange: onViewAllWalletsSwitchValueChanged,
2024-03-31 19:06:15 +02:00
value: state.isViewAllWalletsSwitchEnabled,
2024-03-23 00:11:31 +01:00
disabled: wallets.length <= 0,
}}
/>
<BlueCard>
<BlueText>{loc.settings.default_desc}</BlueText>
</BlueCard>
2024-03-31 19:06:15 +02:00
{!state.isViewAllWalletsSwitchEnabled && (
<ListItem
title={loc.settings.default_info}
onPress={selectWallet}
rightTitle={state.defaultWalletLabel}
chevron
disabled={wallets.length <= 1}
/>
2024-03-23 00:11:31 +01:00
)}
</View>
</ScrollView>
);
};
2024-03-23 17:11:08 +01:00
export default DefaultView;