From 7f83965a8b1b160711aaf1144d9235cfa2f139a0 Mon Sep 17 00:00:00 2001 From: poka Date: Mon, 13 Jun 2022 21:58:57 +0200 Subject: [PATCH] Indexer client implemented; Display identity name when exist --- lib/main.dart | 89 +++++++------ lib/models/queries_indexer.dart | 9 ++ lib/providers/wallet_options.dart | 146 +++++++++++++++++++++- lib/screens/myWallets/wallet_options.dart | 71 ++--------- lib/screens/myWallets/wallets_home.dart | 32 +++-- 5 files changed, 238 insertions(+), 109 deletions(-) create mode 100644 lib/models/queries_indexer.dart diff --git a/lib/main.dart b/lib/main.dart index 586c971..63c9be4 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -43,6 +43,7 @@ import 'package:responsive_framework/responsive_framework.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:sentry_flutter/sentry_flutter.dart'; import 'package:window_size/window_size.dart'; +import 'package:graphql_flutter/graphql_flutter.dart'; const bool enableSentry = true; @@ -79,6 +80,8 @@ Future main() async { } // log.d(await configBox.get('endpoint')); + const indexerEndpoint = "http://192.168.1.72:8080/v1/graphql"; + HttpOverrides.global = MyHttpOverrides(); if (kReleaseMode && enableSentry) { @@ -95,7 +98,7 @@ Future main() async { await SentryFlutter.init((options) { options.dsn = 'https://c09587b46eaa42e8b9fda28d838ed180@o496840.ingest.sentry.io/5572110'; - }, appRunner: () => runApp(const Gecko())); + }, appRunner: () => runApp(const Gecko(indexerEndpoint))); // runZoned>( // () async { @@ -112,16 +115,27 @@ Future main() async { } else { print('Debug mode enabled: No sentry alerte'); - runApp(const Gecko()); + runApp(const Gecko(indexerEndpoint)); } } class Gecko extends StatelessWidget { - const Gecko({Key? key}) : super(key: key); + const Gecko(this.indexerEndpoint, {Key? key}) : super(key: key); + final String? indexerEndpoint; @override Widget build(BuildContext context) { SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]); + final _httpLink = HttpLink( + indexerEndpoint!, + ); + + final _client = ValueNotifier( + GraphQLClient( + cache: GraphQLCache(), + link: _httpLink, + ), + ); return MultiProvider( providers: [ @@ -136,41 +150,44 @@ class Gecko extends StatelessWidget { ChangeNotifierProvider(create: (_) => CesiumPlusProvider()), ChangeNotifierProvider(create: (_) => SubstrateSdk()) ], - child: MaterialApp( - builder: (context, widget) => ResponsiveWrapper.builder( - BouncingScrollWrapper.builder(context, widget!), - maxWidth: 1200, - minWidth: 480, - defaultScale: true, - breakpoints: [ - const ResponsiveBreakpoint.resize(480, name: MOBILE), - const ResponsiveBreakpoint.autoScale(800, name: TABLET), - const ResponsiveBreakpoint.resize(1000, name: DESKTOP), - ], - background: Container(color: backgroundColor)), - title: 'Ğecko', - theme: ThemeData( - appBarTheme: const AppBarTheme( - color: Color(0xffFFD58D), - foregroundColor: Color(0xFF000000), + child: GraphQLProvider( + client: _client, + child: MaterialApp( + builder: (context, widget) => ResponsiveWrapper.builder( + BouncingScrollWrapper.builder(context, widget!), + maxWidth: 1200, + minWidth: 480, + defaultScale: true, + breakpoints: [ + const ResponsiveBreakpoint.resize(480, name: MOBILE), + const ResponsiveBreakpoint.autoScale(800, name: TABLET), + const ResponsiveBreakpoint.resize(1000, name: DESKTOP), + ], + background: Container(color: backgroundColor)), + title: 'Ğecko', + theme: ThemeData( + appBarTheme: const AppBarTheme( + color: Color(0xffFFD58D), + foregroundColor: Color(0xFF000000), + ), + primaryColor: const Color(0xffFFD58D), + textTheme: const TextTheme( + bodyText1: TextStyle(fontSize: 16), + bodyText2: TextStyle(fontSize: 18), + ).apply( + bodyColor: const Color(0xFF000000), + ), + colorScheme: + ColorScheme.fromSwatch().copyWith(secondary: Colors.grey[850]), ), - primaryColor: const Color(0xffFFD58D), - textTheme: const TextTheme( - bodyText1: TextStyle(fontSize: 16), - bodyText2: TextStyle(fontSize: 18), - ).apply( - bodyColor: const Color(0xFF000000), - ), - colorScheme: - ColorScheme.fromSwatch().copyWith(secondary: Colors.grey[850]), + home: const HomeScreen(), + initialRoute: "/", + routes: { + '/mywallets': (context) => const WalletsHome(), + '/search': (context) => const SearchScreen(), + '/searchResult': (context) => const SearchResultScreen(), + }, ), - home: const HomeScreen(), - initialRoute: "/", - routes: { - '/mywallets': (context) => const WalletsHome(), - '/search': (context) => const SearchScreen(), - '/searchResult': (context) => const SearchResultScreen(), - }, ), ); } diff --git a/lib/models/queries_indexer.dart b/lib/models/queries_indexer.dart new file mode 100644 index 0000000..439cd86 --- /dev/null +++ b/lib/models/queries_indexer.dart @@ -0,0 +1,9 @@ +const String getNameByAddressQ = r''' +query ($address: String!) { + account_by_pk(id: $address) { + identity { + name + } + } +} +'''; diff --git a/lib/providers/wallet_options.dart b/lib/providers/wallet_options.dart index 3877072..5474836 100644 --- a/lib/providers/wallet_options.dart +++ b/lib/providers/wallet_options.dart @@ -2,6 +2,7 @@ import 'dart:io'; import 'package:flutter/material.dart'; import 'dart:async'; import 'package:gecko/globals.dart'; +import 'package:gecko/models/queries_indexer.dart'; import 'package:gecko/providers/my_wallets.dart'; import 'package:gecko/models/wallet_data.dart'; import 'package:gecko/providers/substrate_sdk.dart'; @@ -9,6 +10,7 @@ import 'package:gecko/screens/animated_text.dart'; import 'package:gecko/screens/common_elements.dart'; import 'package:gecko/screens/myWallets/unlocking_wallet.dart'; import 'package:gecko/screens/transaction_in_progress.dart'; +import 'package:graphql_flutter/graphql_flutter.dart'; import 'package:image_picker/image_picker.dart'; import 'package:provider/provider.dart'; import 'package:image_cropper/image_cropper.dart'; @@ -25,6 +27,7 @@ class WalletOptionsProvider with ChangeNotifier { TextEditingController nameController = TextEditingController(); late bool isDefaultWallet; bool canValidateNameBool = false; + Map walletNameIndexer = {}; Future? get badWallet => null; @@ -170,12 +173,18 @@ class WalletOptionsProvider with ChangeNotifier { } case 'ConfirmedByOwner': { - return _showText('Identité confirmé'); + return isOwner + ? _showText('Identité confirmé') + : getNameByAddress(context, address, null, 20, true, + Colors.grey[700]!, FontWeight.w500, FontStyle.italic); } case 'Validated': { - return _showText('Membre validé !', 18, true); + return isOwner + ? _showText('Membre validé !', 18, true) + : getNameByAddress(context, address, null, 20, true, + Colors.black, FontWeight.w600, FontStyle.normal); } case 'expired': @@ -420,6 +429,139 @@ class WalletOptionsProvider with ChangeNotifier { return _address; } + + Widget getNameByAddress(BuildContext context, String address, + [WalletData? wallet, + double size = 20, + bool canEdit = false, + Color _color = Colors.black, + FontWeight fontWeight = FontWeight.w400, + FontStyle fontStyle = FontStyle.italic]) { + return Query( + options: QueryOptions( + document: gql( + getNameByAddressQ), // this is the query string you just created + variables: { + 'address': address, + }, + // pollInterval: const Duration(seconds: 10), + ), + builder: (QueryResult result, + {VoidCallback? refetch, FetchMore? fetchMore}) { + if (result.hasException) { + return Text(result.exception.toString()); + } + + if (result.isLoading) { + return const Text('Loading'); + } + + walletNameIndexer[address] = + result.data?['account_by_pk']?['identity']?['name']; + + if (walletNameIndexer[address] == null) { + if (wallet == null) { + return const SizedBox(); + } else { + if (canEdit) { + return walletName(context, wallet, size, _color); + } else { + return walletNameController(context, wallet, size); + } + } + } + + return Text( + _color == Colors.grey[700]! + ? '(${walletNameIndexer[address]!})' + : walletNameIndexer[address]!, + style: TextStyle( + fontSize: size, + color: _color, + fontWeight: fontWeight, + fontStyle: fontStyle, + ), + ); + }); + } + + Widget walletNameController(BuildContext context, WalletData wallet, + [double size = 20]) { + // WidgetsBinding.instance.addPostFrameCallback((_) { + log.d('aaaaaaaaaaaaaaaaaaaaa: ${wallet.name}'); + nameController.text = wallet.name!; + // _walletOptions.reloadBuild(); + // }); + + return SizedBox( + width: 260, + child: Stack(children: [ + TextField( + key: const Key('walletName'), + autofocus: false, + focusNode: walletNameFocus, + enabled: isEditing, + controller: nameController, + minLines: 1, + maxLines: 3, + textAlign: TextAlign.center, + decoration: const InputDecoration( + border: InputBorder.none, + focusedBorder: InputBorder.none, + enabledBorder: InputBorder.none, + disabledBorder: InputBorder.none, + contentPadding: EdgeInsets.all(15.0), + ), + style: TextStyle( + fontSize: isTall ? size : size * 0.9, + color: Colors.black, + fontWeight: FontWeight.w400, + ), + ), + Positioned( + right: 0, + child: InkWell( + key: const Key('renameWallet'), + onTap: () async { + // _isNewNameValid = + // walletProvider.editWalletName(wallet.id(), isCesium: false); + await editWalletName(context, wallet.id()); + await Future.delayed(const Duration(milliseconds: 30)); + walletNameFocus.requestFocus(); + }, + child: ClipRRect( + child: Image.asset( + isEditing + ? 'assets/walletOptions/android-checkmark.png' + : 'assets/walletOptions/edit.png', + width: 25, + height: 25), + ), + ), + ), + ]), + ); + } + + Widget walletName(BuildContext context, WalletData wallet, + [double size = 20, Color color = Colors.black]) { + return SizedBox( + width: 260, + child: + Row(mainAxisAlignment: MainAxisAlignment.center, children: [ + Text( + wallet.name!, + textAlign: TextAlign.center, + style: TextStyle( + fontSize: isTall ? size : size * 0.9, + color: color, + fontWeight: FontWeight.w400, + fontStyle: FontStyle.italic, + ), + ), + ]), + ); + } } Map balanceCache = {}; diff --git a/lib/screens/myWallets/wallet_options.dart b/lib/screens/myWallets/wallet_options.dart index 41f878a..0800784 100644 --- a/lib/screens/myWallets/wallet_options.dart +++ b/lib/screens/myWallets/wallet_options.dart @@ -100,10 +100,18 @@ class WalletOptions extends StatelessWidget { Column( crossAxisAlignment: CrossAxisAlignment.center, children: [ - walletName( - context, walletProvider, _walletOptions), - SizedBox(height: isTall ? 5 : 0), + _walletOptions.getNameByAddress( + context, + walletProvider.address.text, + wallet, + 27, + false, + Colors.black, + FontWeight.w400, + FontStyle.normal), // SizedBox(height: isTall ? 5 : 0), + + SizedBox(height: isTall ? 5 : 0), balance( context, walletProvider.address.text, 21), const SizedBox(width: 30), @@ -247,63 +255,6 @@ class WalletOptions extends StatelessWidget { ); } - Widget walletName(BuildContext context, WalletOptionsProvider walletProvider, - WalletOptionsProvider _walletOptions) { - WidgetsBinding.instance.addPostFrameCallback((_) { - _walletOptions.nameController.text = wallet.name!; - // _walletOptions.reloadBuild(); - }); - - return SizedBox( - width: 260, - child: Stack(children: [ - TextField( - key: const Key('walletName'), - autofocus: false, - focusNode: walletProvider.walletNameFocus, - enabled: walletProvider.isEditing, - controller: walletProvider.nameController, - minLines: 1, - maxLines: 3, - textAlign: TextAlign.center, - decoration: const InputDecoration( - border: InputBorder.none, - focusedBorder: InputBorder.none, - enabledBorder: InputBorder.none, - disabledBorder: InputBorder.none, - contentPadding: EdgeInsets.all(15.0), - ), - style: TextStyle( - fontSize: isTall ? 27 : 23, - color: Colors.black, - fontWeight: FontWeight.w400, - ), - ), - Positioned( - right: 0, - child: InkWell( - key: const Key('renameWallet'), - onTap: () async { - // _isNewNameValid = - // walletProvider.editWalletName(wallet.id(), isCesium: false); - await walletProvider.editWalletName(context, wallet.id()); - await Future.delayed(const Duration(milliseconds: 30)); - walletProvider.walletNameFocus.requestFocus(); - }, - child: ClipRRect( - child: Image.asset( - walletProvider.isEditing - ? 'assets/walletOptions/android-checkmark.png' - : 'assets/walletOptions/edit.png', - width: 25, - height: 25), - ), - ), - ), - ]), - ); - } - Widget pubkeyWidget(WalletOptionsProvider walletProvider, BuildContext ctx) { final String shortPubkey = getShortPubkey(walletProvider.address.text); return GestureDetector( diff --git a/lib/screens/myWallets/wallets_home.dart b/lib/screens/myWallets/wallets_home.dart index 9f7f304..80b8344 100644 --- a/lib/screens/myWallets/wallets_home.dart +++ b/lib/screens/myWallets/wallets_home.dart @@ -263,17 +263,27 @@ class WalletsHome extends StatelessWidget { child: Padding( padding: const EdgeInsets.symmetric(horizontal: 5), - child: Text( - _repository.name!, - textAlign: TextAlign.center, - style: TextStyle( - fontSize: 17.0, - color: _repository.id()[1] == - defaultWallet.id()[1] - ? const Color(0xffF9F9F1) - : Colors.black, - fontStyle: FontStyle.italic), - ), + child: _walletOptions.getNameByAddress( + context, + _repository.address!, + _repository, + 17, + true, + _repository.id()[1] == defaultWallet.id()[1] + ? const Color(0xffF9F9F1) + : Colors.black), + + // Text( + // _repository.name!, + // textAlign: TextAlign.center, + // style: TextStyle( + // fontSize: 17.0, + // color: _repository.id()[1] == + // defaultWallet.id()[1] + // ? const Color(0xffF9F9F1) + // : Colors.black, + // fontStyle: FontStyle.italic), + // ), ), ), // dense: true,