From 267a3e0ba14714b7bd4db70d528bd298c54581ac Mon Sep 17 00:00:00 2001 From: poka Date: Sat, 30 Jan 2021 15:24:08 +0100 Subject: [PATCH] Improve few things ... --- config/gva_endpoints.json | 5 +- lib/globals.dart | 2 + lib/main.dart | 4 +- lib/models/generateWallets.dart | 4 +- lib/models/history.dart | 59 ++++++++++ lib/models/home.dart | 6 +- lib/models/myWallets.dart | 3 +- lib/models/parsingGVA.dart | 53 --------- lib/models/{query.dart => queries.dart} | 0 lib/screens/history.dart | 192 ++++++++++++++++++++++---------- pubspec.lock | 42 +++++++ pubspec.yaml | 1 + 12 files changed, 254 insertions(+), 117 deletions(-) delete mode 100644 lib/models/parsingGVA.dart rename lib/models/{query.dart => queries.dart} (100%) diff --git a/config/gva_endpoints.json b/config/gva_endpoints.json index d45a173..64ac739 100644 --- a/config/gva_endpoints.json +++ b/config/gva_endpoints.json @@ -1,3 +1,4 @@ [ - "https://g1.librelois.fr/gva" -] \ No newline at end of file + "https://g1.librelois.fr/gva", + "http://localhost:30901/gva" +] diff --git a/lib/globals.dart b/lib/globals.dart index 4f09d83..bd3f455 100644 --- a/lib/globals.dart +++ b/lib/globals.dart @@ -1,5 +1,7 @@ import 'dart:io'; +import 'package:shared_preferences/shared_preferences.dart'; Directory appPath; Directory walletsDirectory; String appVersion; +SharedPreferences prefs; diff --git a/lib/main.dart b/lib/main.dart index 1cc13a4..1bdad08 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -12,6 +12,7 @@ import 'package:graphql_flutter/graphql_flutter.dart'; import 'package:provider/provider.dart'; import 'package:sentry_flutter/sentry_flutter.dart'; import 'package:flutter/foundation.dart'; +import 'package:shared_preferences/shared_preferences.dart'; final bool enableSentry = true; @@ -24,11 +25,12 @@ Future main() async { HomeProvider _homeProvider = HomeProvider(); await _homeProvider.getAppPath(); appVersion = await _homeProvider.getAppVersion(); + prefs = await SharedPreferences.getInstance(); String randomEndpoint; // = await getRandomEndpoint(); int i = 0; do { - if (i >= 3) { + if (i >= 5) { print('NO VALID ENDPOINT FOUND !'); break; } diff --git a/lib/models/generateWallets.dart b/lib/models/generateWallets.dart index 9f0dc39..36ed99f 100644 --- a/lib/models/generateWallets.dart +++ b/lib/models/generateWallets.dart @@ -42,8 +42,8 @@ class GenerateWalletsProvider with ChangeNotifier { } await walletNameDirectory.create(); - walletFile.writeAsString('${wallet.dewif}'); - walletPubkey.writeAsString('${wallet.publicKey}'); + await walletFile.writeAsString('${wallet.dewif}'); + await walletPubkey.writeAsString('${wallet.publicKey}'); Navigator.pop(context, true); Navigator.pop(context, wallet.publicKey); diff --git a/lib/models/history.dart b/lib/models/history.dart index bca6755..b058519 100644 --- a/lib/models/history.dart +++ b/lib/models/history.dart @@ -3,6 +3,8 @@ import 'package:flutter/material.dart'; import 'package:permission_handler/permission_handler.dart'; import 'package:sentry/sentry.dart' as sentry; import 'package:qrscan/qrscan.dart' as scanner; +import 'dart:math'; +import 'package:intl/intl.dart'; class HistoryProvider with ChangeNotifier { String pubkey = ''; @@ -56,6 +58,63 @@ class HistoryProvider with ChangeNotifier { return ''; } + List parseHistory(txs) { + var transBC = []; + int i = 0; + + final currentBase = 0; + double currentUD = 10.54; + + for (final trans in txs) { + var direction = trans['direction']; + final transaction = trans['node']; + var output = transaction['outputs'][0]; + + transBC.add(i); + transBC[i] = []; + final dateBrut = DateTime.fromMillisecondsSinceEpoch( + transaction['writtenTime'] * 1000); + final DateFormat formatter = DateFormat('dd-MM-yy\nHH:mm'); + final date = formatter.format(dateBrut); + transBC[i].add(transaction['writtenTime']); + transBC[i].add(date); + // print( + // "DEBUG date et comment: ${date.toString()} -- ${transaction['comment'].toString()}"); + int amountBrut = int.parse(output.split(':')[0]); + final base = int.parse(output.split(':')[1]); + final int applyBase = base - currentBase; + final num amount = + removeDecimalZero(amountBrut * pow(10, applyBase) / 100); + num amountUD = amount / currentUD; + if (direction == "RECEIVED") { + transBC[i].add(transaction['issuers'][0]); + transBC[i].add(amount.toString()); + transBC[i].add(amountUD.toStringAsFixed(2)); + } else if (direction == "SENT") { + final outPubkey = output.split("SIG(")[1].replaceAll(')', ''); + transBC[i].add(outPubkey); + transBC[i].add('- ' + amount.toString()); + transBC[i].add(amountUD.toStringAsFixed(2)); + } + transBC[i].add(transaction['comment']); + + i++; + } + + // transBC.sort((b, a) => Comparable.compare(a[0], b[0])); + return transBC; + } + + void resetdHistory() { + this._outputPubkey.text = ''; + notifyListeners(); + } + + num removeDecimalZero(double n) { + String result = n.toStringAsFixed(n.truncateToDouble() == n ? 0 : 1); + return num.parse(result); + } + // num getBalance(_pubkey) { // getBalance(_pubkey); // } diff --git a/lib/models/home.dart b/lib/models/home.dart index 623f29e..73ee694 100644 --- a/lib/models/home.dart +++ b/lib/models/home.dart @@ -34,7 +34,11 @@ class HomeProvider with ChangeNotifier { // print(_json); // final _list = _json[]; - final _listEndpoints = ['https://g1.librelois.fr/gva']; + // final _listEndpoints = ['https://g1.librelois.fr/gva']; + final _listEndpoints = [ + 'https://g1.librelois.fr/gva', + 'https://duniter-gva.axiom-team.fr/gva' + ]; final _endpoint = getRandomElement(_listEndpoints); print('ENDPOINT: ' + _endpoint); diff --git a/lib/models/myWallets.dart b/lib/models/myWallets.dart index 4e7b762..03fc253 100644 --- a/lib/models/myWallets.dart +++ b/lib/models/myWallets.dart @@ -37,7 +37,8 @@ class MyWalletsProvider with ChangeNotifier { .listSync(recursive: false, followLinks: false) .forEach((wallet) { String _name = wallet.path.split('/').last; - String _pubkey = File(wallet.path + '/pubkey').readAsLinesSync()[0]; + List _pubkeyList = File(wallet.path + '/pubkey').readAsLinesSync(); + String _pubkey = _pubkeyList[0]; print("$_name: $_pubkey"); listWallets[_name] = _pubkey; // i++; diff --git a/lib/models/parsingGVA.dart b/lib/models/parsingGVA.dart deleted file mode 100644 index 49ddb5c..0000000 --- a/lib/models/parsingGVA.dart +++ /dev/null @@ -1,53 +0,0 @@ -import 'dart:math'; -import 'package:intl/intl.dart'; - -num removeDecimalZero(double n) { - String result = n.toStringAsFixed(n.truncateToDouble() == n ? 0 : 1); - return num.parse(result); -} - -List parseHistory(txs) { - var transBC = []; - int i = 0; - - final currentBase = 0; - double currentUD = 10.54; - - for (final trans in txs) { - var direction = trans['direction']; - final transaction = trans['node']; - var output = transaction['outputs'][0]; - - transBC.add(i); - transBC[i] = []; - final dateBrut = - DateTime.fromMillisecondsSinceEpoch(transaction['writtenTime'] * 1000); - final DateFormat formatter = DateFormat('dd-MM-yy\nHH:mm'); - final date = formatter.format(dateBrut); - transBC[i].add(transaction['writtenTime']); - transBC[i].add(date); - print( - "DEBUG date et comment: ${date.toString()} -- ${transaction['comment'].toString()}"); - int amountBrut = int.parse(output.split(':')[0]); - final base = int.parse(output.split(':')[1]); - final int applyBase = base - currentBase; - final num amount = removeDecimalZero(amountBrut * pow(10, applyBase) / 100); - num amountUD = amount / currentUD; - if (direction == "RECEIVED") { - transBC[i].add(transaction['issuers'][0]); - transBC[i].add(amount.toString()); - transBC[i].add(amountUD.toStringAsFixed(2)); - } else if (direction == "SENT") { - final outPubkey = output.split("SIG(")[1].replaceAll(')', ''); - transBC[i].add(outPubkey); - transBC[i].add(amount.toString()); - transBC[i].add(amountUD.toStringAsFixed(2)); - } - transBC[i].add(transaction['comment']); - - i++; - } - - // transBC.sort((b, a) => Comparable.compare(a[0], b[0])); - return transBC; -} diff --git a/lib/models/query.dart b/lib/models/queries.dart similarity index 100% rename from lib/models/query.dart rename to lib/models/queries.dart diff --git a/lib/screens/history.dart b/lib/screens/history.dart index 66a877a..9bc1248 100644 --- a/lib/screens/history.dart +++ b/lib/screens/history.dart @@ -1,5 +1,5 @@ -import 'package:gecko/models/parsingGVA.dart'; -import 'package:gecko/models/query.dart'; +import 'package:flutter/services.dart'; +import 'package:gecko/models/queries.dart'; import 'package:gecko/models/history.dart'; import 'package:flutter/material.dart'; import 'package:flutter/foundation.dart'; @@ -16,6 +16,8 @@ class HistoryScreen extends StatelessWidget with ChangeNotifier { // HistoryProvider _historyProvider; bool isTheEnd = false; List _transBC; + final _formKey = GlobalKey(); + FocusNode _pubkeyFocus = FocusNode(); FetchMore fetchMore; FetchMoreOptions opts; @@ -47,6 +49,8 @@ class HistoryScreen extends StatelessWidget with ChangeNotifier { body: Column(children: [ SizedBox(height: 8), TextField( + autofocus: false, + focusNode: _pubkeyFocus, // Entrée de la pubkey onChanged: (text) { print("Clé tappxé: $text"); @@ -72,6 +76,7 @@ class HistoryScreen extends StatelessWidget with ChangeNotifier { } historyQuery(context) { + _pubkeyFocus.unfocus(); HistoryProvider _historyProvider = Provider.of(context); return Expanded( child: Column( @@ -117,8 +122,8 @@ class HistoryScreen extends StatelessWidget with ChangeNotifier { final String fetchMoreCursor = pageInfo['endCursor']; - final num balance = - removeDecimalZero(result.data['balance']['amount'] / 100); + final num balance = _historyProvider + .removeDecimalZero(result.data['balance']['amount'] / 100); if (fetchMoreCursor != null) { opts = FetchMoreOptions( @@ -140,77 +145,55 @@ class HistoryScreen extends StatelessWidget with ChangeNotifier { print( "###### DEBUG H Parse blockchainTX list. Cursor: $fetchMoreCursor ######"); if (fetchMoreCursor != null) { - _transBC = parseHistory(blockchainTX); + _transBC = _historyProvider.parseHistory(blockchainTX); isTheEnd = false; } else { print("###### DEBUG H - Début de l'historique"); isTheEnd = true; } + // _historyProvider.resetdHistory(); + // Build history list return NotificationListener( child: Expanded( child: ListView( controller: scrollController, children: [ - SizedBox(height: 7), + SizedBox(height: 15), if (_historyProvider.pubkey != '') Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ - Container(width: 32.0, height: 0.0), + Container(width: 70.0, height: 0.0), Text(balance.toString() + ' Ğ1', textAlign: TextAlign.center, style: TextStyle(fontSize: 30.0)), Container( - padding: const EdgeInsets.only(right: 80), - child: Text("Payer")), + padding: const EdgeInsets.only(right: 15), + child: IconButton( + icon: Icon(Icons.payments), + onPressed: () { + showDialog( + context: context, + builder: (BuildContext context) { + return paymentPopup(context); + }); + }, + iconSize: 30, + color: Color(0xFFB16E16))) ]), - SizedBox(height: 12), - for (var repository in _transBC) - Padding( - padding: const EdgeInsets.symmetric(horizontal: 8.0), - child: ListTile( - contentPadding: const EdgeInsets.all(5.0), - leading: Text(repository[1].toString(), - style: TextStyle( - fontSize: 12, - color: Colors.grey[800], - fontWeight: FontWeight.w700), - textAlign: TextAlign.center), - title: Text(repository[5], - style: TextStyle(fontSize: 14.0), - textAlign: TextAlign.center), - subtitle: Text( - truncate(repository[2], 20, - omission: "...", - position: TruncatePosition.end), - style: TextStyle(fontSize: 11.0), - textAlign: TextAlign.center), - trailing: Text("${repository[3]} Ğ1", - style: TextStyle(fontSize: 14.0), - textAlign: TextAlign.justify), - dense: true, - isThreeLine: false, - onTap: () { - // this._outputPubkey.text = repository[2]; - _historyProvider.isPubkey(repository[2]); - })), - if (result.isLoading) - Row( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - CircularProgressIndicator(), - ], - ), - if (isTheEnd) - Column(children: [ - SizedBox(height: 15), - Text("Début de l'historique.", - textAlign: TextAlign.center, - style: TextStyle(fontSize: 20)), - SizedBox(height: 15) - ]) + SizedBox(height: 15), + const Divider( + color: Colors.grey, + height: 5, + thickness: 0.5, + indent: 0, + endIndent: 0, + ), + _transBC == null + ? Text('Aucune transaction à afficher.') + : loopTransactions(context, result), ], )), onNotification: (t) { @@ -227,8 +210,103 @@ class HistoryScreen extends StatelessWidget with ChangeNotifier { )); } - num removeDecimalZero(double n) { - String result = n.toStringAsFixed(n.truncateToDouble() == n ? 0 : 1); - return num.parse(result); + Widget loopTransactions(context, result) { + HistoryProvider _historyProvider = Provider.of(context); + + return Column(children: [ + for (var repository in _transBC) + Padding( + padding: const EdgeInsets.symmetric(horizontal: 8.0), + child: ListTile( + contentPadding: const EdgeInsets.all(5.0), + leading: Text(repository[1].toString(), + style: TextStyle( + fontSize: 12, + color: Colors.grey[800], + fontWeight: FontWeight.w700), + textAlign: TextAlign.center), + title: Text(repository[5], + style: TextStyle(fontSize: 14.0), + textAlign: TextAlign.center), + subtitle: Text( + truncate(repository[2], 20, + omission: "...", position: TruncatePosition.end), + style: TextStyle(fontSize: 11.0), + textAlign: TextAlign.center), + trailing: Text("${repository[3]} Ğ1", + style: TextStyle(fontSize: 14.0), + textAlign: TextAlign.justify), + dense: true, + isThreeLine: false, + onTap: () { + // this._outputPubkey.text = repository[2]; + _historyProvider.isPubkey(repository[2]); + })), + if (result.isLoading) + Row( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + CircularProgressIndicator(), + ], + ), + if (isTheEnd) + Column(children: [ + SizedBox(height: 15), + Text("Début de l'historique.", + textAlign: TextAlign.center, style: TextStyle(fontSize: 20)), + SizedBox(height: 15) + ]) + ]); + } + + Widget paymentPopup(context) { + return AlertDialog( + content: Stack( + overflow: Overflow.visible, + children: [ + Form( + key: _formKey, + child: Column( + mainAxisSize: MainAxisSize.min, + children: [ + Text('À:'), + Padding( + padding: EdgeInsets.all(8.0), + child: Text(this._outputPubkey.text, + textAlign: TextAlign.center, + style: + TextStyle(fontSize: 15, fontWeight: FontWeight.w500)), + ), + SizedBox(height: 20), + Text('Montant (Ğ1):'), + Padding( + padding: EdgeInsets.all(8.0), + child: TextFormField( + textAlign: TextAlign.center, + autofocus: true, + keyboardType: TextInputType.number, + inputFormatters: [ + FilteringTextInputFormatter.allow(RegExp(r'(^\d*\.?\d*)')) + ], + ), + ), + Padding( + padding: const EdgeInsets.all(8.0), + child: RaisedButton( + child: Text("Payer"), + color: Color(0xffFFD68E), + onPressed: () { + if (_formKey.currentState.validate()) { + _formKey.currentState.save(); + } + }, + ), + ) + ], + ), + ), + ], + ), + ); } } diff --git a/pubspec.lock b/pubspec.lock index 2966c75..68051ef 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -492,6 +492,48 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "4.0.1" + shared_preferences: + dependency: "direct main" + description: + name: shared_preferences + url: "https://pub.dartlang.org" + source: hosted + version: "0.5.12+4" + shared_preferences_linux: + dependency: transitive + description: + name: shared_preferences_linux + url: "https://pub.dartlang.org" + source: hosted + version: "0.0.2+4" + shared_preferences_macos: + dependency: transitive + description: + name: shared_preferences_macos + url: "https://pub.dartlang.org" + source: hosted + version: "0.0.1+11" + shared_preferences_platform_interface: + dependency: transitive + description: + name: shared_preferences_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.4" + shared_preferences_web: + dependency: transitive + description: + name: shared_preferences_web + url: "https://pub.dartlang.org" + source: hosted + version: "0.1.2+7" + shared_preferences_windows: + dependency: transitive + description: + name: shared_preferences_windows + url: "https://pub.dartlang.org" + source: hosted + version: "0.0.2+2" sky_engine: dependency: transitive description: flutter diff --git a/pubspec.yaml b/pubspec.yaml index 62fd33e..7a2a49a 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -30,6 +30,7 @@ dependencies: super_tooltip: ^0.9.6 package_info: ^0.4.3+2 printing: ^4.0.0 + shared_preferences: ^0.5.12+4 flutter_icons: