Browse Source

ADD: Lightning invoice fetch every 5 seconsds

localNotifications
Marcos Rodriguez Vélez 6 years ago
parent
commit
6f58cc1944
  1. 1
      loc/en.js
  2. 1
      loc/es.js
  3. 1
      loc/pt_BR.js
  4. 1
      loc/pt_PT.js
  5. 1
      loc/ru.js
  6. 1
      loc/ua.js
  7. 2
      screen/lnd/lndCreateInvoice.js
  8. 85
      screen/lnd/lndViewInvoice.js
  9. 8
      screen/lnd/scanLndInvoice.js
  10. 1
      screen/transactions/details.js
  11. 69
      screen/wallets/list.js
  12. 71
      screen/wallets/transactions.js

1
loc/en.js

@ -200,5 +200,6 @@ module.exports = {
refill_lnd_balance: 'Refill Lightning wallet balance',
refill: 'Refill',
withdraw: 'Withdraw',
expired: 'Expired',
},
};

1
loc/es.js

@ -202,5 +202,6 @@ module.exports = {
refill_lnd_balance: 'Rellenar el balance de la billetera Lightning',
refill: 'Rellenar',
withdraw: 'Retirar',
expired: 'Expirado',
},
};

1
loc/pt_BR.js

@ -203,5 +203,6 @@ module.exports = {
refill_lnd_balance: 'Carregar o saldo da Lightning wallet',
refill: 'Carregar',
withdraw: 'Transferir',
expired: 'Expired',
},
};

1
loc/pt_PT.js

@ -202,5 +202,6 @@ module.exports = {
refill_lnd_balance: 'Carregar o saldo da Lightning wallet',
refill: 'Carregar',
withdraw: 'Transferir',
expired: 'Expired',
},
};

1
loc/ru.js

@ -203,5 +203,6 @@ module.exports = {
refill_lnd_balance: 'Пополнить баланс Lightning кошелька',
refill: 'Пополнить',
withdraw: 'Вывести',
expired: 'Expired',
},
};

1
loc/ua.js

@ -203,5 +203,6 @@ module.exports = {
refill_lnd_balance: 'Збільшити баланс Lightning гаманця',
refill: 'Поповнити',
withdraw: 'Вивести',
expired: 'Expired',
},
};

2
screen/lnd/lndCreateInvoice.js

@ -5,6 +5,7 @@ import PropTypes from 'prop-types';
import { BitcoinUnit } from '../../models/bitcoinUnits';
import ReactNativeHapticFeedback from 'react-native-haptic-feedback';
/** @type {AppStorage} */
let EV = require('../../events');
let loc = require('../../loc');
export default class LNDCreateInvoice extends Component {
@ -31,6 +32,7 @@ export default class LNDCreateInvoice extends Component {
if (invoiceRequest === null) {
ReactNativeHapticFeedback.trigger('notificationError', false);
} else {
EV(EV.enum.TRANSACTIONS_COUNT_CHANGED);
ReactNativeHapticFeedback.trigger('notificationSuccess', false);
this.props.navigation.navigate('LNDViewInvoice', {
invoice: invoiceRequest,

85
screen/lnd/lndViewInvoice.js

@ -1,11 +1,13 @@
import React, { Component } from 'react';
import { Animated, StyleSheet, View, TouchableOpacity, Clipboard, Share } from 'react-native';
// import { QRCode } from 'react-native-custom-qr-codes';
import { BlueLoading, SafeBlueArea, BlueButton, BlueNavigationStyle } from '../../BlueComponents';
import { BlueLoading, BlueText, SafeBlueArea, BlueButton, BlueNavigationStyle } from '../../BlueComponents';
import PropTypes from 'prop-types';
import ReactNativeHapticFeedback from 'react-native-haptic-feedback';
/** @type {AppStorage} */
let BlueApp = require('../../BlueApp');
let loc = require('../../loc');
const loc = require('../../loc');
const EV = require('../../events');
const QRFast = require('react-native-qrcode');
export default class LNDViewInvoice extends Component {
@ -14,15 +16,38 @@ export default class LNDViewInvoice extends Component {
title: loc.receive.header,
headerLeft: null,
});
constructor(props) {
super(props);
const invoice = props.navigation.getParam('invoice').toString();
const invoice = props.navigation.getParam('invoice');
const fromWallet = props.navigation.getParam('fromWallet');
this.state = {
isLoading: false,
invoice,
addressText: invoice,
fromWallet,
addressText: typeof invoice === 'object' ? invoice.payment_request : invoice,
};
this.fetchInvoiceInterval = undefined;
}
async componentDidMount() {
if (!this.state.invoice.isLoading) {
this.fetchInvoiceInterval = setInterval(async () => {
const userInvoices = JSON.stringify(await this.state.fromWallet.getUserInvoices());
const updatedUserInvoice = JSON.parse(userInvoices).filter(
invoice => invoice.payment_request === this.state.invoice.payment_request,
)[0];
this.setState({ invoice: updatedUserInvoice });
if (updatedUserInvoice.ispaid) {
ReactNativeHapticFeedback.trigger('notificationSuccess', false);
clearInterval(this.fetchInvoiceInterval);
EV(EV.enum.TRANSACTIONS_COUNT_CHANGED);
}
}, 5000);
}
}
componentWillUnmount() {
clearInterval(this.fetchInvoiceInterval)
}
copyToClipboard = () => {
@ -37,19 +62,49 @@ export default class LNDViewInvoice extends Component {
return <BlueLoading />;
}
const { invoice } = this.state;
if (typeof invoice === 'object') {
const currentDate = new Date();
const now = (currentDate.getTime() / 1000) | 0;
const invoiceExpiration = invoice.timestamp + invoice.expire_time;
if (invoice.ispaid) {
return (
<SafeBlueArea style={{ flex: 1 }}>
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<BlueText>This invoice has been paid for.</BlueText>
</View>
</SafeBlueArea>
);
}
if (invoiceExpiration < now && !invoice.ispaid) {
return (
<SafeBlueArea style={{ flex: 1 }}>
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<BlueText>This invoice was not paid for and has expired.</BlueText>
</View>
</SafeBlueArea>
);
} else if (invoiceExpiration > now && invoice.ispaid) {
if (invoice.ispaid) {
return (
<SafeBlueArea style={{ flex: 1 }}>
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<BlueText>'This invoice has been paid for.'</BlueText>
</View>
</SafeBlueArea>
);
}
}
}
// Invoice has not expired, nor has it been paid for.
return (
<SafeBlueArea style={{ flex: 1 }}>
<View style={{ flex: 1, justifyContent: 'space-between', alignItems: 'center' }}>
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center', paddingHorizontal: 16 }}>
{/* <QRCode
content={this.state.invoice}
size={(is.ipad() && 300) || 300}
color={BlueApp.settings.foregroundColor}
backgroundColor={BlueApp.settings.brandingColor}
logo={require('../../img/qr-code.png')}
/> */}
<QRFast
value={this.state.invoice}
value={typeof this.state.invoice === 'object' ? invoice.payment_request : invoice}
size={300}
fgColor={BlueApp.settings.brandingColor}
bgColor={BlueApp.settings.foregroundColor}
@ -69,7 +124,7 @@ export default class LNDViewInvoice extends Component {
}}
onPress={async () => {
Share.share({
message: this.state.invoice,
message: invoice,
});
}}
title={loc.receive.details.share}

8
screen/lnd/scanLndInvoice.js

@ -73,7 +73,7 @@ export default class ScanLndInvoice extends React.Component {
if (!this.state.fromWallet) {
alert('Error: cant find source wallet (this should never happen)');
return this.props.navigation.dismiss();
return this.props.navigation.goBack();
}
data = data.replace('LIGHTNING:', '').replace('lightning:', '');
@ -132,7 +132,7 @@ export default class ScanLndInvoice extends React.Component {
end = +new Date();
} catch (Err) {
console.log(Err.message);
this.props.navigation.dismiss();
this.props.navigation.goBack();
return alert('Error');
}
@ -140,7 +140,7 @@ export default class ScanLndInvoice extends React.Component {
EV(EV.enum.REMOTE_TRANSACTIONS_COUNT_CHANGED); // someone should fetch txs
alert('Success');
this.props.navigation.dismiss();
this.props.navigation.goBack();
}
processTextForInvoice = text => {
@ -271,7 +271,7 @@ export default class ScanLndInvoice extends React.Component {
ScanLndInvoice.propTypes = {
navigation: PropTypes.shape({
dismiss: PropTypes.function,
goBack: PropTypes.function,
navigate: PropTypes.function,
getParam: PropTypes.function,
state: PropTypes.shape({

1
screen/transactions/details.js

@ -16,6 +16,7 @@ import PropTypes from 'prop-types';
let BlueApp = require('../../BlueApp');
let loc = require('../../loc');
const dayjs = require('dayjs');
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}

69
screen/wallets/list.js

@ -47,6 +47,7 @@ export default class WalletsList extends Component {
this.state = {
isLoading: true,
wallets: BlueApp.getWallets().concat(false),
lastSnappedTo: 0,
};
EV(EV.enum.WALLETS_COUNT_CHANGED, this.refreshFunction.bind(this));
@ -137,6 +138,7 @@ export default class WalletsList extends Component {
onSnapToItem(index) {
console.log('onSnapToItem', index);
this.lastSnappedTo = index;
this.setState({ lastSnappedTo: index });
if (index < BlueApp.getWallets().length) {
// not the last
@ -226,13 +228,60 @@ export default class WalletsList extends Component {
}
};
rowTitle = item => {
if (item.type === 'user_invoice' || item.type === 'payment_request') {
const currentDate = new Date();
const now = (currentDate.getTime() / 1000) | 0;
const invoiceExpiration = item.timestamp + item.expire_time;
if (invoiceExpiration > now) {
return loc.formatBalanceWithoutSuffix(item.value && item.value, BitcoinUnit.BTC);
} else if (invoiceExpiration < now) {
if (item.ispaid) {
return loc.formatBalanceWithoutSuffix(item.value && item.value, BitcoinUnit.BTC);
} else {
return loc.lnd.expired;
}
}
} else {
return loc.formatBalanceWithoutSuffix(item.value && item.value, BitcoinUnit.BTC);
}
};
rowTitleStyle = item => {
let color = '#37c0a1';
if (item.type === 'user_invoice' || item.type === 'payment_request') {
const currentDate = new Date();
const now = (currentDate.getTime() / 1000) | 0;
const invoiceExpiration = item.timestamp + item.expire_time;
if (invoiceExpiration > now) {
color = '#37c0a1';
} else if (invoiceExpiration < now) {
if (item.ispaid) {
color = '#37c0a1';
} else {
color = '#FF0000';
}
}
} else if (item.value / 100000000 < 0) {
color = BlueApp.settings.foregroundColor;
}
return {
fontWeight: '600',
fontSize: 16,
color: color,
};
};
render() {
const { navigate } = this.props.navigation;
if (this.state.isLoading) {
return <BlueLoading />;
}
return (
<SafeBlueArea style={{ flex: 1, backgroundColor: '#FFFFFF' }}>
<NavigationEvents
@ -352,9 +401,10 @@ export default class WalletsList extends Component {
hash: rowData.item.hash,
});
} else if (rowData.item.type === 'user_invoice') {
this.props.navigation.navigate('LNDViewInvoice', {
invoice: rowData.item.payment_request,
});
// this.props.navigation.navigate('LNDViewInvoice', {
// invoice: rowData.item,
// fromWallet: this.state.wallets[this.state.lastSnappedTo],
// });
}
}}
badge={{
@ -363,15 +413,8 @@ export default class WalletsList extends Component {
containerStyle: { marginTop: 0 },
}}
hideChevron
rightTitle={loc.formatBalanceWithoutSuffix(rowData.item.value && rowData.item.value, BitcoinUnit.BTC)}
rightTitleStyle={{
fontWeight: '600',
fontSize: 16,
color:
rowData.item.value / 100000000 < 0 || rowData.item.type === 'paid_invoice'
? BlueApp.settings.foregroundColor
: '#37c0a1',
}}
rightTitle={this.rowTitle(rowData.item)}
rightTitleStyle={this.rowTitleStyle(rowData.item)}
/>
);
}}

71
screen/wallets/transactions.js

@ -21,7 +21,6 @@ import { BitcoinUnit } from '../../models/bitcoinUnits';
import { LightningCustodianWallet } from '../../class';
/** @type {AppStorage} */
let BlueApp = require('../../BlueApp');
let loc = require('../../loc');
let EV = require('../../events');
@ -296,6 +295,54 @@ export default class WalletTransactions extends Component {
await BlueApp.saveToDisk();
}
rowTitle = item => {
if (item.type === 'user_invoice' || item.type === 'payment_request') {
const currentDate = new Date();
const now = (currentDate.getTime() / 1000) | 0;
const invoiceExpiration = item.timestamp + item.expire_time;
if (invoiceExpiration > now) {
return loc.formatBalanceWithoutSuffix(item.value && item.value, BitcoinUnit.BTC);
} else if (invoiceExpiration < now) {
if (item.ispaid) {
return loc.formatBalanceWithoutSuffix(item.value && item.value, BitcoinUnit.BTC);
} else {
return loc.lnd.expired;
}
}
} else {
return loc.formatBalanceWithoutSuffix(item.value && item.value, BitcoinUnit.BTC);
}
};
rowTitleStyle = item => {
let color = '#37c0a1';
if (item.type === 'user_invoice' || item.type === 'payment_request') {
const currentDate = new Date();
const now = (currentDate.getTime() / 1000) | 0;
const invoiceExpiration = item.timestamp + item.expire_time;
if (invoiceExpiration > now) {
color = '#37c0a1';
} else if (invoiceExpiration < now) {
if (item.ispaid) {
color = '#37c0a1';
} else {
color = '#FF0000';
}
}
} else if (item.value / 100000000 < 0) {
color = BlueApp.settings.foregroundColor;
}
return {
fontWeight: '600',
fontSize: 16,
color: color,
};
};
render() {
const { navigate } = this.props.navigation;
return (
@ -382,7 +429,6 @@ export default class WalletTransactions extends Component {
}
refreshControl={<RefreshControl onRefresh={() => this.refreshTransactions()} refreshing={this.state.isTransactionsLoading} />}
data={this.state.dataSource}
extraData={this.state.dataSource}
keyExtractor={this._keyExtractor}
renderItem={rowData => {
return (
@ -453,9 +499,10 @@ export default class WalletTransactions extends Component {
navigate('TransactionDetails', {
hash: rowData.item.hash,
});
} else if (rowData.item.type === 'user_invoice') {
} else if (rowData.item.type === 'user_invoice' || rowData.item.type === 'payment_request') {
this.props.navigation.navigate('LNDViewInvoice', {
invoice: rowData.item.payment_request,
invoice: rowData.item,
fromWallet: this.state.wallet,
});
}
}}
@ -465,20 +512,8 @@ export default class WalletTransactions extends Component {
containerStyle: { marginTop: 0 },
}}
hideChevron
rightTitle={loc
.formatBalanceWithoutSuffix(
(rowData.item.value && rowData.item.value) || 0,
this.state.wallet.getPreferredBalanceUnit(),
)
.toString()}
rightTitleStyle={{
fontWeight: '600',
fontSize: 16,
color:
rowData.item.value / 100000000 < 0 || rowData.item.type === 'paid_invoice'
? BlueApp.settings.foregroundColor
: '#37c0a1',
}}
rightTitle={this.rowTitle(rowData.item)}
rightTitleStyle={this.rowTitleStyle(rowData.item)}
/>
);
}}

Loading…
Cancel
Save