Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lookup to wallets now uses generators to populate the list faster and… #2

Merged
merged 1 commit into from
Jan 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 9 additions & 5 deletions lib/pages/portfolio.page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ class _PortfolioPageState extends State<PortfolioPage> {
_stake = await _ss.getStake();
_wallets = await _wp.getWallets();
setState((){});
_coins = await _wp.getWalletValues();
setState((){});
await for (Object coin in _wp.getWalletValues()) {
_coins.add(coin);
setState((){});
}
_coins = await _wp.coinsToPrice(coins: _coins, currency: 'sek');
_total = _coins.map((coin) => coin['value']).reduce((double a, double b) => a + b);
setState((){});
Expand Down Expand Up @@ -83,11 +85,13 @@ class _PortfolioPageState extends State<PortfolioPage> {
]),
new Expanded(
child: new ListView(children: [
_coins.length > 0 && _total == 0
? new Center(child: new Padding(padding: const EdgeInsets.symmetric(vertical: 10.0), child: new Text('Converting portfolio to FIAT...')))
: _coins.length == 0
? new Center(child: new CircularProgressIndicator(backgroundColor: Theme.of(ctx).primaryColor))
: new PortfolioChart(data: _coins),
_coins.length == 0
? new Center(child: new Text('Add a wallet to start tracking your assets'))
: new PortfolioChart(data: _coins),
_total == 0.0 && _coins.length > 0
? new Center(child: new CircularProgressIndicator(backgroundColor: Theme.of(ctx).primaryColor))
: new PortfolioList(coins: _coins)
])
),
Expand Down
58 changes: 20 additions & 38 deletions lib/services/wallet.service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,51 +37,33 @@ class WalletProvider {
return wallets;
}

Future<List<Object>> getWalletValues() async {
Stream<Object> getWalletValues() async* {
List<Map<String, String>> wallets = await this.getWallets();
Map<String, double> tokens = {};
List<Object> list = [];
await Future.forEach(wallets, (wallet) async {
switch (wallet['symbol']) {
case 'ETH':
Map<String, double> values = await API.getETHWalletValue(wallet['address']);
values.forEach((String symbol, double value) {
if(tokens.containsKey(symbol)) {
tokens[symbol] += value;
} else {
tokens[symbol] = value;
}
});
break;
for (int i = 0; i < wallets.length; i++) {
String symbol = wallets[i]['symbol'];
String address = wallets[i]['address'];
switch (symbol) {
case 'DASH':
case 'LTC' :
case 'BCH' :
case 'BTC' :
double value = await API.getGenericWalletValue(wallet['symbol'], wallet['address']);
if(tokens.containsKey(wallet['symbol'])) {
tokens[wallet['symbol']] += value;
} else {
tokens[wallet['symbol']] = value;
}
case 'LTC':
case 'BCH':
case 'BTC':
double value = await API.getGenericWalletValue(symbol, address);
yield {'symbol': symbol, 'amount': value, 'value': 0.0};
break;
case 'ADA':
double value = await API.getADAWalletValue(wallet['address']);
if(tokens.containsKey(wallet['symbol'])) {
tokens[wallet['symbol']] += value;
} else {
tokens[wallet['symbol']] = value;
double value = await API.getADAWalletValue(address);
yield {'symbol': symbol, 'amount': value, 'value': 0.0};
break;
case 'ETH':
Map<String, double> values = await API.getETHWalletValue(address);
for(int j = 0; j < values.keys.length; j++) {
String token = values.keys.toList()[j];
double amount = values[token];
yield {'symbol': token, 'amount': amount, 'value': 0.0};
}
break;
}
});
tokens.forEach((String symbol, double amount) {
list.add({
'symbol': symbol,
'amount': amount,
'value': 0.0,
});
});
return list;
}
}

Future<List<Map>> coinsToPrice({List<Object> coins: const [], String currency: 'USD'}) async {
Expand Down
2 changes: 1 addition & 1 deletion lib/styles.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Color getSymbolColor(String symbol) {
'LTC': Colors.red[200],
};
if (!colors.containsKey(symbol)) {
return Colors.white;
return Colors.black;
} else {
return colors[symbol];
}
Expand Down