diff --git a/assets/icon_user.png b/assets/icon_user.png new file mode 100644 index 0000000..dbf8244 Binary files /dev/null and b/assets/icon_user.png differ diff --git a/lib/main.dart b/lib/main.dart index 39ea86e..84eda74 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,5 +1,6 @@ import 'package:dubp/dubp.dart'; import 'package:gecko/globals.dart'; +import 'package:gecko/models/cesiumPlus.dart'; import 'package:gecko/models/generateWallets.dart'; import 'package:gecko/models/history.dart'; import 'package:gecko/models/home.dart'; @@ -24,6 +25,7 @@ Future main() async { WidgetsFlutterBinding.ensureInitialized(); HomeProvider _homeProvider = HomeProvider(); await _homeProvider.getAppPath(); + await _homeProvider.createDefaultAvatar(); appVersion = await _homeProvider.getAppVersion(); prefs = await SharedPreferences.getInstance(); @@ -88,7 +90,8 @@ class Gecko extends StatelessWidget { ChangeNotifierProvider(create: (_) => HistoryProvider('')), ChangeNotifierProvider(create: (_) => MyWalletsProvider()), ChangeNotifierProvider(create: (_) => GenerateWalletsProvider()), - ChangeNotifierProvider(create: (_) => WalletOptionsProvider()) + ChangeNotifierProvider(create: (_) => WalletOptionsProvider()), + ChangeNotifierProvider(create: (_) => CesiumPlusProvider()) ], child: GraphQLProvider( client: _client, diff --git a/lib/models/cesiumPlus.dart b/lib/models/cesiumPlus.dart new file mode 100644 index 0000000..4c4bb7a --- /dev/null +++ b/lib/models/cesiumPlus.dart @@ -0,0 +1,124 @@ +import 'dart:convert'; +import 'dart:io'; +import 'package:flutter/foundation.dart'; +import 'package:flutter/material.dart'; +import 'package:gecko/globals.dart'; +import 'package:http/http.dart' as http; +import 'package:path_provider/path_provider.dart'; + +class CesiumPlusProvider with ChangeNotifier { + // String pubkey = ''; + // CesiumPlusProvider(this.pubkey); + var decodedBytes; + var avatar64; + TextEditingController cesiumName = TextEditingController(); + int iAvatar = 0; + bool isComplete = false; + + Future _buildQuery(_pubkey) async { + var queryGetAvatar = json.encode({ + "query": { + "bool": { + "should": [ + { + "match": { + '_id': {"query": _pubkey, "boost": 2} + } + }, + { + "prefix": {'_id': _pubkey} + } + ] + } + }, + "highlight": { + "fields": {"title": {}, "tags": {}} + }, + "from": 0, + "size": 100, + "_source": [ + "title", + "avatar", + "avatar._content_type", + "description", + "city", + "address", + "socials.url", + "creationTime", + "membersCount", + "type" + ], + "indices_boost": {"user": 100, "page": 1, "group": 0.01} + }); + + String cesiumPod = "https://g1.data.le-sou.org"; + String requestUrl = "/user,page,group/profile,record/_search"; + String podRequest = cesiumPod + requestUrl; + + Map headers = { + 'Content-type': 'application/json', + 'Accept': 'application/json', + }; + + return [podRequest, queryGetAvatar, headers]; + } + + Future getName(String _pubkey) async { + String _name; + + List queryOptions = await _buildQuery(_pubkey); + final response = await http.post(queryOptions[0], + body: queryOptions[1], headers: queryOptions[2]); + // print('RESULT CESIUM QUERY: ${response.body}'); //For debug + final responseJson = json.decode(response.body); + if (responseJson['hits']['hits'].toString() == '[]') { + return ''; + } + final bool avatarExist = + responseJson['hits']['hits'][0]['_source'].containsKey("title"); + if (!avatarExist) { + return ''; + } + _name = responseJson['hits']['hits'][0]['_source']['title']; + print(_name); + + return _name; + } + + Future getAvatar(String _pubkey) async { + List queryOptions = await _buildQuery(_pubkey); + final response = await http.post(queryOptions[0], + body: queryOptions[1], headers: queryOptions[2]); + // print('RESULT CESIUM QUERY: ${response.body}'); //For debug + final responseJson = json.decode(response.body); + if (responseJson['hits']['hits'].toString() == '[]') { + return [File(appPath.path + '/default_avatar.png')]; + } + final bool avatarExist = + responseJson['hits']['hits'][0]['_source'].containsKey("avatar"); + if (!avatarExist) { + return [File(appPath.path + '/default_avatar.png')]; + } + final _avatar = + responseJson['hits']['hits'][0]['_source']['avatar']['_content']; + + var avatarFile = + File('${(await getTemporaryDirectory()).path}/avatar$iAvatar.png'); + // final bool _isAvatarExist = await avatarFile.exists(); + // if (_isAvatarExist) { + // await avatarFile.delete(); + // } + await avatarFile.writeAsBytes(base64.decode(_avatar)); + iAvatar++; + isComplete = true; + + return [avatarFile]; + } + + // isNameComplete() { + // while (isComplete == false) { + // print(isComplete); + // Duration(milliseconds: 50); + // } + // } +} diff --git a/lib/models/history.dart b/lib/models/history.dart index 8f1293c..59cb126 100644 --- a/lib/models/history.dart +++ b/lib/models/history.dart @@ -12,7 +12,7 @@ class HistoryProvider with ChangeNotifier { // String pubkey = 'D2meevcAHFTS2gQMvmRW5Hzi25jDdikk4nC4u1FkwRaU'; // For debug String pubkey = ''; HistoryProvider(this.pubkey); - final TextEditingController _outputPubkey = new TextEditingController(); + final TextEditingController outputPubkey = new TextEditingController(); bool isTheEnd = false; List transBC; bool isFirstBuild = true; @@ -33,7 +33,7 @@ class HistoryProvider with ChangeNotifier { return 'false'; } if (barcode != null) { - this._outputPubkey.text = barcode; + this.outputPubkey.text = barcode; isPubkey(barcode); } else { return 'false'; @@ -54,7 +54,7 @@ class HistoryProvider with ChangeNotifier { print("C'est une pubkey !!!"); this.pubkey = pubkey; - this._outputPubkey.text = pubkey; + this.outputPubkey.text = pubkey; notifyListeners(); return pubkey; @@ -149,8 +149,6 @@ class HistoryProvider with ChangeNotifier { snackNode(context) { if (isFirstBuild) { String _message; - print('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'); - print(endPointGVA); if (endPointGVA == 'HS') { _message = "Aucun noeud Duniter disponible, veuillez réessayer ultérieurement"; @@ -165,12 +163,12 @@ class HistoryProvider with ChangeNotifier { } void resetdHistory() { - this._outputPubkey.text = ''; + this.outputPubkey.text = ''; notifyListeners(); } num removeDecimalZero(double n) { - String result = n.toStringAsFixed(n.truncateToDouble() == n ? 0 : 1); + String result = n.toStringAsFixed(n.truncateToDouble() == n ? 0 : 2); return num.parse(result); } diff --git a/lib/models/home.dart b/lib/models/home.dart index 73ee694..3fc78b6 100644 --- a/lib/models/home.dart +++ b/lib/models/home.dart @@ -2,6 +2,7 @@ import 'dart:io'; import 'dart:math'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; import 'dart:async'; import 'package:gecko/globals.dart'; import 'package:package_info/package_info.dart'; @@ -63,6 +64,16 @@ class HomeProvider with ChangeNotifier { } } + Future createDefaultAvatar() async { + File defaultAvatar = File(appPath.path + '/default_avatar.png'); + final bool isAvatarExist = await defaultAvatar.exists(); + if (!isAvatarExist) { + final byteData = await rootBundle.load('assets/icon_user.png'); + await defaultAvatar.writeAsBytes(byteData.buffer + .asUint8List(byteData.offsetInBytes, byteData.lengthInBytes)); + } + } + T getRandomElement(List list) { final random = new Random(); var i = random.nextInt(list.length); diff --git a/lib/screens/history.dart b/lib/screens/history.dart index 9bea156..aca33f3 100644 --- a/lib/screens/history.dart +++ b/lib/screens/history.dart @@ -1,4 +1,8 @@ +import 'dart:io'; + import 'package:flutter/services.dart'; +import 'package:gecko/globals.dart'; +import 'package:gecko/models/cesiumPlus.dart'; import 'package:gecko/models/queries.dart'; import 'package:gecko/models/history.dart'; import 'package:flutter/material.dart'; @@ -16,6 +20,7 @@ class HistoryScreen extends StatelessWidget with ChangeNotifier { // HistoryProvider _historyProvider; final _formKey = GlobalKey(); FocusNode _pubkeyFocus = FocusNode(); + List cesiumData; FetchMore fetchMore; FetchMoreOptions opts; @@ -49,7 +54,7 @@ class HistoryScreen extends StatelessWidget with ChangeNotifier { ), ), body: Column(children: [ - SizedBox(height: 8), + SizedBox(height: 20), TextField( autofocus: false, focusNode: _pubkeyFocus, @@ -80,6 +85,8 @@ class HistoryScreen extends StatelessWidget with ChangeNotifier { historyQuery(context) { _pubkeyFocus.unfocus(); HistoryProvider _historyProvider = Provider.of(context); + CesiumPlusProvider _cesiumPlusProvider = + Provider.of(context); return Expanded( child: Column( mainAxisAlignment: MainAxisAlignment.start, @@ -122,6 +129,7 @@ class HistoryScreen extends StatelessWidget with ChangeNotifier { opts = _historyProvider.checkQueryResult(result, opts); // Build history list + // _cesiumPlusProvider.cesiumName.text = "NAMEE"; return NotificationListener( child: Expanded( child: ListView( @@ -131,13 +139,51 @@ class HistoryScreen extends StatelessWidget with ChangeNotifier { if (_historyProvider.pubkey != '') Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, + mainAxisSize: MainAxisSize.min, + crossAxisAlignment: CrossAxisAlignment.center, children: [ - Container(width: 70.0, height: 0.0), + Container( + padding: const EdgeInsets.only(left: 30), + child: FutureBuilder( + future: _cesiumPlusProvider + .getAvatar(_historyProvider.pubkey), + initialData: [ + File(appPath.path + '/default_avatar.png') + ], + builder: (BuildContext context, + AsyncSnapshot _avatar) { + cesiumData = _avatar.data; + // _cesiumPlusProvider.isComplete = true; + if (_avatar.connectionState != + ConnectionState.done) { + return Image.file( + File(appPath.path + + '/default_avatar.png'), + height: 65); + } + if (_avatar.hasError) { + return Image.file( + File(appPath.path + + '/default_avatar.png'), + height: 65); + } + if (_avatar.hasData) { + return SingleChildScrollView( + padding: EdgeInsets.all(0.0), + child: Image.file(_avatar.data[0], + height: 65)); + } + return Image.file( + File(appPath.path + + '/default_avatar.png'), + height: 65); + })), Text(balance.toString() + ' Ğ1', textAlign: TextAlign.center, style: TextStyle(fontSize: 30.0)), Container( - padding: const EdgeInsets.only(right: 15), + padding: const EdgeInsets.fromLTRB( + 30, 0, 15, 0), // .only(right: 15), child: IconButton( icon: Icon(Icons.payments), onPressed: () { @@ -150,7 +196,22 @@ class HistoryScreen extends StatelessWidget with ChangeNotifier { iconSize: 30, color: Color(0xFFB16E16))) ]), - SizedBox(height: 15), + SizedBox(height: 10), + Row( + mainAxisAlignment: MainAxisAlignment.spaceAround, + crossAxisAlignment: CrossAxisAlignment.center, + children: [ + Container( + // padding: const EdgeInsets., + child: FutureBuilder( + future: _cesiumPlusProvider + .getName(_historyProvider.pubkey), + initialData: '', + builder: (context, snapshot) { + return Text(snapshot.data); + })) + ]), + SizedBox(height: 20), const Divider( color: Colors.grey, height: 5, diff --git a/lib/screens/myWallets/confirmWalletStorage.dart b/lib/screens/myWallets/confirmWalletStorage.dart index e64188c..b4b81bd 100644 --- a/lib/screens/myWallets/confirmWalletStorage.dart +++ b/lib/screens/myWallets/confirmWalletStorage.dart @@ -49,7 +49,7 @@ class ConfirmStoreWallet extends StatelessWidget with ChangeNotifier { }), title: SizedBox( height: 22, - child: Text('Confirmez ce portefeuille'), + child: Text('Enregistrer ce portefeuille'), )), body: Center( child: Column(children: [ diff --git a/lib/screens/myWallets/generateWallets.dart b/lib/screens/myWallets/generateWallets.dart index c085097..e48ce21 100644 --- a/lib/screens/myWallets/generateWallets.dart +++ b/lib/screens/myWallets/generateWallets.dart @@ -157,6 +157,7 @@ class GenerateWalletsScreen extends StatelessWidget { child: Row( mainAxisAlignment: MainAxisAlignment.center, children: [ + SizedBox(width: 20), Column(children: [ SizedBox( width: 30, diff --git a/pubspec.lock b/pubspec.lock index 68051ef..299602e 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -574,6 +574,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.9.6" + sync_http: + dependency: "direct main" + description: + name: sync_http + url: "https://pub.dartlang.org" + source: hosted + version: "0.2.0" term_glyph: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 76c4b84..bb3adfc 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -5,7 +5,7 @@ description: A new Flutter project. # pub.dev using `pub publish`. This is preferred for private packages. publish_to: 'none' # Remove this line if you wish to publish to pub.dev -version: 0.0.1+0 +version: 0.0.1+2 environment: sdk: ">=2.7.0 <3.0.0" @@ -31,6 +31,7 @@ dependencies: package_info: ^0.4.3+2 printing: ^4.0.0 shared_preferences: ^0.5.12+4 + sync_http: ^0.2.0 flutter_icons: @@ -53,3 +54,4 @@ flutter: - assets/icon/gecko_final.png - assets/ - assets/OpenSans-Regular.ttf + - assets/icon_user.png