FIX: If theres only one wallet, select it by default

This commit is contained in:
Marcos Rodriguez Velez 2024-04-20 22:34:47 -04:00
parent e95cc870a7
commit 054e72b1ef
No known key found for this signature in database
GPG key ID: 6030B2F48CCE86D7
2 changed files with 19 additions and 7 deletions

View file

@ -1,9 +1,9 @@
import React, { useContext, useEffect, useReducer } from 'react';
import React, { useEffect, useReducer } from 'react';
import { View, TouchableWithoutFeedback, ScrollView } from 'react-native';
import { useNavigation } from '@react-navigation/native';
import { BlueCard, BlueText } from '../../BlueComponents';
import loc from '../../loc';
import { BlueStorageContext } from '../../blue_modules/storage-context';
import { useStorage } from '../../blue_modules/storage-context';
import ListItem from '../../components/ListItem';
import useOnAppLaunch from '../../hooks/useOnAppLaunch';
import { NativeStackNavigationProp } from '@react-navigation/native-stack';
@ -45,7 +45,7 @@ const DefaultView: React.FC = () => {
});
const { navigate, pop } = useNavigation<DefaultViewNavigationProp>();
const { wallets } = useContext(BlueStorageContext);
const { wallets } = useStorage();
const { isViewAllWalletsEnabled, getSelectedDefaultWallet, setSelectedDefaultWallet, setViewAllWalletsEnabled } = useOnAppLaunch();
useEffect(() => {
@ -67,7 +67,13 @@ const DefaultView: React.FC = () => {
const onViewAllWalletsSwitchValueChanged = async (value: boolean) => {
await setViewAllWalletsEnabled(value);
dispatch({ type: ActionType.SetViewAllWalletsSwitch, payload: value });
if (!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) {
const selectedWalletID = await getSelectedDefaultWallet();
const selectedWallet = wallets.find(wallet => wallet.getID() === selectedWalletID);
if (selectedWallet) {
@ -104,7 +110,13 @@ const DefaultView: React.FC = () => {
<BlueText>{loc.settings.default_desc}</BlueText>
</BlueCard>
{!state.isViewAllWalletsSwitchEnabled && (
<ListItem title={loc.settings.default_info} onPress={selectWallet} rightTitle={state.defaultWalletLabel} chevron />
<ListItem
title={loc.settings.default_info}
onPress={selectWallet}
rightTitle={state.defaultWalletLabel}
chevron
disabled={wallets.length <= 1}
/>
)}
</View>
</ScrollView>

View file

@ -40,8 +40,8 @@ const GeneralSettings: React.FC = () => {
};
return (
<ScrollView style={[styles.root, stylesWithThemeHook.root]}>
{wallets.length > 1 && (
<ScrollView style={[styles.root, stylesWithThemeHook.root]} automaticallyAdjustContentInsets contentInsetAdjustmentBehavior="automatic">
{wallets.length > 0 && (
<>
{/* @ts-ignore: Fix later */}
<ListItem onPress={() => navigate('DefaultView')} title={loc.settings.default_title} chevron />