From a1f2b92d5bb075a801c0133c7ffd3d688ed2a95e Mon Sep 17 00:00:00 2001 From: poka Date: Mon, 11 Jan 2021 19:13:28 +0100 Subject: [PATCH] Add random endpoint choossing in main; WIP: Add commented code for wallet list refresh; Add TODO --- config/gva_endpoints.json | 3 ++ lib/main.dart | 61 ++++++++++++++++++++++++++++++++++--- lib/ui/generateWallets.dart | 13 ++++---- lib/ui/myWallets.dart | 2 +- pubspec.lock | 2 +- pubspec.yaml | 2 ++ 6 files changed, 71 insertions(+), 12 deletions(-) create mode 100644 config/gva_endpoints.json diff --git a/config/gva_endpoints.json b/config/gva_endpoints.json new file mode 100644 index 0000000..d45a173 --- /dev/null +++ b/config/gva_endpoints.json @@ -0,0 +1,3 @@ +[ + "https://g1.librelois.fr/gva" +] \ No newline at end of file diff --git a/lib/main.dart b/lib/main.dart index 75d4242..736ccd9 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -4,31 +4,84 @@ import 'package:flutter/material.dart'; import 'package:graphql_flutter/graphql_flutter.dart'; import 'package:sentry_flutter/sentry_flutter.dart'; import 'package:flutter/foundation.dart'; +import 'package:http/http.dart' as http; +// import 'package:flutter/services.dart' show rootBundle; +import 'dart:math'; +// import 'dart:convert'; -// void main() => runApp(Gecko()); final bool enableSentry = true; +// Future getJsonEndpoints() { +// return rootBundle.loadString('config/gva_endpoints.json'); +// } + +T getRandomElement(List list) { + final random = new Random(); + var i = random.nextInt(list.length); + return list[i]; +} + +Future getRandomEndpoint() async { + // TODO: Improve implemention of getRandomEndpoint() with JSON and error handling + // final _json = json.decode(await getJsonEndpoints()); + // print('JSON !! :'); + // print(_json); + // final _list = _json[]; + + final _list = ['https://g1.librelois.fr/gva']; + final _endpoint = getRandomElement(_list); + print('ENDPOINT: ' + _endpoint); + + // http.post(_endpoint); + final response = await http.post(_endpoint); + if (response.statusCode != 200) { + print('Endpoint statutcode: ' + response.statusCode.toString()); + // _endpoint = getRandomElement(_list); + return 'HS'; + } + + return _endpoint; +} + Future main() async { + String randomEndpoint; // = await getRandomEndpoint(); + int i = 0; + do { + print(i); + if (i >= 3) { + print('NO VALID ENDPOINT FOUND !'); + break; + } + if (i != 0) { + await Future.delayed(Duration(milliseconds: 500)); + } + randomEndpoint = await getRandomEndpoint(); + i++; + } while (randomEndpoint == 'HS'); + if (kReleaseMode && enableSentry) { await SentryFlutter.init( (options) { options.dsn = 'https://c09587b46eaa42e8b9fda28d838ed180@o496840.ingest.sentry.io/5572110'; }, - appRunner: () => runApp(Gecko()), + appRunner: () => runApp(Gecko(randomEndpoint)), ); } else { print('Debug mode enabled: No sentry alerte'); - runApp(Gecko()); + runApp(Gecko(randomEndpoint)); } } class Gecko extends StatelessWidget { + Gecko(this.randomEndpoint); + final String randomEndpoint; + @override Widget build(BuildContext context) { final _httpLink = HttpLink( // 'http://192.168.1.91:10060/gva', - 'https://g1.librelois.fr/gva', + randomEndpoint, ); final _client = ValueNotifier( diff --git a/lib/ui/generateWallets.dart b/lib/ui/generateWallets.dart index aa0ecc1..8f1f326 100644 --- a/lib/ui/generateWallets.dart +++ b/lib/ui/generateWallets.dart @@ -14,7 +14,7 @@ class GenerateWalletScreen extends StatefulWidget { } class GenerateWalletState extends State { - GlobalKey _keyWallets = GlobalKey(); + GlobalKey _keyMyWallets = GlobalKey(); GlobalKey _keyValidWallets = GlobalKey(); void initState() { super.initState(); @@ -102,7 +102,7 @@ class GenerateWalletState extends State { if (fSnapshot.hasData) return Visibility( visible: fSnapshot.data, - child: MyWalletsScreen(keyWallets: _keyWallets)); + child: MyWalletsScreen(keyMyWallets: _keyMyWallets)); return Center(child: CircularProgressIndicator()); }), SizedBox(height: 8), @@ -164,7 +164,7 @@ class GenerateWalletState extends State { fontSize: 30.0, color: Colors.black, fontWeight: FontWeight.bold)), - SizedBox(height: 12), + SizedBox(height: 20), // Expanded(child: Align(alignment: Alignment.bottomCenter)), new RaisedButton( color: Color(0xffFFD68E), @@ -192,12 +192,12 @@ class GenerateWalletState extends State { // setState(() {}); // getAllWalletsNames(); // checkIfWalletExist('tata'); - // _keyWallets.currentState.getAllWalletsNames(); + // _keyMyWallets.currentState.getAllWalletsNames(); } })); } : null, - child: Text('Enregistrer ce wallet', + child: Text('Enregistrer ce portefeuille', style: TextStyle(fontSize: 20))), SizedBox(height: 20) ]), @@ -219,7 +219,8 @@ class GenerateWalletState extends State { final bool _isExist = await checkIfWalletExist('tata'); print('The wallet exist in resetWalletState(): ' + _isExist.toString()); // initState(); - // _keyWallets.currentState.setState(() {}); + // _keyMyWallets.currentState.setState(() {}); + // _keyMyWallets.currentState.initAppDirectory(); setState(() { // getAllWalletsNames(); // this.walletIsGenerated = true; diff --git a/lib/ui/myWallets.dart b/lib/ui/myWallets.dart index 780be3d..04c1bee 100644 --- a/lib/ui/myWallets.dart +++ b/lib/ui/myWallets.dart @@ -9,7 +9,7 @@ import 'package:path_provider/path_provider.dart'; import 'package:pin_code_fields/pin_code_fields.dart'; class MyWalletsScreen extends StatefulWidget { - const MyWalletsScreen({Key keyWallets}) : super(key: keyWallets); + const MyWalletsScreen({Key keyMyWallets}) : super(key: keyMyWallets); @override MyWalletState createState() => MyWalletState(); } diff --git a/pubspec.lock b/pubspec.lock index 376b9d0..a6dbebd 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -227,7 +227,7 @@ packages: source: hosted version: "1.4.4+1" http: - dependency: transitive + dependency: "direct main" description: name: http url: "https://pub.dartlang.org" diff --git a/pubspec.yaml b/pubspec.yaml index f5eced2..7be3479 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -26,6 +26,7 @@ dependencies: sentry_flutter: ^4.0.1 path_provider: ^1.6.24 pin_code_fields: ^6.0.2 + http: ^0.12.2 flutter_icons: @@ -44,3 +45,4 @@ flutter: assets: - images/ + - config/ \ No newline at end of file