From 8a45cb4dc7f2abe03c06cd6c2e48d0cc2ea88f58 Mon Sep 17 00:00:00 2001 From: OmarHatem Date: Wed, 8 Feb 2023 20:16:12 +0200 Subject: [PATCH] - Add Linter rules for return types and final fields - Enhance exception_handler code - Add ShareUtil to unify modification point --- analysis_options.yaml | 2 ++ .../dashboard/widgets/address_page.dart | 13 +++------ .../exchange_trade/exchange_trade_page.dart | 2 +- lib/src/screens/receive/receive_page.dart | 12 +++----- lib/src/screens/seed/wallet_seed_page.dart | 11 +++---- lib/utils/exception_handler.dart | 29 ++++++++++--------- lib/utils/share_util.dart | 13 +++++++++ 7 files changed, 44 insertions(+), 38 deletions(-) create mode 100644 lib/utils/share_util.dart diff --git a/analysis_options.yaml b/analysis_options.yaml index 524f70011..396904041 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -18,6 +18,8 @@ analyzer: linter: rules: - cancel_subscriptions + - always_declare_return_types + - prefer_final_fields # analyzer: diff --git a/lib/src/screens/dashboard/widgets/address_page.dart b/lib/src/screens/dashboard/widgets/address_page.dart index 92528d0d3..9a9a2c070 100644 --- a/lib/src/screens/dashboard/widgets/address_page.dart +++ b/lib/src/screens/dashboard/widgets/address_page.dart @@ -1,10 +1,8 @@ import 'package:cake_wallet/src/screens/base_page.dart'; -import 'package:cake_wallet/src/widgets/alert_with_one_action.dart'; import 'package:cake_wallet/src/widgets/alert_with_two_actions.dart'; import 'package:cake_wallet/src/widgets/keyboard_done_button.dart'; -import 'package:cake_wallet/src/widgets/primary_button.dart'; -import 'package:cake_wallet/store/settings_store.dart'; import 'package:cake_wallet/themes/theme_base.dart'; +import 'package:cake_wallet/utils/share_util.dart'; import 'package:cake_wallet/utils/show_pop_up.dart'; import 'package:cake_wallet/view_model/dashboard/dashboard_view_model.dart'; import 'package:flutter/material.dart'; @@ -15,7 +13,6 @@ import 'package:cake_wallet/generated/i18n.dart'; import 'package:flutter_mobx/flutter_mobx.dart'; import 'package:keyboard_actions/keyboard_actions.dart'; import 'package:mobx/mobx.dart'; -import 'package:share_plus/share_plus.dart'; class AddressPage extends BasePage { AddressPage({ @@ -101,11 +98,9 @@ class AddressPage extends BasePage { splashColor: Colors.transparent, iconSize: 25, onPressed: () { - final box = context.findRenderObject() as RenderBox?; - - Share.share( - addressListViewModel.address.address, - sharePositionOrigin: box!.localToGlobal(Offset.zero) & box.size, + ShareUtil.share( + text: addressListViewModel.address.address, + context: context, ); }, icon: shareImage, diff --git a/lib/src/screens/exchange_trade/exchange_trade_page.dart b/lib/src/screens/exchange_trade/exchange_trade_page.dart index f56ae6b2a..9eb17c762 100644 --- a/lib/src/screens/exchange_trade/exchange_trade_page.dart +++ b/lib/src/screens/exchange_trade/exchange_trade_page.dart @@ -305,7 +305,7 @@ class ExchangeTradeState extends State { _effectsInstalled = true; } - transactionStatePopup() { + void transactionStatePopup() { showPopUp( context: context, builder: (BuildContext popupContext) { diff --git a/lib/src/screens/receive/receive_page.dart b/lib/src/screens/receive/receive_page.dart index aeef99341..4a573b2e1 100644 --- a/lib/src/screens/receive/receive_page.dart +++ b/lib/src/screens/receive/receive_page.dart @@ -1,13 +1,11 @@ import 'package:cake_wallet/src/widgets/keyboard_done_button.dart'; import 'package:cake_wallet/src/widgets/section_divider.dart'; import 'package:cake_wallet/themes/theme_base.dart'; +import 'package:cake_wallet/utils/share_util.dart'; import 'package:cake_wallet/utils/show_pop_up.dart'; -import 'package:cake_wallet/view_model/dashboard/dashboard_view_model.dart'; import 'package:cw_core/wallet_type.dart'; import 'package:flutter/material.dart'; -import 'package:flutter/cupertino.dart'; import 'package:flutter_mobx/flutter_mobx.dart'; -import 'package:share_plus/share_plus.dart'; import 'package:cake_wallet/routes.dart'; import 'package:cake_wallet/generated/i18n.dart'; import 'package:cake_wallet/di.dart'; @@ -102,11 +100,9 @@ class ReceivePage extends BasePage { splashColor: Colors.transparent, iconSize: 25, onPressed: () { - final box = context.findRenderObject() as RenderBox?; - - Share.share( - addressListViewModel.address.address, - sharePositionOrigin: box!.localToGlobal(Offset.zero) & box.size, + ShareUtil.share( + text: addressListViewModel.address.address, + context: context, ); }, icon: shareImage diff --git a/lib/src/screens/seed/wallet_seed_page.dart b/lib/src/screens/seed/wallet_seed_page.dart index 13181cecf..64895db36 100644 --- a/lib/src/screens/seed/wallet_seed_page.dart +++ b/lib/src/screens/seed/wallet_seed_page.dart @@ -1,12 +1,11 @@ import 'package:cake_wallet/palette.dart'; import 'package:cake_wallet/themes/theme_base.dart'; import 'package:cake_wallet/src/widgets/alert_with_two_actions.dart'; +import 'package:cake_wallet/utils/share_util.dart'; import 'package:cake_wallet/utils/show_bar.dart'; import 'package:cake_wallet/utils/show_pop_up.dart'; -import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; -import 'package:share_plus/share_plus.dart'; import 'package:flutter_mobx/flutter_mobx.dart'; import 'package:cake_wallet/generated/i18n.dart'; import 'package:cake_wallet/src/widgets/primary_button.dart'; @@ -160,11 +159,9 @@ class WalletSeedPage extends BasePage { padding: EdgeInsets.only(right: 8.0), child: PrimaryButton( onPressed: () { - final box = context.findRenderObject() as RenderBox?; - - Share.share( - walletSeedViewModel.seed, - sharePositionOrigin: box!.localToGlobal(Offset.zero) & box.size, + ShareUtil.share( + text: walletSeedViewModel.seed, + context: context, ); }, text: S.of(context).save, diff --git a/lib/utils/exception_handler.dart b/lib/utils/exception_handler.dart index a4df85a7b..5fb50dabf 100644 --- a/lib/utils/exception_handler.dart +++ b/lib/utils/exception_handler.dart @@ -14,6 +14,7 @@ import 'package:shared_preferences/shared_preferences.dart'; class ExceptionHandler { static bool _hasError = false; + static const coolDownDurationInDays = 7; static void _saveException(String? error, StackTrace? stackTrace) async { final appDocDir = await getApplicationDocumentsDirectory(); @@ -77,12 +78,11 @@ class ExceptionHandler { final lastPopupDate = DateTime.tryParse(sharedPrefs.getString(PreferencesKey.lastPopupDate) ?? '') ?? - DateTime.parse("2001-01-01"); + DateTime.now().subtract(Duration(days: coolDownDurationInDays + 1)); final durationSinceLastReport = DateTime.now().difference(lastPopupDate).inDays; - // cool-down duration to be 7 days between reports - if (_hasError || durationSinceLastReport < 7) { + if (_hasError || durationSinceLastReport < coolDownDurationInDays) { return; } _hasError = true; @@ -117,14 +117,17 @@ class ExceptionHandler { } /// Ignore User related errors or system errors - static bool _ignoreError(String error) { - return error.contains("errno = 103") || // SocketException: Software caused connection abort - error.contains("errno = 9") || // SocketException: Bad file descriptor - error.contains("errno = 32") || // SocketException: Write failed (OS Error: Broken pipe) - error.contains("errno = 60") || // SocketException: Operation timed out - error.contains("errno = 54") || // SocketException: Connection reset by peer - error.contains("errno = 49") || // SocketException: Can't assign requested address - error.contains("PERMISSION_NOT_GRANTED") || - error.contains("errno = 28"); // OS Error: No space left on device - } + static bool _ignoreError(String error) => + _ignoredErrors.any((element) => error.contains(element)); + + static const List _ignoredErrors = const [ + "errno = 103", // SocketException: Software caused connection abort + "errno = 9", // SocketException: Bad file descriptor + "errno = 32", // SocketException: Write failed (OS Error: Broken pipe) + "errno = 60", // SocketException: Operation timed out + "errno = 54", // SocketException: Connection reset by peer + "errno = 49", // SocketException: Can't assign requested address + "errno = 28", // OS Error: No space left on device + "PERMISSION_NOT_GRANTED", + ]; } diff --git a/lib/utils/share_util.dart b/lib/utils/share_util.dart new file mode 100644 index 000000000..5b33399c7 --- /dev/null +++ b/lib/utils/share_util.dart @@ -0,0 +1,13 @@ +import 'package:flutter/material.dart'; +import 'package:share_plus/share_plus.dart'; + +class ShareUtil { + static void share({required String text, required BuildContext context}) { + final box = context.findRenderObject() as RenderBox?; + + Share.share( + text, + sharePositionOrigin: box!.localToGlobal(Offset.zero) & box.size, + ); + } +} \ No newline at end of file