From db5358ace8db8ff47ceab73f89ac840a43157f02 Mon Sep 17 00:00:00 2001 From: poka Date: Fri, 24 Nov 2023 20:42:28 +0100 Subject: [PATCH] refacto: replace huge switch cases by 2 maps in screen transaction_in_proggress --- assets/translations/en.json | 3 +- assets/translations/es.json | 3 +- assets/translations/fr.json | 3 +- lib/screens/transaction_in_progress.dart | 164 +++++++---------------- 4 files changed, 53 insertions(+), 120 deletions(-) diff --git a/assets/translations/en.json b/assets/translations/en.json index 054ae34..f49029c 100644 --- a/assets/translations/en.json +++ b/assets/translations/en.json @@ -226,5 +226,6 @@ "feesExplanationDetails": "These fees are transferred to the common treasury account.", "gotit": "Got it", "moreInfo": "More information", - "keepThisPaperSafe": "Keep this sheet safe from prying lizards.\nIt will allow you to restore all your wallets at any time." + "keepThisPaperSafe": "Keep this sheet safe from prying lizards.\nIt will allow you to restore all your wallets at any time.", + "fundsUnavailable": "Insufficient funds" } \ No newline at end of file diff --git a/assets/translations/es.json b/assets/translations/es.json index 81b0905..bb7a1d4 100644 --- a/assets/translations/es.json +++ b/assets/translations/es.json @@ -227,5 +227,6 @@ "feesExplanationDetails": "These fees are transferred to the common treasury account.", "gotit": "Got it", "moreInfo": "More information", - "keepThisPaperSafe": "Keep this sheet safe from prying lizards.\nIt will allow you to restore all your wallets at any time." + "keepThisPaperSafe": "Keep this sheet safe from prying lizards.\nIt will allow you to restore all your wallets at any time.", + "fundsUnavailable": "Insufficient funds" } \ No newline at end of file diff --git a/assets/translations/fr.json b/assets/translations/fr.json index 105224f..445e827 100644 --- a/assets/translations/fr.json +++ b/assets/translations/fr.json @@ -226,5 +226,6 @@ "feesExplanationDetails": "Ces frais sont transférés vers le compte de trésorerie commune.", "gotit": "J'ai compris", "moreInfo": "Plus d'info", - "keepThisPaperSafe": "Gardez cette feuille précieusement, à l’abri des lézards indiscrets.\nElle vous permettra de restaurer tous vos portefeuilles à tout moment." + "keepThisPaperSafe": "Gardez cette feuille précieusement, à l’abri des lézards indiscrets.\nElle vous permettra de restaurer tous vos portefeuilles à tout moment.", + "fundsUnavailable": "Fonds insuffisants" } \ No newline at end of file diff --git a/lib/screens/transaction_in_progress.dart b/lib/screens/transaction_in_progress.dart index c2f801a..432e159 100644 --- a/lib/screens/transaction_in_progress.dart +++ b/lib/screens/transaction_in_progress.dart @@ -1,5 +1,4 @@ import 'package:easy_localization/easy_localization.dart'; - import 'package:gecko/globals.dart'; import 'package:flutter/material.dart'; import 'package:gecko/models/widgets_keys.dart'; @@ -7,8 +6,6 @@ import 'package:gecko/providers/my_wallets.dart'; import 'package:gecko/providers/substrate_sdk.dart'; import 'package:gecko/providers/wallets_profiles.dart'; import 'package:provider/provider.dart'; -// import 'package:gecko/models/home.dart'; -// import 'package:provider/provider.dart'; class TransactionInProgress extends StatelessWidget { const TransactionInProgress( @@ -32,12 +29,9 @@ class TransactionInProgress extends StatelessWidget { Provider.of(context, listen: false); bool isValid = false; - String resultText; bool isLoading = true; final result = sub.transactionStatus; - log.d(walletProfiles.address); - final from = fromAddress ?? g1WalletsBox .get(myWalletProvider.getDefaultWallet().address) @@ -48,121 +42,55 @@ class TransactionInProgress extends StatelessWidget { myWalletProvider.getWalletDataByAddress(to)?.name ?? getShortPubkey(to); final amount = walletProfiles.payAmount.text; - String actionName = ''; final bool isUdUnit = configBox.get('isUdUnit') ?? false; - log.d("$transType :: $actionName :: $result"); + final Map actionMap = { + 'pay': 'transaction'.tr(), + 'cert': 'certification'.tr(), + 'comfirmIdty': 'identityConfirm'.tr(), + 'revokeIdty': 'revokeAdhesion'.tr(), + 'identityMigration': 'identityMigration'.tr(), + }; + String resultText = ''; + final Map resultMap = { + '': 'sending'.tr(), + 'Ready': 'propagating'.tr(), + 'Broadcast': 'validating'.tr(), + 'cert.NotRespectCertPeriod': '24hbetweenCerts'.tr(), + 'identity.CreatorNotAllowedToCreateIdty': '24hbetweenCerts'.tr(), + 'cert.CannotCertifySelf': 'canNotCertifySelf'.tr(), + 'identity.IdtyNameAlreadyExist': 'nameAlreadyExist'.tr(), + 'balances.KeepAlive': '2GDtoKeepAlive'.tr(), + '1010: Invalid Transaction: Inability to pay some fees , e.g. account balance too low': + 'youHaveToFeedThisAccountBeforeUsing'.tr(), + 'Token.FundsUnavailable': 'fundsUnavailable'.tr(), + 'timeout': 'execTimeoutOver'.tr(), + }; - switch (transType) { - case 'pay': - { - actionName = 'transaction'.tr(); - } - break; - case 'cert': - { - actionName = 'certification'.tr(); - } - break; - case 'comfirmIdty': - { - actionName = "identityConfirm".tr(); - } - break; - case 'revokeIdty': - { - actionName = "revokeAdhesion".tr(); - } - break; - case 'identityMigration': - { - actionName = "identityMigration".tr(); - } - break; - default: - { - actionName = 'strangeTransaction'.tr(); - } + if (result.contains('blockHash: ')) { + isLoading = false; + isValid = true; + resultText = 'extrinsicValidated' + .tr(args: [actionMap[transType] ?? 'strangeTransaction'.tr()]); + log.i('Bloc of last transaction: ${sub.blocNumber} --- $result'); + } else if (result.contains('Exception: ')) { + isLoading = false; + isValid = false; + resultText = "${"anErrorOccurred".tr()}:\n"; + final List exceptionSplit = result.split('Exception: '); + String exception; + if (exceptionSplit.length > 1) { + exception = exceptionSplit[1]; + } else { + exception = exceptionSplit[0]; + } + resultText = resultMap[exception] ?? "$resultText\n$exception"; + log.d('expection: $exceptionSplit'); + } else { + resultText = resultMap[result] ?? 'unknown status...'; } - switch (result) { - case '': - { - resultText = 'sending'.tr(); - } - break; - case 'Ready': - { - resultText = 'propagating'.tr(); - } - break; - case 'Broadcast': - { - resultText = 'validating'.tr(); - } - break; - default: - { - isLoading = false; - // jsonResult = json.decode(_result); - if (result.contains('blockHash: ')) { - isValid = true; - resultText = 'extrinsicValidated'.tr(args: [actionName]); - log.i( - 'g1migration Bloc of last transaction: ${sub.blocNumber} --- $result'); - } else { - isValid = false; - resultText = "${"anErrorOccurred".tr()}:\n"; - final List exceptionSplit = result.split('Exception: '); - String exception; - if (exceptionSplit.length > 1) { - exception = exceptionSplit[1]; - } else { - exception = exceptionSplit[0]; - } - log.d('expection: $exceptionSplit'); - switch (exception) { - case 'cert.NotRespectCertPeriod': - case 'identity.CreatorNotAllowedToCreateIdty': - { - resultText = "24hbetweenCerts".tr(); - } - break; - case 'cert.CannotCertifySelf': - { - resultText = "canNotCertifySelf".tr(); - } - break; - case 'identity.IdtyNameAlreadyExist': - { - resultText = "nameAlreadyExist".tr(); - } - break; - case 'balances.KeepAlive': - { - resultText = "2GDtoKeepAlive".tr(); - } - break; - case '1010: Invalid Transaction: Inability to pay some fees , e.g. account balance too low': - { - resultText = "youHaveToFeedThisAccountBeforeUsing".tr(); - } - break; - - case 'timeout': - { - resultText += "execTimeoutOver".tr(); - } - break; - default: - { - resultText += "\n$exception"; - } - break; - } - } - } - } + log.d("$transType :: ${actionMap[transType]} :: $result"); return WillPopScope( onWillPop: () { @@ -184,7 +112,9 @@ class TransactionInProgress extends StatelessWidget { child: Row( mainAxisAlignment: MainAxisAlignment.center, children: [ - Text('extrinsicInProgress'.tr(args: [actionName])) + Text('extrinsicInProgress'.tr(args: [ + actionMap[transType] ?? 'strangeTransaction'.tr() + ])) ]), )), body: SafeArea(