FIX: Remove BTC trailing zeros. Save to disk onBlur for preferred balance unit.

This commit is contained in:
Marcos Rodriguez Vélez 2018-12-23 08:05:33 -05:00
parent 44ab347ee0
commit 91a2ecb1dc
6 changed files with 53 additions and 29 deletions

View file

@ -24,7 +24,6 @@ import { HDLegacyP2PKHWallet } from './class/hd-legacy-p2pkh-wallet';
import { HDLegacyBreadwalletWallet } from './class/hd-legacy-breadwallet-wallet';
import { HDSegwitP2SHWallet } from './class/hd-segwit-p2sh-wallet';
import { LightningCustodianWallet } from './class/lightning-custodian-wallet';
import { BitcoinUnit } from './models/bitcoinUnits';
let loc = require('./loc/');
/** @type {AppStorage} */
let BlueApp = require('./BlueApp');
@ -1049,7 +1048,7 @@ export class WalletsCarousel extends Component {
color: '#fff',
}}
>
{loc.formatBalance(Number(item.getBalance()), BitcoinUnit.BTC, item.getPreferredBalanceUnit())}
{loc.formatBalance(Number(item.getBalance()), item.getPreferredBalanceUnit())}
</Text>
<Text style={{ backgroundColor: 'transparent' }} />
<Text

View file

@ -34,7 +34,7 @@
</dict>
</array>
<key>CFBundleVersion</key>
<string>162</string>
<string>164</string>
<key>ITSAppUsesNonExemptEncryption</key>
<false/>
<key>LSRequiresIPhoneOS</key>

View file

@ -63,6 +63,18 @@ strings.transactionTimeToReadable = function(time) {
}
};
function removeTrailingZeros(value) {
value = value.toString();
if (value.indexOf('.') === -1) {
return value;
}
while ((value.slice(-1) === '0' || value.slice(-1) === '.') && value.indexOf('.') !== -1) {
value = value.substr(0, value.length - 1);
}
return value;
}
/**
*
* @param balance {Number} Float amount of bitcoins
@ -89,7 +101,8 @@ strings.formatBalanceWithoutSuffix = (balance, toUnit) => {
}
if (balance !== 0) {
if (toUnit === BitcoinUnit.BTC || toUnit === undefined) {
return new BigNumber(balance).dividedBy(100000000).toFixed(8);
const value = new BigNumber(balance).dividedBy(100000000).toFixed(8);
return removeTrailingZeros(value);
} else if (toUnit === BitcoinUnit.SATS) {
const value = new BigNumber(balance)
.multipliedBy(0.0001)

View file

@ -185,27 +185,34 @@ export default class SendDetails extends Component {
}
processBIP70Invoice(text) {
if (BitcoinBIP70TransactionDecode.matchesPaymentURL(text)) {
this.setState(
{
isLoading: true,
},
() => {
Keyboard.dismiss();
BitcoinBIP70TransactionDecode.decode(text).then(response => {
this.setState({
address: response.address,
amount: loc.formatBalanceWithoutSuffix(response.amount, BitcoinUnit.BTC),
memo: response.memo,
fee: response.fee,
bip70TransactionExpiration: response.expires,
isLoading: false,
});
});
},
);
try {
if (BitcoinBIP70TransactionDecode.matchesPaymentURL(text)) {
this.setState(
{
isLoading: true,
},
() => {
Keyboard.dismiss();
BitcoinBIP70TransactionDecode.decode(text)
.then(response => {
this.setState({
address: response.address,
amount: loc.formatBalanceWithoutSuffix(response.amount, BitcoinUnit.BTC),
memo: response.memo,
fee: response.fee,
bip70TransactionExpiration: response.expires,
isLoading: false,
});
})
.catch(error => {
alert(error.errorMessage);
this.setState({ address: text.replace(' ', ''), isLoading: false, bip70TransactionExpiration: null, amount: 0 });
});
},
);
}
return true;
} else {
} catch (error) {
this.setState({ address: text.replace(' ', ''), isLoading: false, bip70TransactionExpiration: null, amount: 0 });
return false;
}
@ -480,6 +487,8 @@ export default class SendDetails extends Component {
onChangeText={text => {
if (!this.processBIP70Invoice(text)) {
this.setState({ address: text.replace(' ', ''), isLoading: false, bip70TransactionExpiration: null });
} else {
this.setState({ address: text.replace(' ', ''), isLoading: false, bip70TransactionExpiration: null });
}
}}
placeholder={loc.send.details.address}

View file

@ -17,6 +17,7 @@ import { Icon } from 'react-native-elements';
import { NavigationEvents } from 'react-navigation';
import ReactNativeHapticFeedback from 'react-native-haptic-feedback';
import PropTypes from 'prop-types';
import { BitcoinUnit } from '../../models/bitcoinUnits';
const BigNumber = require('bignumber.js');
let EV = require('../../events');
let A = require('../../analytics');
@ -345,10 +346,7 @@ export default class WalletsList extends Component {
containerStyle: { marginTop: 0 },
}}
hideChevron
rightTitle={new BigNumber((rowData.item.value && rowData.item.value) || 0)
.dividedBy(100000000)
.toFixed(8)
.toString()}
rightTitle={loc.formatBalanceWithoutSuffix(rowData.item.value && rowData.item.value, BitcoinUnit.BTC)}
rightTitleStyle={{
fontWeight: '600',
fontSize: 16,

View file

@ -189,7 +189,7 @@ export default class WalletTransactions extends Component {
walletPreviousPreferredUnit = BitcoinUnit.BTC;
}
this.setState({ wallet: wallet, walletPreviousPreferredUnit: walletPreviousPreferredUnit }, () => BlueApp.saveToDisk());
this.setState({ wallet: wallet, walletPreviousPreferredUnit: walletPreviousPreferredUnit });
}
renderWalletHeader = () => {
@ -288,6 +288,10 @@ export default class WalletTransactions extends Component {
);
};
async onWillBlur() {
await BlueApp.saveToDisk();
}
render() {
const { navigate } = this.props.navigation;
return (
@ -296,6 +300,7 @@ export default class WalletTransactions extends Component {
onWillFocus={() => {
this.refreshFunction();
}}
onWillBlur={() => this.onWillBlur()}
/>
{this.renderWalletHeader()}
<View style={{ flex: 1, backgroundColor: '#FFFFFF' }}>