From 3096b8f0ed2a0a7fde7a98a6ed6cc7d3253840ff Mon Sep 17 00:00:00 2001 From: poka Date: Mon, 6 Jun 2022 19:21:22 +0200 Subject: [PATCH] wip: try to subscribe to each specifics balances --- lib/main.dart | 4 +- lib/providers/subscribe_balances.dart | 38 ++++++++++++++ lib/providers/wallet_options.dart | 74 ++++++++++++++++----------- lib/screens/wallet_view.dart | 18 ++++--- 4 files changed, 95 insertions(+), 39 deletions(-) create mode 100644 lib/providers/subscribe_balances.dart diff --git a/lib/main.dart b/lib/main.dart index 586c971..b151d03 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -24,6 +24,7 @@ import 'package:gecko/models/chest_data.dart'; import 'package:gecko/providers/chest_provider.dart'; import 'package:gecko/models/g1_wallets_list.dart'; import 'package:gecko/providers/generate_wallets.dart'; +import 'package:gecko/providers/subscribe_balances.dart'; import 'package:gecko/providers/substrate_sdk.dart'; import 'package:gecko/providers/wallets_profiles.dart'; import 'package:gecko/providers/home.dart'; @@ -134,7 +135,8 @@ class Gecko extends StatelessWidget { ChangeNotifierProvider(create: (_) => WalletOptionsProvider()), ChangeNotifierProvider(create: (_) => SearchProvider()), ChangeNotifierProvider(create: (_) => CesiumPlusProvider()), - ChangeNotifierProvider(create: (_) => SubstrateSdk()) + ChangeNotifierProvider(create: (_) => SubstrateSdk()), + ChangeNotifierProvider(create: (_) => SubscribeBalances()) ], child: MaterialApp( builder: (context, widget) => ResponsiveWrapper.builder( diff --git a/lib/providers/subscribe_balances.dart b/lib/providers/subscribe_balances.dart new file mode 100644 index 0000000..b53ccce --- /dev/null +++ b/lib/providers/subscribe_balances.dart @@ -0,0 +1,38 @@ +// ignore_for_file: avoid_print + +import 'package:flutter/material.dart'; +import 'package:gecko/globals.dart'; +import 'package:gecko/providers/substrate_sdk.dart'; +import 'package:provider/provider.dart'; + +class SubscribeBalances with ChangeNotifier { + Map balances = {}; + + Future> getBalance( + BuildContext context, String address) async { + SubstrateSdk _sub = Provider.of(context, listen: false); + // double balance = 0; + + if (_sub.nodeConnected) { + await _sub.sdk.api.account.subscribeBalance( + address, + (p0) { + balances.clear(); + // balances[address] = int.parse(p0.freeBalance) / 100; + balances.putIfAbsent(address, () => int.parse(p0.freeBalance) / 100); + // balances.update(address, (_) => int.parse(p0.freeBalance) / 100); + log.d('tatatatataata : ' + balances.toString()); + + notifyListeners(); + // return balance; + }, + ); + } + log.d(balances); + return balances; + } + + void reload() { + notifyListeners(); + } +} diff --git a/lib/providers/wallet_options.dart b/lib/providers/wallet_options.dart index 1077986..d5301c7 100644 --- a/lib/providers/wallet_options.dart +++ b/lib/providers/wallet_options.dart @@ -4,6 +4,7 @@ import 'dart:async'; import 'package:gecko/globals.dart'; import 'package:gecko/providers/my_wallets.dart'; import 'package:gecko/models/wallet_data.dart'; +import 'package:gecko/providers/subscribe_balances.dart'; import 'package:gecko/providers/substrate_sdk.dart'; import 'package:gecko/screens/animated_text.dart'; import 'package:gecko/screens/common_elements.dart'; @@ -301,42 +302,55 @@ class WalletOptionsProvider with ChangeNotifier { } } -Map balanceCache = {}; +Map balanceCache = {}; Widget balance(BuildContext context, String address, double size, [Color _color = Colors.black]) { + SubscribeBalances _subBalances = + Provider.of(context, listen: false); + + log.d('Start balance widget: $address'); + return Column(children: [ - Consumer(builder: (context, _sdk, _) { - return FutureBuilder( - future: _sdk.getBalance(address), - builder: (BuildContext context, AsyncSnapshot _balance) { - if (_balance.connectionState != ConnectionState.done || - _balance.hasError) { - if (balanceCache[address] != null) { - return Text(balanceCache[address]!, - style: TextStyle( - fontSize: isTall ? size : size * 0.9, color: _color)); - } else { - return SizedBox( - height: 15, - width: 15, - child: CircularProgressIndicator( - color: orangeC, - strokeWidth: 2, - ), - ); - } + FutureBuilder( + future: _subBalances.getBalance(context, address), + builder: (BuildContext context, + AsyncSnapshot> _balance) { + if (_balance.connectionState != ConnectionState.done || + _balance.hasError) { + if (balanceCache[address] != null) { + log.d('BALANCES: ' + balanceCache[address].toString()); + return Text(balanceCache[address].toString(), + style: TextStyle( + fontSize: isTall ? size : size * 0.9, color: _color)); + } else { + return SizedBox( + height: 15, + width: 15, + child: CircularProgressIndicator( + color: orangeC, + strokeWidth: 2, + ), + ); + } + } + balanceCache[address] = _subBalances.balances[address] ?? 0; + + _subBalances.balances.forEach((key, value) { + if (key == address) { + balanceCache[address] = value; } - balanceCache[address] = "${_balance.data.toString()} $currencyName"; - return Text( - balanceCache[address]!, - style: TextStyle( - fontSize: isTall ? size : size * 0.9, - color: _color, - ), - ); }); - }), + + log.d('balanceCache: $balanceCache'); + return Text( + balanceCache[address].toString(), + style: TextStyle( + fontSize: isTall ? size : size * 0.9, + color: _color, + ), + ); + }), ]); } diff --git a/lib/screens/wallet_view.dart b/lib/screens/wallet_view.dart index 0a875ca..a0c9a36 100644 --- a/lib/screens/wallet_view.dart +++ b/lib/screens/wallet_view.dart @@ -424,8 +424,7 @@ class WalletViewScreen extends StatelessWidget { builder: (BuildContext context, StateSetter setState) { if (_walletViewProvider.payAmount.text != '' && double.parse(_walletViewProvider.payAmount.text) <= - double.parse( - balanceCache[defaultWallet.address]!.split(' ')[0]) && + balanceCache[defaultWallet.address]! && _walletViewProvider.address != defaultWallet.address) { canValidate = true; } else { @@ -520,7 +519,7 @@ class WalletViewScreen extends StatelessWidget { future: _sub.getBalance(defaultWallet.address!), builder: (BuildContext context, - AsyncSnapshot _balance) { + AsyncSnapshot _balance) { if (_balance.connectionState != ConnectionState.done || _balance.hasError) { @@ -529,7 +528,8 @@ class WalletViewScreen extends StatelessWidget { null) { return Text( balanceCache[ - defaultWallet.address!]!, + defaultWallet.address!] + .toString(), style: const TextStyle( fontSize: 20, )); @@ -545,9 +545,10 @@ class WalletViewScreen extends StatelessWidget { } } balanceCache[defaultWallet.address!] = - "${_balance.data.toString()} $currencyName"; + _balance.data!; return Text( - balanceCache[defaultWallet.address!]!, + balanceCache[defaultWallet.address] + .toString(), style: const TextStyle( fontSize: 20, ), @@ -689,7 +690,7 @@ class WalletViewScreen extends StatelessWidget { Provider.of(context, listen: false); // SubstrateSdk _sub = Provider.of(context, listen: false); - bool isAccountExist = balanceCache[pubkey] != '0.0 $currencyName'; + bool isAccountExist = balanceCache[pubkey] != 0; return Stack(children: [ Consumer(builder: (context, _sub, _) { @@ -736,7 +737,8 @@ class WalletViewScreen extends StatelessWidget { balance(context, pubkey!, 22), const SizedBox(height: 10), - _walletOptions.idtyStatus(context, pubkey!, isOwner: false, color: Colors.black), + _walletOptions.idtyStatus(context, pubkey!, + isOwner: false, color: Colors.black), getCerts(context, pubkey!, 14), // if (username == null &&