gecko/lib/ui/historyScreen.dart

270 lines
8.4 KiB
Dart
Raw Normal View History

import 'package:gecko/parsingGVA.dart';
import 'package:gecko/query.dart';
2020-12-21 01:05:00 +01:00
import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';
2021-01-06 05:04:54 +01:00
import 'package:qrscan/qrscan.dart' as scanner;
2020-12-21 01:05:00 +01:00
import 'dart:async';
import 'dart:typed_data';
import 'dart:ui';
2020-12-21 03:59:25 +01:00
import 'package:graphql_flutter/graphql_flutter.dart';
2021-01-06 05:04:54 +01:00
import 'package:permission_handler/permission_handler.dart';
import 'package:sentry/sentry.dart' as sentry;
import 'package:truncate/truncate.dart';
2021-01-06 05:04:54 +01:00
//ignore: must_be_immutable
class HistoryScreen extends StatefulWidget {
const HistoryScreen({Key keyHistory}) : super(key: keyHistory);
2021-01-06 05:04:54 +01:00
@override
State<StatefulWidget> createState() => HistoryScreenState();
2021-01-06 05:04:54 +01:00
}
class HistoryScreenState extends State<HistoryScreen> {
2021-01-06 05:04:54 +01:00
Widget currentScreen;
2020-12-21 01:05:00 +01:00
Uint8List bytes = Uint8List(0);
final TextEditingController _outputPubkey = new TextEditingController();
2021-01-06 05:04:54 +01:00
final nRepositories = 20;
2021-01-08 02:10:23 +01:00
// String pubkey = 'D2meevcAHFTS2gQMvmRW5Hzi25jDdikk4nC4u1FkwRaU'; // For debug
String pubkey = '';
2021-01-06 05:04:54 +01:00
bool isBuilding = true;
2020-12-21 03:59:25 +01:00
ScrollController _scrollController = new ScrollController();
2020-12-21 01:05:00 +01:00
2021-01-06 05:04:54 +01:00
_scrollListener() {
if (_scrollController.offset >=
_scrollController.position.maxScrollExtent &&
!_scrollController.position.outOfRange) {
setState(() {
print("reach the bottom");
});
}
}
@override
void initState() {
super.initState();
_scrollController = ScrollController();
_scrollController.addListener(_scrollListener);
// _scrollController
// ..addListener(() {
// if (_scrollController.position.pixels ==
// _scrollController.position.maxScrollExtent) {
// // print(
// // "DEBUG H fetchMoreCursor in scrollController: $fetchMoreCursor");
// fetchMore(opts);
// }
// });
}
2020-12-21 01:05:00 +01:00
@override
Widget build(BuildContext context) {
print('Build pubkey : ' + pubkey);
print('Build this.pubkey : ' + this.pubkey);
2021-01-01 17:52:55 +01:00
print('isBuilding: ' + isBuilding.toString());
2021-01-06 05:04:54 +01:00
return Column(children: <Widget>[
TextField(
2021-01-08 02:10:23 +01:00
// Entrée de la pubkey
onChanged: (text) {
print("Clé tappxé: $text");
this.pubkey = text;
isPubkey(text);
},
controller: this._outputPubkey,
maxLines: 1,
textAlign: TextAlign.center,
decoration: InputDecoration(
hintText: 'Tappez/Collez une clé publique, ou scannez',
hintStyle: TextStyle(fontSize: 15),
contentPadding: EdgeInsets.symmetric(horizontal: 7, vertical: 15),
border: InputBorder.none,
focusedBorder: InputBorder.none,
enabledBorder: InputBorder.none,
errorBorder: InputBorder.none,
disabledBorder: InputBorder.none,
),
style: TextStyle(
fontSize: 15.0,
color: Colors.black,
fontWeight: FontWeight.bold)),
historyQuery(),
2021-01-06 05:04:54 +01:00
]);
}
2021-01-08 02:10:23 +01:00
historyQuery() {
return Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Query(
options: QueryOptions(
document: gql(getHistory),
variables: <String, dynamic>{
'pubkey': this.pubkey,
'number': nRepositories,
// set cursor to null so as to start at the beginning
'cursor': null
},
),
builder: (QueryResult result, {refetch, FetchMore fetchMore}) {
if (result.isLoading && result.data == null) {
return const Center(
child: CircularProgressIndicator(),
);
}
if (result.hasException) {
return Text('\nErrors: \n ' + result.exception.toString());
}
if (result.data == null && result.exception.toString() == null) {
return const Text('Both data and errors are null');
}
final List<dynamic> blockchainTX =
(result.data['txsHistoryBc']['both']['edges'] as List<dynamic>);
final Map pageInfo =
result.data['txsHistoryBc']['both']['pageInfo'];
2021-01-06 05:04:54 +01:00
final String fetchMoreCursor = pageInfo['endCursor'];
2021-01-08 02:10:23 +01:00
final num balance = result.data['balance']['amount'] / 100;
FetchMoreOptions opts = FetchMoreOptions(
variables: {'cursor': fetchMoreCursor},
updateQuery: (previousResultData, fetchMoreResultData) {
final List<dynamic> repos = [
...previousResultData['txsHistoryBc']['both']['edges']
as List<dynamic>,
...fetchMoreResultData['txsHistoryBc']['both']['edges']
as List<dynamic>
];
fetchMoreResultData['txsHistoryBc']['both']['edges'] = repos;
return fetchMoreResultData;
},
);
2021-01-06 05:04:54 +01:00
// _scrollController
// ..addListener(() {
// if (_scrollController.position.pixels ==
// _scrollController.position.maxScrollExtent) {
// if (!result.isLoading) {
// print(
// "DEBUG H fetchMoreCursor in scrollController: $fetchMoreCursor");
// fetchMore(opts);
// }
// }
// });
// s/o : https://stackoverflow.com/questions/54065354/how-to-detect-scroll-position-of-listview-in-flutter/54188385#54188385
// new NotificationListener(
// child: new ListView(
// controller: _scrollController,
// ),
// onNotification: (t) {
// if (t is ScrollEndNotification) {
// fetchMore(opts);
// }
// },
// );
// fetchMore(opts);
print(
"###### DEBUG H Parse blockchainTX list. Cursor: $fetchMoreCursor ######");
List _transBC = parseHistory(blockchainTX);
2021-01-08 02:10:23 +01:00
// Build history list
return Expanded(
child: ListView(
controller: _scrollController,
children: <Widget>[
if (this.pubkey != '')
Text(balance.toString() + ' Ğ1',
textAlign: TextAlign.center,
style: TextStyle(fontSize: 30.0, color: Colors.black)),
for (var repository in _transBC)
ListTile(
contentPadding: const EdgeInsets.all(5.0),
leading: Text(repository[3].toString()),
title: Text(repository[1].toString() +
'\n' +
truncate(repository[2], 17,
omission: "...", position: TruncatePosition.end)),
subtitle: Text(repository[5]),
dense: true,
onTap: () {
isPubkey(repository[2]);
}),
if (result.isLoading)
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
CircularProgressIndicator(),
],
),
],
));
},
),
],
));
}
2020-12-21 01:05:00 +01:00
Future scan() async {
2020-12-21 01:05:00 +01:00
await Permission.camera.request();
2021-01-06 05:04:54 +01:00
String barcode;
try {
barcode = await scanner.scan();
} catch (e, stack) {
print(e);
if (kReleaseMode) {
await sentry.Sentry.captureException(
e,
stackTrace: stack,
);
}
2021-01-08 05:46:59 +01:00
return 'false';
2021-01-06 05:04:54 +01:00
}
2020-12-21 01:05:00 +01:00
// this._outputPubkey.text = "";
if (barcode != null) {
this._outputPubkey.text = barcode;
2020-12-21 01:05:00 +01:00
isPubkey(barcode);
2021-01-08 05:46:59 +01:00
} else {
return 'false';
2020-12-21 01:05:00 +01:00
}
return barcode;
}
2020-12-21 03:59:25 +01:00
String isPubkey(pubkey) {
2021-01-08 00:16:24 +01:00
final RegExp regExp = new RegExp(
2020-12-21 01:05:00 +01:00
r'^[a-zA-Z0-9]+$',
caseSensitive: false,
multiLine: false,
);
if (regExp.hasMatch(pubkey) == true &&
pubkey.length > 42 &&
pubkey.length < 45) {
print("C'est une pubkey !!!");
2021-01-06 05:04:54 +01:00
setState(() {
this.pubkey = pubkey;
this._outputPubkey.text = pubkey;
2021-01-06 05:04:54 +01:00
});
2021-01-08 02:10:23 +01:00
// setState(() {
// this._outputBalance.text = balance.toString();
// });
return pubkey;
2020-12-21 01:05:00 +01:00
}
return '';
2020-12-21 01:05:00 +01:00
}
}