Merge pull request #677 from BlueWallet/defualtview

ADD: Default into wallet
This commit is contained in:
Overtorment 2019-10-06 13:30:50 +03:00 committed by GitHub
commit a6f71e09b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 160 additions and 5 deletions

View File

@ -10,6 +10,8 @@ import EncryptStorage from './screen/settings/encryptStorage';
import PlausibleDeniability from './screen/plausibledeniability';
import LightningSettings from './screen/settings/lightningSettings';
import ElectrumSettings from './screen/settings/electrumSettings';
import DefaultView from './screen/settings/defaultView';
import WalletsList from './screen/wallets/list';
import WalletTransactions from './screen/wallets/transactions';
import AddWallet from './screen/wallets/add';
@ -98,6 +100,9 @@ const WalletsStackNavigator = createStackNavigator(
headerTintColor: '#0c2550',
},
},
SelectWallet: {
screen: SelectWallet,
},
Currency: {
screen: Currency,
},
@ -112,6 +117,10 @@ const WalletsStackNavigator = createStackNavigator(
Selftest: {
screen: Selftest,
},
DefaultView: {
screen: DefaultView,
path: 'DefaultView',
},
Language: {
screen: Language,
path: 'Language',

View File

@ -1,5 +1,5 @@
import { BitcoinUnit, Chain } from '../models/bitcoinUnits';
const createHash = require('create-hash');
export class AbstractWallet {
static type = 'abstract';
static typeReadable = 'abstract';
@ -31,6 +31,13 @@ export class AbstractWallet {
this.hideBalance = false;
}
getID() {
return createHash('sha256')
.update(this.getSecret())
.digest()
.toString('hex');
}
getTransactions() {
return this.transactions;
}

45
class/onAppLaunch.js Normal file
View File

@ -0,0 +1,45 @@
import AsyncStorage from '@react-native-community/async-storage';
const BlueApp = require('../BlueApp');
export default class OnAppLaunch {
static STORAGE_KEY = 'ONAPP_LAUNCH_SELECTED_DEFAULT_WALLET_KEY';
static async isViewAllWalletsEnabled() {
try {
const selectedDefaultWallet = await AsyncStorage.getItem(OnAppLaunch.STORAGE_KEY);
return selectedDefaultWallet === '' || selectedDefaultWallet === null;
} catch (_e) {
return true;
}
}
static async setViewAllWalletsEnabled(value) {
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() {
let selectedWallet = false;
try {
const selectedWalletID = JSON.parse(await AsyncStorage.getItem(OnAppLaunch.STORAGE_KEY));
selectedWallet = BlueApp.getWallets().find(wallet => wallet.getID() === selectedWalletID);
if (!selectedWallet) {
await AsyncStorage.setItem(OnAppLaunch.STORAGE_KEY, '');
}
} catch (_e) {
return false;
}
return selectedWallet;
}
static async setSelectedDefaultWallet(value) {
await AsyncStorage.setItem(OnAppLaunch.STORAGE_KEY, JSON.stringify(value));
}
}

View File

@ -0,0 +1,80 @@
import React, { Component } from 'react';
import { TouchableOpacity, View } from 'react-native';
import { SafeBlueArea, BlueNavigationStyle, BlueListItem } from '../../BlueComponents';
import PropTypes from 'prop-types';
import OnAppLaunch from '../../class/onAppLaunch';
const BlueApp = require('../../BlueApp');
export default class DefaultView extends Component {
static navigationOptions = () => ({
...BlueNavigationStyle(),
title: 'On Launch',
});
constructor(props) {
super(props);
this.state = { defaultWalletLabel: '', viewAllWalletsEnabled: true };
}
async componentDidMount() {
const viewAllWalletsEnabled = await OnAppLaunch.isViewAllWalletsEnabled();
let defaultWalletLabel = '';
const wallet = await OnAppLaunch.getSelectedDefaultWallet();
if (wallet) {
defaultWalletLabel = wallet.getLabel();
}
this.setState({ viewAllWalletsEnabled, defaultWalletLabel });
}
selectWallet = () => {
this.props.navigation.navigate('SelectWallet', { onWalletSelect: this.onWalletSelectValueChanged });
};
onViewAllWalletsSwitchValueChanged = async value => {
await OnAppLaunch.setViewAllWalletsEnabled(value);
if (value) {
return this.setState({ viewAllWalletsEnabled: true, defaultWalletLabel: '' });
} else {
const selectedWallet = await OnAppLaunch.getSelectedDefaultWallet();
return this.setState({ viewAllWalletsEnabled: false, defaultWalletLabel: selectedWallet.getLabel() });
}
};
onWalletSelectValueChanged = async wallet => {
await OnAppLaunch.setViewAllWalletsEnabled(false);
await OnAppLaunch.setSelectedDefaultWallet(wallet.getID());
this.setState({ defaultWalletLabel: wallet.getLabel(), viewAllWalletsEnabled: false }, () => this.props.navigation.pop());
};
render() {
return (
<SafeBlueArea forceInset={{ horizontal: 'always' }} style={{ flex: 1 }}>
<View>
<BlueListItem
title="View All Wallets"
hideChevron
switchButton
swithchEnabled={BlueApp.getWallets().length > 0}
switched={this.state.viewAllWalletsEnabled}
onSwitch={this.onViewAllWalletsSwitchValueChanged}
/>
{!this.state.viewAllWalletsEnabled && (
<BlueListItem
title="Default into"
component={TouchableOpacity}
onPress={this.selectWallet}
rightTitle={this.state.defaultWalletLabel}
/>
)}
</View>
</SafeBlueArea>
);
}
}
DefaultView.propTypes = {
navigation: PropTypes.shape({
navigate: PropTypes.func,
pop: PropTypes.func,
}),
};

View File

@ -12,6 +12,7 @@ import {
import AsyncStorage from '@react-native-community/async-storage';
import PropTypes from 'prop-types';
import { AppStorage } from '../../class';
const BlueApp = require('../../BlueApp');
let loc = require('../../loc');
export default class Settings extends Component {
@ -53,6 +54,9 @@ export default class Settings extends Component {
<SafeBlueArea forceInset={{ horizontal: 'always' }} style={{ flex: 1 }}>
<BlueHeaderDefaultSub leftText={loc.settings.header} rightComponent={null} />
<ScrollView maxHeight={450}>
{BlueApp.getWallets().length > 1 && (
<BlueListItem component={TouchableOpacity} onPress={() => this.props.navigation.navigate('DefaultView')} title="On Launch" />
)}
<TouchableOpacity onPress={() => this.props.navigation.navigate('EncryptStorage')}>
<BlueListItem title={loc.settings.encrypt_storage} />
</TouchableOpacity>

View File

@ -6,6 +6,7 @@ import { NavigationEvents } from 'react-navigation';
import ReactNativeHapticFeedback from 'react-native-haptic-feedback';
import PropTypes from 'prop-types';
import WalletGradient from '../../class/walletGradient';
import OnAppLaunch from '../../class/onAppLaunch';
let EV = require('../../events');
let A = require('../../analytics');
/** @type {AppStorage} */
@ -47,10 +48,16 @@ export default class WalletsList extends Component {
componentDidMount() {
this.redrawScreen();
// the idea is that upon wallet launch we will refresh
// all balances and all transactions here:
InteractionManager.runAfterInteractions(async () => {
const isViewAllWalletsEnabled = await OnAppLaunch.isViewAllWalletsEnabled();
if (!isViewAllWalletsEnabled) {
const selectedDefaultWallet = await OnAppLaunch.getSelectedDefaultWallet();
const walletIndex = this.state.wallets.findIndex(wallet => wallet.getID() === selectedDefaultWallet.getID());
this.handleClick(walletIndex);
}
let noErr = true;
try {
await BlueElectrum.waitTillConnected();

View File

@ -23,6 +23,7 @@ export default class SelectWallet extends Component {
isLoading: true,
data: [],
};
this.chainType = props.navigation.getParam('chainType');
}
dismissComponent = () => {
@ -30,7 +31,9 @@ export default class SelectWallet extends Component {
};
componentDidMount() {
const wallets = BlueApp.getWallets().filter(item => item.chain === this.props.navigation.getParam('chainType') && item.allowSend());
const wallets = this.chainType
? BlueApp.getWallets().filter(item => item.chain === this.chainType && item.allowSend())
: BlueApp.getWallets();
this.setState({
data: wallets,
isLoading: false,

View File

@ -39,7 +39,7 @@ let BlueElectrum = require('../../BlueElectrum');
export default class WalletTransactions extends Component {
static navigationOptions = ({ navigation }) => {
return {
return ({
headerRight: (
<TouchableOpacity
disabled={navigation.getParam('isLoading') === true}
@ -60,7 +60,7 @@ export default class WalletTransactions extends Component {
shadowRadius: 0,
},
headerTintColor: '#FFFFFF',
};
});
};
walletBalanceText = null;