BlueWallet/screen/settings/defaultView.js

80 lines
2.7 KiB
JavaScript
Raw Normal View History

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