BlueWallet/screen/settings/defaultView.js

81 lines
2.8 KiB
JavaScript
Raw Normal View History

import React, { useContext, useEffect, useState } from 'react';
2024-01-13 10:56:29 -04:00
import { View, TouchableWithoutFeedback, ScrollView } from 'react-native';
2020-07-20 16:38:46 +03:00
import { useNavigation } from '@react-navigation/native';
2020-12-25 19:09:53 +03:00
import navigationStyle from '../../components/navigationStyle';
import { BlueCard, BlueText } from '../../BlueComponents';
import OnAppLaunch from '../../class/on-app-launch';
2020-07-20 16:38:46 +03:00
import loc from '../../loc';
import { BlueStorageContext } from '../../blue_modules/storage-context';
2023-12-16 17:44:35 -04:00
import ListItem from '../../components/ListItem';
2019-10-02 23:06:47 -04:00
const DefaultView = () => {
const [defaultWalletLabel, setDefaultWalletLabel] = useState('');
const [viewAllWalletsEnabled, setViewAllWalletsEnabled] = useState(true);
const { navigate, pop } = useNavigation();
const { wallets } = useContext(BlueStorageContext);
2019-10-02 23:06:47 -04:00
useEffect(() => {
(async () => {
const newViewAllWalletsEnabled = await OnAppLaunch.isViewAllWalletsEnabled();
let newDefaultWalletLabel = '';
const wallet = await OnAppLaunch.getSelectedDefaultWallet();
if (wallet) {
newDefaultWalletLabel = wallet.getLabel();
}
setDefaultWalletLabel(newDefaultWalletLabel);
setViewAllWalletsEnabled(newViewAllWalletsEnabled);
})();
});
2019-10-02 23:06:47 -04:00
const onViewAllWalletsSwitchValueChanged = async value => {
2019-10-02 23:06:47 -04:00
await OnAppLaunch.setViewAllWalletsEnabled(value);
if (value) {
setViewAllWalletsEnabled(true);
setDefaultWalletLabel('');
2019-10-02 23:06:47 -04:00
} else {
const selectedWallet = await OnAppLaunch.getSelectedDefaultWallet();
setDefaultWalletLabel(selectedWallet.getLabel());
setViewAllWalletsEnabled(false);
2019-10-02 23:06:47 -04:00
}
};
const selectWallet = () => {
navigate('SelectWallet', { onWalletSelect: onWalletSelectValueChanged });
};
const onWalletSelectValueChanged = async wallet => {
2019-10-02 23:06:47 -04:00
await OnAppLaunch.setViewAllWalletsEnabled(false);
await OnAppLaunch.setSelectedDefaultWallet(wallet.getID());
setDefaultWalletLabel(wallet.getLabel());
setViewAllWalletsEnabled(false);
pop();
2019-10-02 23:06:47 -04:00
};
return (
2024-01-13 10:56:29 -04:00
<ScrollView automaticallyAdjustContentInsets contentInsetAdjustmentBehavior="automatic">
<View>
2023-12-16 17:44:35 -04:00
<ListItem
2020-07-20 16:38:46 +03:00
title={loc.settings.default_wallets}
2020-05-03 14:17:49 -04:00
Component={TouchableWithoutFeedback}
switch={{
onValueChange: onViewAllWalletsSwitchValueChanged,
value: viewAllWalletsEnabled,
disabled: wallets.length <= 0,
2020-05-03 14:17:49 -04:00
}}
/>
<BlueCard>
2020-11-22 03:04:04 -05:00
<BlueText>{loc.settings.default_desc}</BlueText>
</BlueCard>
{!viewAllWalletsEnabled && (
2023-12-16 17:44:35 -04:00
<ListItem title={loc.settings.default_info} onPress={selectWallet} rightTitle={defaultWalletLabel} chevron />
)}
</View>
2024-01-13 10:56:29 -04:00
</ScrollView>
);
2019-10-02 23:06:47 -04:00
};
2021-02-15 11:03:54 +03:00
DefaultView.navigationOptions = navigationStyle({}, opts => ({ ...opts, title: loc.settings.default_title }));
export default DefaultView;