Compare commits

...

1 Commits

Author SHA1 Message Date
poka 3096b8f0ed wip: try to subscribe to each specifics balances 2022-06-06 19:21:22 +02:00
4 changed files with 95 additions and 39 deletions

View File

@ -24,6 +24,7 @@ import 'package:gecko/models/chest_data.dart';
import 'package:gecko/providers/chest_provider.dart'; import 'package:gecko/providers/chest_provider.dart';
import 'package:gecko/models/g1_wallets_list.dart'; import 'package:gecko/models/g1_wallets_list.dart';
import 'package:gecko/providers/generate_wallets.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/substrate_sdk.dart';
import 'package:gecko/providers/wallets_profiles.dart'; import 'package:gecko/providers/wallets_profiles.dart';
import 'package:gecko/providers/home.dart'; import 'package:gecko/providers/home.dart';
@ -134,7 +135,8 @@ class Gecko extends StatelessWidget {
ChangeNotifierProvider(create: (_) => WalletOptionsProvider()), ChangeNotifierProvider(create: (_) => WalletOptionsProvider()),
ChangeNotifierProvider(create: (_) => SearchProvider()), ChangeNotifierProvider(create: (_) => SearchProvider()),
ChangeNotifierProvider(create: (_) => CesiumPlusProvider()), ChangeNotifierProvider(create: (_) => CesiumPlusProvider()),
ChangeNotifierProvider(create: (_) => SubstrateSdk()) ChangeNotifierProvider(create: (_) => SubstrateSdk()),
ChangeNotifierProvider(create: (_) => SubscribeBalances())
], ],
child: MaterialApp( child: MaterialApp(
builder: (context, widget) => ResponsiveWrapper.builder( builder: (context, widget) => ResponsiveWrapper.builder(

View File

@ -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<String, double> balances = {};
Future<Map<String, double>> getBalance(
BuildContext context, String address) async {
SubstrateSdk _sub = Provider.of<SubstrateSdk>(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();
}
}

View File

@ -4,6 +4,7 @@ import 'dart:async';
import 'package:gecko/globals.dart'; import 'package:gecko/globals.dart';
import 'package:gecko/providers/my_wallets.dart'; import 'package:gecko/providers/my_wallets.dart';
import 'package:gecko/models/wallet_data.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/providers/substrate_sdk.dart';
import 'package:gecko/screens/animated_text.dart'; import 'package:gecko/screens/animated_text.dart';
import 'package:gecko/screens/common_elements.dart'; import 'package:gecko/screens/common_elements.dart';
@ -301,42 +302,55 @@ class WalletOptionsProvider with ChangeNotifier {
} }
} }
Map<String, String> balanceCache = {}; Map<String, double> balanceCache = {};
Widget balance(BuildContext context, String address, double size, Widget balance(BuildContext context, String address, double size,
[Color _color = Colors.black]) { [Color _color = Colors.black]) {
SubscribeBalances _subBalances =
Provider.of<SubscribeBalances>(context, listen: false);
log.d('Start balance widget: $address');
return Column(children: <Widget>[ return Column(children: <Widget>[
Consumer<SubstrateSdk>(builder: (context, _sdk, _) { FutureBuilder(
return FutureBuilder( future: _subBalances.getBalance(context, address),
future: _sdk.getBalance(address), builder: (BuildContext context,
builder: (BuildContext context, AsyncSnapshot<num?> _balance) { AsyncSnapshot<Map<String, double>> _balance) {
if (_balance.connectionState != ConnectionState.done || if (_balance.connectionState != ConnectionState.done ||
_balance.hasError) { _balance.hasError) {
if (balanceCache[address] != null) { if (balanceCache[address] != null) {
return Text(balanceCache[address]!, log.d('BALANCES: ' + balanceCache[address].toString());
style: TextStyle( return Text(balanceCache[address].toString(),
fontSize: isTall ? size : size * 0.9, color: _color)); style: TextStyle(
} else { fontSize: isTall ? size : size * 0.9, color: _color));
return SizedBox( } else {
height: 15, return SizedBox(
width: 15, height: 15,
child: CircularProgressIndicator( width: 15,
color: orangeC, child: CircularProgressIndicator(
strokeWidth: 2, 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,
),
);
}),
]); ]);
} }

View File

@ -424,8 +424,7 @@ class WalletViewScreen extends StatelessWidget {
builder: (BuildContext context, StateSetter setState) { builder: (BuildContext context, StateSetter setState) {
if (_walletViewProvider.payAmount.text != '' && if (_walletViewProvider.payAmount.text != '' &&
double.parse(_walletViewProvider.payAmount.text) <= double.parse(_walletViewProvider.payAmount.text) <=
double.parse( balanceCache[defaultWallet.address]! &&
balanceCache[defaultWallet.address]!.split(' ')[0]) &&
_walletViewProvider.address != defaultWallet.address) { _walletViewProvider.address != defaultWallet.address) {
canValidate = true; canValidate = true;
} else { } else {
@ -520,7 +519,7 @@ class WalletViewScreen extends StatelessWidget {
future: future:
_sub.getBalance(defaultWallet.address!), _sub.getBalance(defaultWallet.address!),
builder: (BuildContext context, builder: (BuildContext context,
AsyncSnapshot<num?> _balance) { AsyncSnapshot<double> _balance) {
if (_balance.connectionState != if (_balance.connectionState !=
ConnectionState.done || ConnectionState.done ||
_balance.hasError) { _balance.hasError) {
@ -529,7 +528,8 @@ class WalletViewScreen extends StatelessWidget {
null) { null) {
return Text( return Text(
balanceCache[ balanceCache[
defaultWallet.address!]!, defaultWallet.address!]
.toString(),
style: const TextStyle( style: const TextStyle(
fontSize: 20, fontSize: 20,
)); ));
@ -545,9 +545,10 @@ class WalletViewScreen extends StatelessWidget {
} }
} }
balanceCache[defaultWallet.address!] = balanceCache[defaultWallet.address!] =
"${_balance.data.toString()} $currencyName"; _balance.data!;
return Text( return Text(
balanceCache[defaultWallet.address!]!, balanceCache[defaultWallet.address]
.toString(),
style: const TextStyle( style: const TextStyle(
fontSize: 20, fontSize: 20,
), ),
@ -689,7 +690,7 @@ class WalletViewScreen extends StatelessWidget {
Provider.of<WalletOptionsProvider>(context, listen: false); Provider.of<WalletOptionsProvider>(context, listen: false);
// SubstrateSdk _sub = Provider.of<SubstrateSdk>(context, listen: false); // SubstrateSdk _sub = Provider.of<SubstrateSdk>(context, listen: false);
bool isAccountExist = balanceCache[pubkey] != '0.0 $currencyName'; bool isAccountExist = balanceCache[pubkey] != 0;
return Stack(children: <Widget>[ return Stack(children: <Widget>[
Consumer<SubstrateSdk>(builder: (context, _sub, _) { Consumer<SubstrateSdk>(builder: (context, _sub, _) {
@ -736,7 +737,8 @@ class WalletViewScreen extends StatelessWidget {
balance(context, pubkey!, 22), balance(context, pubkey!, 22),
const SizedBox(height: 10), 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), getCerts(context, pubkey!, 14),
// if (username == null && // if (username == null &&