Skip to content
Snippets Groups Projects
Commit 70fafdad authored by vjrj's avatar vjrj
Browse files

Added retry to gva.history

parent 988e958c
No related branches found
No related tags found
No related merge requests found
...@@ -31,7 +31,7 @@ class TransactionsCubit extends HydratedCubit<TransactionsAndBalanceState> { ...@@ -31,7 +31,7 @@ class TransactionsCubit extends HydratedCubit<TransactionsAndBalanceState> {
Future<void> fetchTransactions(NodeListCubit cubit) async { Future<void> fetchTransactions(NodeListCubit cubit) async {
logger('Loading transactions'); logger('Loading transactions');
final Map<String, dynamic>? txData = final Map<String, dynamic>? txData =
await gva().history(SharedPreferencesHelper().getPubKey()); await gvaHistoryAndBalance(SharedPreferencesHelper().getPubKey());
if (txData == null) { if (txData == null) {
logger('Failed to get transactions'); logger('Failed to get transactions');
return; return;
......
...@@ -411,10 +411,7 @@ Future<String> pay( ...@@ -411,10 +411,7 @@ Future<String> pay(
final String node = output; final String node = output;
try { try {
final Gva gva = Gva(node: node); final Gva gva = Gva(node: node);
logger('Trying $node to get balance');
final CesiumWallet wallet = await SharedPreferencesHelper().getWallet(); final CesiumWallet wallet = await SharedPreferencesHelper().getWallet();
logger('Current balance ${await gva.balance(wallet.pubkey)}');
// logger('Current balance ${await gva.balance(wallet.pubkey)}');
logger( logger(
'Trying $node to send $amount to $to with comment ${comment ?? ''}'); 'Trying $node to send $amount to $to with comment ${comment ?? ''}');
...@@ -448,40 +445,33 @@ String getGvaNode() { ...@@ -448,40 +445,33 @@ String getGvaNode() {
} }
} }
Future<double> gvaBalance() async { Future<Map<String, dynamic>?> gvaHistoryAndBalance(String pubKey) async {
final String output = getGvaNode(); final List<Node> nodes = NodeManager().nodeList(NodeType.gva);
if (Uri.tryParse(output) != null) { if (nodes.isEmpty) {
final String node = output; nodes.addAll(defaultGvaNodes);
try {
final Gva gva = Gva(node: node);
logger('Trying $node to get balance');
final String pubKey = SharedPreferencesHelper().getPubKey();
final double balance = await gva.balance(pubKey);
logger('Current balance $balance');
return balance;
} catch (e, stacktrace) {
// move logger outside main
logger(e);
logger(stacktrace);
throw Exception('Oops! failed to obtain balance');
}
} }
throw Exception('Sorry: I cannot find a working node to get your balance'); for (int i = 0; i < nodes.length; i++) {
} final Node node = nodes[i];
if (node.errors >= NodeManager.maxNodeErrors) {
Gva gva() { logger('Too much errors skip ${node.url}');
final String output = getGvaNode(); continue;
if (Uri.tryParse(output) != null) { }
final String node = output;
try { try {
final Gva gva = Gva(node: node); final String output = getGvaNode();
return gva; if (Uri.tryParse(output) != null) {
} catch (e, stacktrace) { final String node = output;
// move logger outside main final Gva gva = Gva(node: node);
logger(e); final Map<String, dynamic>? result = await gva.history(pubKey);
logger(stacktrace); return result;
throw Exception('Oops! failed to obtain balance'); }
} catch (e) {
logger('Error trying ${node.url} $e');
logger('Increasing node errors of ${node.url} (${node.errors})');
NodeManager()
.updateNode(NodeType.gva, node.copyWith(errors: node.errors + 1));
continue;
} }
} }
throw Exception('Sorry: I cannot find a working node to get your balance'); throw Exception(
'Sorry: I cannot find a working node to get your transactions');
} }
...@@ -11,7 +11,6 @@ import '../../../data/models/transaction.dart'; ...@@ -11,7 +11,6 @@ import '../../../data/models/transaction.dart';
import '../../../data/models/transaction_cubit.dart'; import '../../../data/models/transaction_cubit.dart';
import '../../../shared_prefs.dart'; import '../../../shared_prefs.dart';
import '../../ui_helpers.dart'; import '../../ui_helpers.dart';
import '../loading_box.dart';
import 'transaction_chart.dart'; import 'transaction_chart.dart';
import 'transaction_item.dart'; import 'transaction_item.dart';
...@@ -52,20 +51,18 @@ class _TransactionsAndBalanceWidgetState ...@@ -52,20 +51,18 @@ class _TransactionsAndBalanceWidgetState
if (/* _transScrollController.position.pixels == if (/* _transScrollController.position.pixels ==
_transScrollController.position.maxScrollExtent || */ _transScrollController.position.maxScrollExtent || */
_transScrollController.offset == 0) { _transScrollController.offset == 0) {
await _refreshTransactions(); // await _refreshTransactions();
_refreshIndicatorKey.currentState?.show();
} }
} }
Future<void> _refreshTransactions() async { Future<void> _refreshTransactions() async {
setState(() { return transCubit.fetchTransactions(nodeListCubit);
isLoading = true;
});
await transCubit.fetchTransactions(nodeListCubit);
setState(() {
isLoading = false;
});
} }
final GlobalKey<RefreshIndicatorState> _refreshIndicatorKey =
GlobalKey<RefreshIndicatorState>();
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final String myPubKey = SharedPreferencesHelper().getPubKey(); final String myPubKey = SharedPreferencesHelper().getPubKey();
...@@ -76,67 +73,70 @@ class _TransactionsAndBalanceWidgetState ...@@ -76,67 +73,70 @@ class _TransactionsAndBalanceWidgetState
final ContactsCubit contactsCubit = context.read<ContactsCubit>(); final ContactsCubit contactsCubit = context.read<ContactsCubit>();
final List<Transaction> transactions = transBalanceState.transactions; final List<Transaction> transactions = transBalanceState.transactions;
final double balance = transBalanceState.balance; final double balance = transBalanceState.balance;
if (!isLoading) { return BackdropScaffold(
return BackdropScaffold( appBar: BackdropAppBar(
appBar: BackdropAppBar( backgroundColor: Theme.of(context).colorScheme.inversePrimary,
backgroundColor: Theme.of(context).colorScheme.inversePrimary, title: Text(tr('balance')),
title: Text(tr('balance')), actions: <Widget>[
actions: <Widget>[ IconButton(
IconButton( icon: const Icon(Icons.refresh),
icon: const Icon(Icons.refresh), onPressed: () {
onPressed: () { _refreshIndicatorKey.currentState?.show();
_refreshTransactions(); // _refreshTransactions();
}, },
), ),
const BackdropToggleButton( const BackdropToggleButton(
// The default // The default
// icon: AnimatedIcons.close_menu, // icon: AnimatedIcons.close_menu,
) )
], ],
), ),
backLayer: Center( backLayer: Center(
child: Container( child: Container(
decoration: BoxDecoration( decoration: BoxDecoration(
color: Theme.of(context).colorScheme.inversePrimary, color: Theme.of(context).colorScheme.inversePrimary,
border: Border.all( border: Border.all(
color: Theme.of(context).colorScheme.inversePrimary, color: Theme.of(context).colorScheme.inversePrimary,
width: 3), width: 3),
/* borderRadius: const BorderRadius.only( /* borderRadius: const BorderRadius.only(
topLeft: Radius.circular(8), topLeft: Radius.circular(8),
topRight: Radius.circular(8), topRight: Radius.circular(8),
), */ ), */
), ),
child: Scrollbar( child: Scrollbar(
child: ListView( child: ListView(
// controller: scrollController, // controller: scrollController,
children: <Widget>[ children: <Widget>[
Padding( Padding(
padding: const EdgeInsets.symmetric(vertical: 10.0), padding: const EdgeInsets.symmetric(vertical: 10.0),
child: Center( child: Center(
child: Text( child: Text(
formatKAmount(context, balance), formatKAmount(context, balance),
style: TextStyle( style: TextStyle(
fontSize: 36.0, fontSize: 36.0,
color: balance == 0 color:
? Colors.lightBlue balance == 0 ? Colors.lightBlue : Colors.lightBlue,
: Colors.lightBlue, fontWeight: FontWeight.bold),
fontWeight: FontWeight.bold), )),
)), ),
), if (!kReleaseMode) TransactionChart()
if (!kReleaseMode) TransactionChart() /*BalanceChart(
/*BalanceChart(
transactions: .transactions),*/ transactions: .transactions),*/
], ],
)),
)), )),
subHeader: BackdropSubHeader( )),
title: Text(tr('transactions')), subHeader: BackdropSubHeader(
title: Text(tr('transactions')),
divider: Divider(
color: Theme.of(context).colorScheme.surfaceVariant,
height: 0,
), ),
frontLayer: Center( ),
child: Column( frontLayer: Center(
crossAxisAlignment: CrossAxisAlignment.start, child: Column(
children: <Widget>[ crossAxisAlignment: CrossAxisAlignment.start,
/* Container( children: <Widget>[
/* Container(
/* color: Theme /* color: Theme
.of(context) .of(context)
.colorScheme .colorScheme
...@@ -146,31 +146,37 @@ class _TransactionsAndBalanceWidgetState ...@@ -146,31 +146,37 @@ class _TransactionsAndBalanceWidgetState
child: const Padding( child: const Padding(
padding: EdgeInsets.symmetric(horizontal: 16), padding: EdgeInsets.symmetric(horizontal: 16),
child: Header(text: 'transactions'))), */ child: Header(text: 'transactions'))), */
Expanded( Expanded(
child: Padding( child: Padding(
padding: const EdgeInsets.fromLTRB(0, 0, 0, 50), padding: const EdgeInsets.fromLTRB(0, 0, 0, 50),
child: transactions.isEmpty child: transactions.isEmpty
? Padding( ? Padding(
padding: const EdgeInsets.symmetric( padding: const EdgeInsets.symmetric(horizontal: 20),
horizontal: 20), child: Center(child: Text(tr('no_transactions'))))
child: Center( : RefreshIndicator(
child: Text(tr('no_transactions')))) key: _refreshIndicatorKey,
: ListView.builder( color: Colors.white,
physics: backgroundColor:
const AlwaysScrollableScrollPhysics(), Theme.of(context).colorScheme.primary,
shrinkWrap: true, strokeWidth: 4.0,
controller: _transScrollController, onRefresh: () async {
itemCount: transactions.length, return _refreshTransactions();
// Size of elements },
// itemExtent: 100, // Pull from top to show refresh indicator.
itemBuilder: child: ListView.builder(
(BuildContext context, int index) { physics: const AlwaysScrollableScrollPhysics(),
return TransactionListItem( shrinkWrap: true,
index: index, controller: _transScrollController,
transaction: transactions[index], itemCount: transactions.length,
avatar: avatar(null), // Size of elements
); // itemExtent: 100,
/* itemBuilder: (BuildContext context, int index) {
return TransactionListItem(
index: index,
transaction: transactions[index],
avatar: avatar(null),
);
/*
Slidable( Slidable(
// Specify a key if the Slidable is dismissible. // Specify a key if the Slidable is dismissible.
...@@ -238,14 +244,11 @@ class _TransactionsAndBalanceWidgetState ...@@ -238,14 +244,11 @@ class _TransactionsAndBalanceWidgetState
: Colors.blue)), : Colors.blue)),
)); ));
*/ */
}, },
)), )),
) ))
]), ]),
)); ));
} else {
return const LoadingScreen();
}
}); });
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment