Improve view; Parse and display history

This commit is contained in:
poka 2020-12-13 18:31:17 +01:00
parent 2a24c15f17
commit 81915382f3
2 changed files with 143 additions and 32 deletions

View File

@ -1,3 +1,4 @@
import 'dart:math';
import "package:dio/dio.dart" as dio; import "package:dio/dio.dart" as dio;
import 'package:gql/language.dart' as gqlLang; import 'package:gql/language.dart' as gqlLang;
import 'package:gql_dio_link/gql_dio_link.dart'; import 'package:gql_dio_link/gql_dio_link.dart';
@ -7,7 +8,7 @@ import "package:gql_link/gql_link.dart";
// Configure node // Configure node
const graphqlEndpoint = "https://g1.librelois.fr/gva"; const graphqlEndpoint = "https://g1.librelois.fr/gva";
// Build query // Build queries
Future buildQ(query) async { Future buildQ(query) async {
final client = dio.Dio(); final client = dio.Dio();
final Link link = DioLink( final Link link = DioLink(
@ -64,18 +65,55 @@ Future getHistory(String pubkey) async {
comment comment
} }
} }
currentUd {
amount
base
}
}"""; }""";
final res = await buildQ(query); final res = await buildQ(query);
// Parse history // Parse history
var result = res.data["transactionsHistory"]["received"]; var result = res.data["transactionsHistory"]["received"];
String outPubkey; var i = 0;
// String outPubkey;
var trans = [];
final currentBase = res.data['currentUd']['base'];
final currentUD = res.data['currentUd']['amount'] / 100;
for (var bloc in result) { for (var bloc in result) {
var output = bloc['outputs'][0]; var output = bloc['outputs'][0];
outPubkey = output.split("SIG(")[1].replaceAll(')', ''); // outPubkey = output.split("SIG(")[1].replaceAll(')', '');
print(outPubkey); trans.add(i);
trans[i] = [];
trans[i].add(bloc['writtenTime']);
trans[i].add(bloc['issuers'][0]);
var amountBrut = int.parse(output.split(':')[0]);
final base = int.parse(output.split(':')[1]);
final applyBase = base - currentBase;
final amount = amountBrut * pow(10, applyBase) / 100;
trans[i].add(amount);
final amountUD = amount / currentUD;
trans[i].add(amountUD.toStringAsFixed(2));
trans[i].add(bloc['comment']);
trans[i].add(base);
i++;
} }
return outPubkey; // Order transactions by date
trans.sort((b, a) => Comparable.compare(a[0], b[0]));
// // Keep only base if there is base change
// var lastBase = 0;
// for (i in trans) {
// if (i[5] == lastBase){
// i[6] = null;
// }
// else {
// lastBase = i[6];
// }
// print(trans);
return trans;
} }

View File

@ -19,17 +19,30 @@ class MyApp extends StatefulWidget {
class _MyAppState extends State<MyApp> { class _MyAppState extends State<MyApp> {
Uint8List bytes = Uint8List(0); Uint8List bytes = Uint8List(0);
TextEditingController _outputPubkey; TextEditingController _outputPubkey;
TextEditingController _outputAmount; TextEditingController _outputBalance;
TextEditingController _outputHistory;
@override @override
initState() { initState() {
super.initState(); super.initState();
this._outputPubkey = new TextEditingController(); this._outputPubkey = new TextEditingController();
this._outputAmount = new TextEditingController(); this._outputBalance = new TextEditingController();
this._outputHistory = new TextEditingController();
} }
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
// final List<String> names = <String>[
// 'Aby',
// 'Aish',
// 'Ayan',
// 'Ben',
// 'Bob',
// 'Charlie',
// 'Cook',
// 'Carline'
// ];
// final List<int> msgCount = <int>[2, 0, 10, 6, 52, 4, 0, 2];
return MaterialApp( return MaterialApp(
home: Scaffold( home: Scaffold(
backgroundColor: Colors.grey[300], backgroundColor: Colors.grey[300],
@ -43,32 +56,66 @@ class _MyAppState extends State<MyApp> {
children: <Widget>[ children: <Widget>[
SizedBox(height: 20), SizedBox(height: 20),
TextField( TextField(
controller: this._outputPubkey, enabled: false,
maxLines: 2, controller: this._outputPubkey,
decoration: InputDecoration( maxLines: 1,
prefixIcon: Icon(Icons.wrap_text), decoration: InputDecoration(
helperText: 'Clé publique scanné', hintText: 'Clé publique scanné',
hintText: 'Clé publique scanné', hintStyle: TextStyle(fontSize: 15),
hintStyle: TextStyle(fontSize: 15), contentPadding: EdgeInsets.symmetric(
contentPadding: EdgeInsets.symmetric( horizontal: 7, vertical: 15),
horizontal: 7, vertical: 15), ),
), style: TextStyle(
), fontSize: 15.0,
color: Colors.black,
fontWeight: FontWeight.bold)),
TextField( TextField(
controller: this._outputAmount, enabled: false,
maxLines: 2, controller: this._outputBalance,
decoration: InputDecoration( maxLines: 1,
prefixIcon: Icon(Icons.wrap_text), textAlign: TextAlign.center,
helperText: 'Solde du compte scanné', decoration: InputDecoration(
hintText: 'Solde du compte scanné', hintText: 'Solde du compte scanné',
hintStyle: TextStyle(fontSize: 15), hintStyle: TextStyle(fontSize: 15),
contentPadding: EdgeInsets.symmetric( contentPadding: EdgeInsets.symmetric(
horizontal: 7, vertical: 15), horizontal: 7, vertical: 15),
), ),
), style:
TextStyle(fontSize: 18.0, color: Colors.black)),
TextField(
enabled: false,
controller: this._outputHistory,
maxLines: null,
keyboardType: TextInputType.multiline,
decoration: InputDecoration(
prefixIcon: Icon(Icons.wrap_text),
hintText: 'Historique du compte scanné',
hintStyle: TextStyle(fontSize: 15),
contentPadding: EdgeInsets.symmetric(
horizontal: 7, vertical: 15),
),
style: TextStyle(
fontSize: 14.0,
height: 1.5,
color: Colors.black)),
SizedBox(height: 20), SizedBox(height: 20),
this._buttonGroup(), this._buttonGroup(),
SizedBox(height: 70), SizedBox(height: 70),
// Expanded(
// child: ListView.builder(
// padding: const EdgeInsets.all(8),
// itemCount: names.length,
// itemBuilder: (BuildContext context, int index) {
// return Container(
// height: 50,
// margin: EdgeInsets.all(2),
// child: Center(
// child: Text(
// '${names[index]} (${msgCount[index]})',
// style: TextStyle(fontSize: 18),
// )),
// );
// }))
], ],
), ),
), ),
@ -121,12 +168,38 @@ class _MyAppState extends State<MyApp> {
} else { } else {
print("Debug: " + barcode); print("Debug: " + barcode);
this._outputPubkey.text = ""; this._outputPubkey.text = "";
this._outputAmount.text = ""; this._outputBalance.text = "";
this._outputHistory.text = "";
// final udValue = await getUD(); // final udValue = await getUD();
final myBalance = await getBalance(barcode.toString()); final myBalance = await getBalance(barcode.toString());
final myHistory = await getHistory(barcode.toString());
this._outputPubkey.text = barcode; this._outputPubkey.text = barcode;
print(myBalance.toString()); this._outputBalance.text = myBalance.toString() + " Ḡ1";
this._outputAmount.text = myBalance.toString();
String historyBloc = "";
var j = 0;
for (var i in myHistory) {
print(i);
var date = i[0];
date = DateTime.fromMillisecondsSinceEpoch(date * 1000);
final issuer = i[1];
final amount = i[2];
// final amountUD = i[3];
final comment = i[4];
historyBloc += date.toString() +
" | " +
issuer.toString() +
" | " +
amount.toString() +
" | " +
comment.toString() +
"\n---\n";
j++;
if (j >= 10) {
break;
}
}
this._outputHistory.text = historyBloc;
} }
} }