gecko/lib/screens/history.dart

370 lines
16 KiB
Dart
Raw Normal View History

2021-02-02 20:33:09 +01:00
import 'dart:io';
2021-01-30 15:24:08 +01:00
import 'package:flutter/services.dart';
2021-02-02 20:33:09 +01:00
import 'package:gecko/globals.dart';
import 'package:gecko/models/cesiumPlus.dart';
2021-01-30 15:24:08 +01:00
import 'package:gecko/models/queries.dart';
2021-01-23 16:38:03 +01:00
import 'package:gecko/models/history.dart';
2020-12-21 01:05:00 +01:00
import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';
2020-12-21 01:05:00 +01:00
import 'dart:ui';
2020-12-21 03:59:25 +01:00
import 'package:graphql_flutter/graphql_flutter.dart';
import 'package:provider/provider.dart';
2021-01-06 05:04:54 +01:00
2021-01-26 21:00:26 +01:00
// ignore: must_be_immutable
2021-01-23 16:38:03 +01:00
class HistoryScreen extends StatelessWidget with ChangeNotifier {
2021-01-26 21:00:26 +01:00
final TextEditingController _outputPubkey = TextEditingController();
ScrollController scrollController = ScrollController();
2021-01-06 05:04:54 +01:00
final nRepositories = 20;
2021-01-29 03:44:09 +01:00
// HistoryProvider _historyProvider;
2021-01-30 15:24:08 +01:00
final _formKey = GlobalKey<FormState>();
FocusNode _pubkeyFocus = FocusNode();
2021-02-02 20:33:09 +01:00
List cesiumData;
final double avatarsSize = 80;
FetchMore fetchMore;
FetchMoreOptions opts;
2021-01-06 05:04:54 +01:00
2020-12-21 01:05:00 +01:00
@override
Widget build(BuildContext context) {
HistoryProvider _historyProvider = Provider.of<HistoryProvider>(context);
2021-01-26 21:00:26 +01:00
this._outputPubkey.text = _historyProvider.pubkey;
print('Build pubkey : ' + _historyProvider.pubkey);
// _historyProvider.snackNode(context);
WidgetsBinding.instance.addPostFrameCallback((_) {
_historyProvider.snackNode(context);
});
2021-01-10 07:49:29 +01:00
return Scaffold(
floatingActionButton: Container(
height: 80.0,
width: 80.0,
child: FittedBox(
child: FloatingActionButton(
heroTag: "buttonScan",
onPressed: () async {
await _historyProvider.scan();
2021-01-10 07:49:29 +01:00
},
child: Container(
height: 40.0,
width: 40.0,
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 3),
child: Image.asset('assets/qrcode-scan.png'))),
2021-01-10 07:49:29 +01:00
backgroundColor: Color(
0xffEFEFBF), //Color(0xffFFD68E), //Color.fromARGB(500, 204, 255, 255),
),
),
2021-01-10 07:49:29 +01:00
),
body: Column(children: <Widget>[
SizedBox(height: 0),
if (_historyProvider.pubkey != '')
historyQuery(context, _historyProvider),
2021-01-10 07:49:29 +01:00
]));
}
Widget historyQuery(context, HistoryProvider _historyProvider) {
2021-01-30 15:24:08 +01:00
_pubkeyFocus.unfocus();
// HistoryProvider _historyProvider = Provider.of<HistoryProvider>(context);
2021-02-02 20:33:09 +01:00
CesiumPlusProvider _cesiumPlusProvider =
Provider.of<CesiumPlusProvider>(context);
print("I'M HERE 1");
2021-02-07 02:59:48 +01:00
bool _isFirstExec = true;
return Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Query(
options: QueryOptions(
document: gql(getHistory),
variables: <String, dynamic>{
'pubkey': _historyProvider.pubkey,
'number': nRepositories,
'cursor': null
},
),
builder: (QueryResult result, {fetchMore, refetch}) {
2021-02-07 02:59:48 +01:00
print("I'M HERE 2 ! $_isFirstExec");
// print(result.source.isEager);
if (result.isLoading && result.data == null) {
print("I'M HERE 3 !");
return const Center(
child: CircularProgressIndicator(),
);
}
if (result.hasException) {
print('Error GVA: ' + result.exception.toString());
return Column(children: <Widget>[
SizedBox(height: 50),
Text(
"Aucun noeud GVA valide n'a pu être trouvé.\nVeuillez réessayer ultérieurement.",
style: TextStyle(fontSize: 17.0),
)
]);
}
if (result.data == null && result.exception.toString() == null) {
return const Text('Aucune donnée à afficher.');
}
2021-01-30 15:24:08 +01:00
final num balance = _historyProvider
.removeDecimalZero(result.data['balance']['amount'] / 100);
2021-01-08 02:10:23 +01:00
opts = _historyProvider.checkQueryResult(
result, opts, _outputPubkey.text);
2021-01-30 15:24:08 +01:00
2021-02-07 02:59:48 +01:00
// _historyProvider.transBC = null;
2021-01-08 02:10:23 +01:00
// Build history list
return NotificationListener(
child: Expanded(
child: ListView(
controller: scrollController,
children: <Widget>[
SizedBox(height: 20),
if (_historyProvider.pubkey != '')
2021-01-29 14:21:57 +01:00
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
2021-02-02 20:33:09 +01:00
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
2021-01-29 14:21:57 +01:00
children: [
2021-02-07 02:59:48 +01:00
if (_isFirstExec)
Container(
padding:
const EdgeInsets.fromLTRB(12, 0, 5, 0),
2021-02-07 02:59:48 +01:00
child: FutureBuilder(
future: _cesiumPlusProvider
.getAvatar(_historyProvider.pubkey),
initialData: [
File(appPath.path +
'/default_avatar.png')
],
builder: (BuildContext context,
AsyncSnapshot<List> _avatar) {
cesiumData = _avatar.data;
// _cesiumPlusProvider.isComplete = true;
if (_avatar.connectionState !=
ConnectionState.done) {
return Image.file(
File(appPath.path +
'/default_avatar.png'),
height: avatarsSize);
2021-02-07 02:59:48 +01:00
}
if (_avatar.hasError) {
return Image.file(
File(appPath.path +
'/default_avatar.png'),
height: avatarsSize);
2021-02-07 02:59:48 +01:00
}
if (_avatar.hasData) {
return SingleChildScrollView(
padding: EdgeInsets.all(0.0),
child: Image.file(_avatar.data[0],
height: avatarsSize));
2021-02-07 02:59:48 +01:00
}
2021-02-02 20:33:09 +01:00
return Image.file(
File(appPath.path +
'/default_avatar.png'),
height: avatarsSize);
2021-02-07 02:59:48 +01:00
})),
GestureDetector(
onTap: () {
Clipboard.setData(ClipboardData(
text: _historyProvider.pubkey));
_historyProvider.snackCopyKey(context);
},
child: Text(
_historyProvider
.getShortPubkey(_historyProvider.pubkey),
style: TextStyle(
fontSize: 22,
fontWeight: FontWeight.w800,
fontFamily: 'Monospace')),
),
2021-01-29 14:21:57 +01:00
Container(
2021-02-02 20:33:09 +01:00
padding: const EdgeInsets.fromLTRB(
30, 0, 5, 0), // .only(right: 15),
child: Text('TODO')),
SizedBox(width: 0)
2021-01-29 14:21:57 +01:00
]),
2021-02-07 02:59:48 +01:00
if (_isFirstExec)
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
padding: const EdgeInsets.fromLTRB(0, 0, 0, 0),
2021-02-07 02:59:48 +01:00
// padding: const EdgeInsets.,
child: FutureBuilder(
future: _cesiumPlusProvider
.getName(_historyProvider.pubkey),
initialData: '...',
2021-02-07 02:59:48 +01:00
builder: (context, snapshot) {
return Text(
snapshot.data != ''
? snapshot.data
: '-',
style: TextStyle(fontSize: 20));
2021-02-07 02:59:48 +01:00
}))
]),
SizedBox(height: 18),
2021-02-16 03:55:01 +01:00
if (_isFirstExec)
Container(
padding: const EdgeInsets.fromLTRB(0, 0, 0, 0),
child: Text(balance.toString() + ' Ğ1',
textAlign: TextAlign.center,
style: TextStyle(fontSize: 18.0))),
2021-02-02 20:33:09 +01:00
SizedBox(height: 20),
ElevatedButton(
style: ElevatedButton.styleFrom(
elevation: 1,
primary: Colors.grey[50], // background
onPrimary: Colors.black, // foreground
),
onPressed: () {
_historyProvider.switchProfileView();
},
child: Text(_historyProvider.historySwitchButtun,
style: TextStyle(
fontSize: 15, color: Color(0xffD28928)))),
// const Divider(
// color: Colors.grey,
// height: 5,
// thickness: 0.5,
// indent: 0,
// endIndent: 0,
// ),
_historyProvider.isHistoryScreen
? historyView(context, result)
: payView(context),
],
)),
onNotification: (t) {
if (t is ScrollEndNotification &&
scrollController.position.pixels >=
2021-01-29 14:21:57 +01:00
scrollController.position.maxScrollExtent * 0.7) {
fetchMore(opts);
}
return true;
});
},
),
],
));
}
2020-12-21 01:05:00 +01:00
Widget payView(context) {
TextEditingController payComment = new TextEditingController();
2021-01-30 15:24:08 +01:00
return Stack(
overflow: Overflow.visible,
children: <Widget>[
Form(
key: _formKey,
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
SizedBox(height: 20),
Text('Commentaire:'),
Padding(
2021-01-30 15:24:08 +01:00
padding: EdgeInsets.all(8.0),
child: TextField(
controller: payComment,
maxLines: 2,
2021-01-30 15:24:08 +01:00
textAlign: TextAlign.center,
decoration: InputDecoration(),
style: TextStyle(
fontSize: 14.0,
color: Colors.black,
fontWeight: FontWeight.bold))),
SizedBox(height: 20),
Text('Montant (Ğ1):'),
Padding(
padding: EdgeInsets.all(8.0),
child: TextFormField(
textAlign: TextAlign.center,
maxLines: 1,
keyboardType: TextInputType.number,
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.allow(RegExp(r'(^\d*\.?\d*)'))
],
2021-01-30 15:24:08 +01:00
),
),
Padding(
2021-02-17 01:13:47 +01:00
padding: const EdgeInsets.only(top: 15),
child: OutlineButton(
borderSide: BorderSide(width: 2, color: Color(0xffD28928)),
onPressed: () {
if (_formKey.currentState.validate()) {
_formKey.currentState.save();
}
},
child: Padding(
padding: const EdgeInsets.all(12),
child: Text("PAYER",
style: TextStyle(
fontSize: 25, color: Colors.grey[850]))),
))
],
2021-01-30 15:24:08 +01:00
),
),
],
2021-01-30 15:24:08 +01:00
);
2021-01-09 10:48:06 +01:00
}
Widget historyView(context, result) {
HistoryProvider _historyProvider = Provider.of<HistoryProvider>(context);
return _historyProvider.transBC == null
? Text('Aucune transaction à afficher.')
: Column(children: <Widget>[
for (var repository in _historyProvider.transBC)
Padding(
padding: const EdgeInsets.symmetric(horizontal: 5.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[3],
style: TextStyle(
fontSize: 15.0, fontFamily: 'Monospace'),
textAlign: TextAlign.center),
subtitle: Text(repository[6] != '' ? repository[6] : '-',
style: TextStyle(fontSize: 12.0),
textAlign: TextAlign.center),
trailing: Text("${repository[4]} Ğ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: <Widget>[
CircularProgressIndicator(),
],
),
// if (_historyProvider.isTheEnd) // What I did before ...
if (!_historyProvider.pageInfo['hasPreviousPage'])
Column(children: <Widget>[
SizedBox(height: 15),
Text("Début de l'historique.",
textAlign: TextAlign.center,
style: TextStyle(fontSize: 20)),
SizedBox(height: 15)
])
]);
}
}