diff --git a/lib/pages/wallet_view/wallet_view.dart b/lib/pages/wallet_view/wallet_view.dart index e6a676bf3..eaea818e9 100644 --- a/lib/pages/wallet_view/wallet_view.dart +++ b/lib/pages/wallet_view/wallet_view.dart @@ -5,6 +5,8 @@ import 'package:event_bus/event_bus.dart'; import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_svg/svg.dart'; +import 'package:isar/isar.dart'; +import 'package:stackwallet/models/isar/exchange_cache/currency.dart'; import 'package:stackwallet/notifications/show_flush_bar.dart'; import 'package:stackwallet/pages/buy_view/buy_in_wallet_view.dart'; import 'package:stackwallet/pages/coin_control/coin_control_view.dart'; @@ -33,6 +35,7 @@ import 'package:stackwallet/services/coins/manager.dart'; import 'package:stackwallet/services/event_bus/events/global/node_connection_status_changed_event.dart'; import 'package:stackwallet/services/event_bus/events/global/wallet_sync_status_changed_event.dart'; import 'package:stackwallet/services/event_bus/global_event_bus.dart'; +import 'package:stackwallet/services/exchange/exchange_data_loading_service.dart'; import 'package:stackwallet/services/mixins/paynym_wallet_interface.dart'; import 'package:stackwallet/utilities/assets.dart'; import 'package:stackwallet/utilities/constants.dart'; @@ -255,19 +258,14 @@ class _WalletViewState extends ConsumerState { } void _onExchangePressed(BuildContext context) async { - // too expensive - // unawaited(ExchangeDataLoadingService.instance.loadAll(ref)); - final coin = ref.read(managerProvider).coin; - if (coin == Coin.epicCash) { - await showDialog( - context: context, - builder: (_) => const StackOkDialog( - title: "Exchange not available for Epic Cash", - ), - ); - } else if (coin.name.endsWith("TestNet")) { + final currency = ExchangeDataLoadingService.instance.isar.currencies + .where() + .tickerEqualToAnyExchangeNameName(coin.ticker) + .findFirstSync(); + + if (coin.isTestNet) { await showDialog( context: context, builder: (_) => const StackOkDialog( @@ -275,41 +273,13 @@ class _WalletViewState extends ConsumerState { ), ); } else { - // ref.read(currentExchangeNameStateProvider.state).state = - // ChangeNowExchange.exchangeName; - // final walletId = ref.read(managerProvider).walletId; - // ref.read(prefsChangeNotifierProvider).exchangeRateType = - // ExchangeRateType.estimated; - // - // final currencies = ref - // .read(availableChangeNowCurrenciesProvider) - // .currencies - // .where((element) => - // element.ticker.toLowerCase() == coin.ticker.toLowerCase()); - // - // if (currencies.isNotEmpty) { - // ref - // .read(exchangeFormStateProvider(ExchangeRateType.estimated)) - // .setCurrencies( - // currencies.first, - // ref - // .read(availableChangeNowCurrenciesProvider) - // .currencies - // .firstWhere( - // (element) => - // element.ticker.toLowerCase() != - // coin.ticker.toLowerCase(), - // ), - // ); - // } - if (mounted) { unawaited( Navigator.of(context).pushNamed( WalletInitiatedExchangeView.routeName, arguments: Tuple2( walletId, - coin, + currency == null ? Coin.bitcoin : coin, ), ), ); @@ -317,6 +287,28 @@ class _WalletViewState extends ConsumerState { } } + void _onBuyPressed(BuildContext context) async { + final coin = ref.read(managerProvider).coin; + + if (coin.isTestNet) { + await showDialog( + context: context, + builder: (_) => const StackOkDialog( + title: "Buy not available for test net coins", + ), + ); + } else { + if (mounted) { + unawaited( + Navigator.of(context).pushNamed( + BuyInWalletView.routeName, + arguments: coin.hasBuySupport ? coin : Coin.bitcoin, + ), + ); + } + } + } + Future attemptAnonymize() async { bool shouldPop = false; unawaited( @@ -818,23 +810,20 @@ class _WalletViewState extends ConsumerState { ); }, ), - WalletNavigationBarItemData( - label: "Exchange", - icon: const ExchangeNavIcon(), - onTap: () => _onExchangePressed(context), - ), - WalletNavigationBarItemData( - label: "Buy", - icon: const BuyNavIcon(), - onTap: () { - unawaited( - Navigator.of(context).pushNamed( - BuyInWalletView.routeName, - arguments: coin, - ), - ); - }, - ), + if (Constants.enableExchange) + WalletNavigationBarItemData( + label: "Swap", + icon: const ExchangeNavIcon(), + onTap: () => _onExchangePressed(context), + ), + if (Constants.enableExchange) + WalletNavigationBarItemData( + label: "Buy", + icon: const BuyNavIcon(), + onTap: () => _onBuyPressed(context), + ), + ], + moreItems: [ if (ref.watch( walletsChangeNotifierProvider.select( (value) => value diff --git a/lib/widgets/wallet_navigation_bar/wallet_navigation_bar.dart b/lib/widgets/wallet_navigation_bar/wallet_navigation_bar.dart index 0fca4d4d5..31e796eb9 100644 --- a/lib/widgets/wallet_navigation_bar/wallet_navigation_bar.dart +++ b/lib/widgets/wallet_navigation_bar/wallet_navigation_bar.dart @@ -4,19 +4,20 @@ import 'package:flutter_svg/svg.dart'; import 'package:stackwallet/utilities/assets.dart'; import 'package:stackwallet/utilities/text_styles.dart'; import 'package:stackwallet/utilities/theme/stack_colors.dart'; +import 'package:stackwallet/widgets/conditional_parent.dart'; import 'package:stackwallet/widgets/wallet_navigation_bar/components/wallet_navigation_bar_item.dart'; -const _kMaxItems = 5; - final walletNavBarMore = StateProvider.autoDispose((ref) => false); class WalletNavigationBar extends ConsumerStatefulWidget { const WalletNavigationBar({ Key? key, required this.items, + required this.moreItems, }) : super(key: key); final List items; + final List moreItems; @override ConsumerState createState() => @@ -28,23 +29,18 @@ class _WalletNavigationBarState extends ConsumerState { final _moreDuration = const Duration(milliseconds: 200); - late final bool hasMore; - - double _moreScale = 0; - void _onMorePressed() { ref.read(walletNavBarMore.state).state = !ref.read(walletNavBarMore.state).state; } - @override - void initState() { - hasMore = widget.items.length > _kMaxItems; - super.initState(); - } - @override Widget build(BuildContext context) { + final width = MediaQuery.of(context).size.width - 40; + + final hasMore = widget.moreItems.isNotEmpty; + final buttonCount = widget.items.length + (hasMore ? 1 : 0); + return Stack( alignment: Alignment.bottomCenter, children: [ @@ -93,7 +89,7 @@ class _WalletNavigationBarState extends ConsumerState { crossAxisAlignment: CrossAxisAlignment.stretch, mainAxisSize: MainAxisSize.min, children: [ - ...widget.items.sublist(_kMaxItems - 1).map( + ...widget.moreItems.map( (e) { return Column( children: [ @@ -133,94 +129,92 @@ class _WalletNavigationBarState extends ConsumerState { child: Padding( padding: const EdgeInsets.symmetric( vertical: 6, - horizontal: 12, + horizontal: 20, ), - child: IntrinsicWidth( - child: Row( - mainAxisAlignment: MainAxisAlignment.spaceBetween, - children: [ - const Spacer(), - if (!hasMore) + // child: IntrinsicWidth( + child: ConditionalParent( + condition: buttonCount > 4, + builder: (child) => SizedBox( + width: width * 0.9, + child: child, + ), + child: ConditionalParent( + condition: buttonCount <= 4, + builder: (child) => SizedBox( + width: width * 0.2 * buttonCount, + child: child, + ), + child: Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ ...widget.items.map( - (e) => Flexible( - flex: 10000, + (e) => Expanded( child: WalletNavigationBarItem( data: e, disableDuration: _moreDuration, ), ), ), - if (hasMore) - ...widget.items.sublist(0, _kMaxItems - 1).map( - (e) => Flexible( - flex: 10000, - child: WalletNavigationBarItem( - data: e, - disableDuration: _moreDuration, - ), - ), - ), - if (hasMore) - Flexible( - flex: 10000, - child: WalletNavigationBarItem( - data: WalletNavigationBarItemData( - icon: AnimatedCrossFade( - firstChild: SvgPicture.asset( - Assets.svg.bars, - width: 20, - height: 20, - color: Theme.of(context) - .extension()! - .bottomNavIconIcon, - ), - secondChild: SvgPicture.asset( - Assets.svg.bars, - width: 20, - height: 20, - color: Theme.of(context) - .extension()! - .infoItemIcons, - ), - crossFadeState: ref - .watch(walletNavBarMore.state) - .state - ? CrossFadeState.showSecond - : CrossFadeState.showFirst, - duration: _moreDuration, - ), - overrideText: AnimatedCrossFade( - firstChild: Text( - "More", - style: - STextStyles.buttonSmall(context), - ), - secondChild: Text( - "More", - style: - STextStyles.buttonSmall(context) - .copyWith( + if (hasMore) + Expanded( + child: WalletNavigationBarItem( + data: WalletNavigationBarItemData( + icon: AnimatedCrossFade( + firstChild: SvgPicture.asset( + Assets.svg.bars, + width: 20, + height: 20, + color: Theme.of(context) + .extension()! + .bottomNavIconIcon, + ), + secondChild: SvgPicture.asset( + Assets.svg.bars, + width: 20, + height: 20, color: Theme.of(context) .extension()! .infoItemIcons, ), + crossFadeState: ref + .watch(walletNavBarMore.state) + .state + ? CrossFadeState.showSecond + : CrossFadeState.showFirst, + duration: _moreDuration, ), - crossFadeState: ref - .watch(walletNavBarMore.state) - .state - ? CrossFadeState.showSecond - : CrossFadeState.showFirst, - duration: _moreDuration, + overrideText: AnimatedCrossFade( + firstChild: Text( + "More", + style: STextStyles.buttonSmall( + context), + ), + secondChild: Text( + "More", + style: + STextStyles.buttonSmall(context) + .copyWith( + color: Theme.of(context) + .extension()! + .infoItemIcons, + ), + ), + crossFadeState: ref + .watch(walletNavBarMore.state) + .state + ? CrossFadeState.showSecond + : CrossFadeState.showFirst, + duration: _moreDuration, + ), + label: null, + isMore: true, + onTap: _onMorePressed, ), - label: null, - isMore: true, - onTap: _onMorePressed, + disableDuration: _moreDuration, ), - disableDuration: _moreDuration, ), - ), - const Spacer(), - ], + ], + ), ), ), ),