diff --git a/lib/pages_desktop_specific/settings/settings_menu/advanced_settings/desktop_manage_block_explorers_dialog.dart b/lib/pages_desktop_specific/settings/settings_menu/advanced_settings/desktop_manage_block_explorers_dialog.dart index 41e65e9d76..5f55b292de 100644 --- a/lib/pages_desktop_specific/settings/settings_menu/advanced_settings/desktop_manage_block_explorers_dialog.dart +++ b/lib/pages_desktop_specific/settings/settings_menu/advanced_settings/desktop_manage_block_explorers_dialog.dart @@ -30,9 +30,7 @@ import '../../../../widgets/desktop/secondary_button.dart'; import '../../../../widgets/rounded_white_container.dart'; class DesktopManageBlockExplorersDialog extends ConsumerWidget { - const DesktopManageBlockExplorersDialog({ - super.key, - }); + const DesktopManageBlockExplorersDialog({super.key}); @override Widget build(BuildContext context, WidgetRef ref) { @@ -41,9 +39,8 @@ class DesktopManageBlockExplorersDialog extends ConsumerWidget { ); final coins = AppConfig.coins - .where( - (e) => showTestNet || e.network == CryptoCurrencyNetwork.main, - ) + .where((e) => showTestNet || e.network == CryptoCurrencyNetwork.main) + .where(hasDefaultBlockExplorer) .toList(); return DesktopDialog( @@ -67,16 +64,10 @@ class DesktopManageBlockExplorersDialog extends ConsumerWidget { ), Expanded( child: Padding( - padding: const EdgeInsets.only( - left: 32, - right: 32, - bottom: 32, - ), + padding: const EdgeInsets.only(left: 32, right: 32, bottom: 32), child: ListView.separated( itemCount: coins.length, - separatorBuilder: (_, __) => const SizedBox( - height: 12, - ), + separatorBuilder: (_, __) => const SizedBox(height: 12), itemBuilder: (_, index) { final coin = coins[index]; @@ -91,24 +82,18 @@ class DesktopManageBlockExplorersDialog extends ConsumerWidget { child: Row( children: [ SvgPicture.file( - File( - ref.watch(coinIconProvider(coin)), - ), + File(ref.watch(coinIconProvider(coin))), width: 24, height: 24, ), - const SizedBox( - width: 12, - ), + const SizedBox(width: 12), Expanded( child: Text( "${coin.prettyName} block explorer", style: STextStyles.desktopTextSmall(context), ), ), - const SizedBox( - width: 12, - ), + const SizedBox(width: 12), SvgPicture.asset( Assets.svg.chevronRight, width: 20, @@ -122,9 +107,7 @@ class DesktopManageBlockExplorersDialog extends ConsumerWidget { useSafeArea: false, barrierDismissible: true, builder: (context) { - return _DesktopEditBlockExplorerDialog( - coin: coin, - ); + return _DesktopEditBlockExplorerDialog(coin: coin); }, ); }, @@ -157,9 +140,10 @@ class _DesktopEditBlockExplorerDialogState @override void initState() { _textEditingController = TextEditingController( - text: getBlockExplorerTransactionUrlFor(coin: widget.coin, txid: "[TXID]") - .toString() - .replaceAll("%5BTXID%5D", "[TXID]"), + text: getBlockExplorerTransactionUrlFor( + coin: widget.coin, + txid: "[TXID]", + ).toString().replaceAll("%5BTXID%5D", "[TXID]"), ); _focusNode = FocusNode(); super.initState(); @@ -194,11 +178,7 @@ class _DesktopEditBlockExplorerDialogState ], ), Padding( - padding: const EdgeInsets.only( - left: 32, - right: 32, - bottom: 32, - ), + padding: const EdgeInsets.only(left: 32, right: 32, bottom: 32), child: Column( mainAxisSize: MainAxisSize.min, children: [ @@ -215,12 +195,11 @@ class _DesktopEditBlockExplorerDialogState style: STextStyles.field(context), ), ), - const SizedBox( - height: 16, - ), + const SizedBox(height: 16), RoundedWhiteContainer( - borderColor: - Theme.of(context).extension()!.textSubtitle6, + borderColor: Theme.of(context) + .extension()! + .textSubtitle6, child: Text( "Edit your block explorer above. Keep in mind that" " every block explorer has a slightly different URL scheme." @@ -232,9 +211,7 @@ class _DesktopEditBlockExplorerDialogState style: STextStyles.desktopTextExtraExtraSmall(context), ), ), - const SizedBox( - height: 16, - ), + const SizedBox(height: 16), Row( children: [ Expanded( @@ -244,21 +221,18 @@ class _DesktopEditBlockExplorerDialogState onPressed: Navigator.of(context).pop, ), ), - const SizedBox( - width: 16, - ), + const SizedBox(width: 16), Expanded( child: PrimaryButton( label: "Save", buttonHeight: ButtonHeight.l, onPressed: () async { - _textEditingController.text = - _textEditingController.text.trim(); + _textEditingController.text = _textEditingController + .text + .trim(); await setBlockExplorerForCoin( coin: widget.coin, - url: Uri.parse( - _textEditingController.text, - ), + url: Uri.parse(_textEditingController.text), ); if (mounted) { diff --git a/lib/utilities/block_explorers.dart b/lib/utilities/block_explorers.dart index b7f55b4b4e..bf7f316255 100644 --- a/lib/utilities/block_explorers.dart +++ b/lib/utilities/block_explorers.dart @@ -18,10 +18,7 @@ Future setBlockExplorerForCoin({ required Uri url, }) async { return await MainDB.instance.putTransactionBlockExplorer( - TransactionBlockExplorer( - ticker: coin.ticker, - url: url.toString(), - ), + TransactionBlockExplorer(ticker: coin.ticker, url: url.toString()), ); } @@ -30,8 +27,9 @@ Uri getBlockExplorerTransactionUrlFor({ required CryptoCurrency coin, required String txid, }) { - String? url = - MainDB.instance.getTransactionBlockExplorer(cryptoCurrency: coin)?.url; + String? url = MainDB.instance + .getTransactionBlockExplorer(cryptoCurrency: coin) + ?.url; if (url == null) { return coin.defaultBlockExplorer(txid); } else { @@ -39,3 +37,15 @@ Uri getBlockExplorerTransactionUrlFor({ return Uri.parse(url); } } + +/// Whether [coin] has a default block explorer. Coins without one throw from +/// [CryptoCurrency.defaultBlockExplorer], so they cannot be offered where an +/// explorer URL is shown or edited. +bool hasDefaultBlockExplorer(CryptoCurrency coin) { + try { + coin.defaultBlockExplorer("[TXID]"); + return true; + } catch (_) { + return false; + } +} diff --git a/lib/widgets/choose_coin_view.dart b/lib/widgets/choose_coin_view.dart index b44f7152a1..ba3cf45e70 100644 --- a/lib/widgets/choose_coin_view.dart +++ b/lib/widgets/choose_coin_view.dart @@ -13,10 +13,13 @@ import 'dart:io'; import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_svg/svg.dart'; + import '../providers/providers.dart'; import '../app_config.dart'; +import '../pages/settings_views/global_settings_view/advanced_views/manage_explorer_view.dart'; import '../themes/coin_icon_provider.dart'; import '../themes/stack_colors.dart'; +import '../utilities/block_explorers.dart'; import '../utilities/constants.dart'; import '../utilities/text_styles.dart'; import '../wallets/crypto_currency/crypto_currency.dart'; @@ -59,6 +62,9 @@ class _ChooseCoinViewState extends ConsumerState { _coins.removeWhere( (e) => e.identifier == Firo(CryptoCurrencyNetwork.test).identifier, ); + if (widget.nextRouteName == ManageExplorerView.routeName) { + _coins.removeWhere((e) => !hasDefaultBlockExplorer(e)); + } super.initState(); } @@ -75,11 +81,7 @@ class _ChooseCoinViewState extends ConsumerState { final List coins = showTestNet ? _coins - : _coins - .where( - (e) => e.network == CryptoCurrencyNetwork.main, - ) - .toList(); + : _coins.where((e) => e.network == CryptoCurrencyNetwork.main).toList(); return Background( child: Scaffold( @@ -90,73 +92,57 @@ class _ChooseCoinViewState extends ConsumerState { Navigator.of(context).pop(); }, ), - title: Text( - widget.title, - style: STextStyles.navBarTitle(context), - ), + title: Text(widget.title, style: STextStyles.navBarTitle(context)), ), body: Padding( - padding: const EdgeInsets.only( - top: 12, - left: 12, - right: 12, - ), + padding: const EdgeInsets.only(top: 12, left: 12, right: 12), child: SingleChildScrollView( child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ - ...coins.map( - (coin) { - return Padding( - padding: const EdgeInsets.all(4), - child: RoundedWhiteContainer( - padding: const EdgeInsets.all(0), - child: RawMaterialButton( - // splashColor: Theme.of(context).extension()!.highlight, - shape: RoundedRectangleBorder( - borderRadius: BorderRadius.circular( - Constants.size.circularBorderRadius, - ), + ...coins.map((coin) { + return Padding( + padding: const EdgeInsets.all(4), + child: RoundedWhiteContainer( + padding: const EdgeInsets.all(0), + child: RawMaterialButton( + // splashColor: Theme.of(context).extension()!.highlight, + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular( + Constants.size.circularBorderRadius, ), - materialTapTargetSize: - MaterialTapTargetSize.shrinkWrap, - onPressed: () { - Navigator.of(context).pushNamed( - widget.nextRouteName, - arguments: coin, - ); - }, - child: Padding( - padding: const EdgeInsets.all(12), - child: Row( - children: [ - SvgPicture.file( - File( - ref.watch(coinIconProvider(coin)), + ), + materialTapTargetSize: MaterialTapTargetSize.shrinkWrap, + onPressed: () { + Navigator.of(context) + .pushNamed(widget.nextRouteName, arguments: coin); + }, + child: Padding( + padding: const EdgeInsets.all(12), + child: Row( + children: [ + SvgPicture.file( + File(ref.watch(coinIconProvider(coin))), + width: 24, + height: 24, + ), + const SizedBox(width: 12), + Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + "${coin.prettyName} ${widget.coinAdditional}", + style: STextStyles.titleBold12(context), ), - width: 24, - height: 24, - ), - const SizedBox( - width: 12, - ), - Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Text( - "${coin.prettyName} ${widget.coinAdditional}", - style: STextStyles.titleBold12(context), - ), - ], - ), - ], - ), + ], + ), + ], ), ), ), - ); - }, - ), + ), + ); + }), ], ), ), diff --git a/test/pages/settings_views/block_explorer_coins_test.dart b/test/pages/settings_views/block_explorer_coins_test.dart new file mode 100644 index 0000000000..2b3f37578e --- /dev/null +++ b/test/pages/settings_views/block_explorer_coins_test.dart @@ -0,0 +1,104 @@ +import 'dart:io'; + +import 'package:flutter/material.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:stackwallet/app_config.dart'; +import 'package:stackwallet/models/isar/stack_theme.dart'; +import 'package:stackwallet/pages/settings_views/global_settings_view/advanced_views/manage_explorer_view.dart'; +import 'package:stackwallet/pages_desktop_specific/settings/settings_menu/advanced_settings/desktop_manage_block_explorers_dialog.dart'; +import 'package:stackwallet/providers/global/prefs_provider.dart'; +import 'package:stackwallet/themes/coin_icon_provider.dart'; +import 'package:stackwallet/themes/stack_colors.dart'; +import 'package:stackwallet/themes/theme_providers.dart'; +import 'package:stackwallet/utilities/block_explorers.dart'; +import 'package:stackwallet/utilities/prefs.dart'; +import 'package:stackwallet/utilities/stack_file_system.dart'; +import 'package:stackwallet/wallets/crypto_currency/crypto_currency.dart'; +import 'package:stackwallet/widgets/choose_coin_view.dart'; + +import '../../sample_data/theme_json.dart'; + +class _TestPrefs extends ChangeNotifier implements Prefs { + @override + bool get showTestNetCoins => false; + + @override + dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); +} + +void main() { + final epic = Epiccash(CryptoCurrencyNetwork.main); + final mwc = Mimblewimblecoin(CryptoCurrencyNetwork.main); + final bitcoin = Bitcoin(CryptoCurrencyNetwork.main); + + Future pump(WidgetTester tester, Widget home) async { + tester.view.physicalSize = const Size(1200, 6000); + tester.view.devicePixelRatio = 1; + addTearDown(tester.view.resetPhysicalSize); + addTearDown(tester.view.resetDevicePixelRatio); + final previousThemesDir = StackFileSystem.themesDir; + addTearDown(() => StackFileSystem.themesDir = previousThemesDir); + StackFileSystem.themesDir = Directory('test/sample_data').absolute; + final theme = StackTheme.fromJson(json: lightThemeJsonMap); + final icon = File('test/sample_data/light/assets/dummy.svg').absolute.path; + await tester.pumpWidget( + ProviderScope( + overrides: [ + prefsChangeNotifierProvider.overrideWithValue(_TestPrefs()), + themeProvider.overrideWithProvider(StateProvider((ref) => theme)), + coinIconProvider.overrideWithProvider((_) => Provider((_) => icon)), + ], + child: MaterialApp( + theme: ThemeData( + extensions: [StackColors.fromStackColorTheme(theme)], + ), + home: home, + ), + ), + ); + await tester.pump(); + expect(tester.takeException(), isNull); + } + + test('Epic Cash and MWC have no default block explorer', () { + expect(hasDefaultBlockExplorer(epic), isFalse); + expect(hasDefaultBlockExplorer(mwc), isFalse); + expect(hasDefaultBlockExplorer(bitcoin), isTrue); + }); + + test('the app offers Epic Cash, MWC and Bitcoin', () { + expect(AppConfig.coins, containsAll([epic, mwc, bitcoin])); + }); + + testWidgets('the mobile explorer list leaves out coins without an explorer', ( + tester, + ) async { + await pump( + tester, + const ChooseCoinView( + title: 'Manage block explorers', + coinAdditional: 'block explorer', + nextRouteName: ManageExplorerView.routeName, + ), + ); + + expect(find.text('${bitcoin.prettyName} block explorer'), findsOneWidget); + expect(find.text('${epic.prettyName} block explorer'), findsNothing); + expect(find.text('${mwc.prettyName} block explorer'), findsNothing); + }); + + testWidgets( + 'the desktop explorer list leaves out coins without an explorer', + (tester) async { + await pump( + tester, + const Scaffold(body: DesktopManageBlockExplorersDialog()), + ); + + expect(find.text('${bitcoin.prettyName} block explorer'), findsOneWidget); + expect(find.text('${epic.prettyName} block explorer'), findsNothing); + expect(find.text('${mwc.prettyName} block explorer'), findsNothing); + }, + ); +}