diff --git a/lib/features/account/account_balance_card.dart b/lib/features/account/account_balance_card.dart new file mode 100644 index 00000000..517fa593 --- /dev/null +++ b/lib/features/account/account_balance_card.dart @@ -0,0 +1,132 @@ +import 'package:auto_size_text/auto_size_text.dart'; +import 'package:didpay/features/account/account_balance_notifier.dart'; +import 'package:didpay/features/payment/payment_amount_page.dart'; +import 'package:didpay/features/pfis/pfis_notifier.dart'; +import 'package:didpay/features/transaction/transaction.dart'; +import 'package:didpay/l10n/app_localizations.dart'; +import 'package:didpay/shared/theme/grid.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_hooks/flutter_hooks.dart'; +import 'package:hooks_riverpod/hooks_riverpod.dart'; + +class AccountBalanceCard extends HookConsumerWidget { + const AccountBalanceCard({super.key}); + + @override + Widget build(BuildContext context, WidgetRef ref) { + final pfis = ref.watch(pfisProvider); + final accountBalance = ref.watch(accountBalanceProvider(pfis)); + + final accountTotal = accountBalance.asData?.value?.total ?? ''; + final accountCurrency = accountBalance.asData?.value?.currencyCode ?? ''; + + AccountBalanceNotifier getAccountBalanceNotifier() => + ref.read(accountBalanceProvider(pfis).notifier); + + useEffect( + () { + Future.microtask( + () async => getAccountBalanceNotifier().startPolling(), + ); + return getAccountBalanceNotifier().stopPolling; + }, + [], + ); + + return Padding( + padding: const EdgeInsets.symmetric( + vertical: Grid.xs, + horizontal: Grid.side, + ), + child: Container( + decoration: BoxDecoration( + color: Theme.of(context).colorScheme.surface, + borderRadius: BorderRadius.circular(Grid.radius), + ), + padding: const EdgeInsets.all(Grid.xs), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + _buildCardTitle(context), + const SizedBox(height: Grid.xxs), + _buildAccountBalance(context, accountTotal, accountCurrency), + const SizedBox(height: Grid.xs), + if (pfis.isNotEmpty) _buildDepositWithdrawButtons(context), + ], + ), + ), + ); + } + + Widget _buildCardTitle(BuildContext context) => Text( + Loc.of(context).accountBalance, + style: Theme.of(context).textTheme.labelMedium?.copyWith( + color: Theme.of(context).colorScheme.onSurface, + ), + ); + + Widget _buildAccountBalance( + BuildContext context, + String accountTotal, + String accountCurrency, + ) => + Center( + child: Row( + mainAxisAlignment: MainAxisAlignment.center, + crossAxisAlignment: CrossAxisAlignment.baseline, + textBaseline: TextBaseline.alphabetic, + children: [ + Flexible( + child: AutoSizeText( + accountTotal, + style: Theme.of(context).textTheme.displayMedium?.copyWith( + fontWeight: FontWeight.bold, + ), + maxLines: 1, + ), + ), + const SizedBox(width: Grid.half), + Padding( + padding: const EdgeInsets.symmetric(horizontal: Grid.xxs), + child: Text( + accountCurrency, + style: Theme.of(context).textTheme.headlineMedium?.copyWith( + fontWeight: FontWeight.bold, + ), + ), + ), + ], + ), + ); + + Widget _buildDepositWithdrawButtons(BuildContext context) => Row( + mainAxisAlignment: MainAxisAlignment.spaceEvenly, + children: [ + Expanded( + child: FilledButton( + onPressed: () => Navigator.of(context).push( + MaterialPageRoute( + builder: (context) => const PaymentAmountPage( + transactionType: TransactionType.deposit, + ), + ), + ), + child: Text(Loc.of(context).deposit), + ), + ), + const SizedBox(width: Grid.xs), + Expanded( + child: FilledButton( + onPressed: () => Navigator.of(context).push( + MaterialPageRoute( + builder: (context) => const PaymentAmountPage( + transactionType: TransactionType.withdraw, + ), + ), + ), + child: Text(Loc.of(context).withdraw), + ), + ), + ], + ); +} diff --git a/lib/features/account/account_balance_display.dart b/lib/features/account/account_balance_display.dart deleted file mode 100644 index 8cdfff23..00000000 --- a/lib/features/account/account_balance_display.dart +++ /dev/null @@ -1,60 +0,0 @@ -import 'package:auto_size_text/auto_size_text.dart'; -import 'package:didpay/features/account/account_balance_notifier.dart'; -import 'package:didpay/features/pfis/pfis_notifier.dart'; -import 'package:didpay/shared/theme/grid.dart'; -import 'package:flutter/material.dart'; -import 'package:flutter_hooks/flutter_hooks.dart'; -import 'package:hooks_riverpod/hooks_riverpod.dart'; - -class AccountBalanceDisplay extends HookConsumerWidget { - const AccountBalanceDisplay({super.key}); - - @override - Widget build(BuildContext context, WidgetRef ref) { - final pfis = ref.watch(pfisProvider); - final accountBalance = ref.watch(accountBalanceProvider(pfis)); - - final accountTotal = accountBalance.asData?.value?.total ?? ''; - final accountCurrency = accountBalance.asData?.value?.currencyCode ?? ''; - - AccountBalanceNotifier getAccountBalanceNotifier() => - ref.read(accountBalanceProvider(pfis).notifier); - - useEffect( - () { - Future.microtask( - () async => getAccountBalanceNotifier().startPolling(), - ); - return getAccountBalanceNotifier().stopPolling; - }, - [], - ); - - return Row( - mainAxisAlignment: MainAxisAlignment.center, - crossAxisAlignment: CrossAxisAlignment.baseline, - textBaseline: TextBaseline.alphabetic, - children: [ - Flexible( - child: AutoSizeText( - accountTotal, - style: Theme.of(context).textTheme.displayMedium?.copyWith( - fontWeight: FontWeight.bold, - ), - maxLines: 1, - ), - ), - const SizedBox(width: Grid.half), - Padding( - padding: const EdgeInsets.symmetric(horizontal: Grid.xxs), - child: Text( - accountCurrency, - style: Theme.of(context).textTheme.headlineMedium?.copyWith( - fontWeight: FontWeight.bold, - ), - ), - ), - ], - ); - } -} diff --git a/lib/features/account/account_page.dart b/lib/features/account/account_page.dart index ad927fae..c9c169d9 100644 --- a/lib/features/account/account_page.dart +++ b/lib/features/account/account_page.dart @@ -193,13 +193,13 @@ class AccountPage extends HookConsumerWidget { itemCount: credentials.length, itemBuilder: (context, index) => TileContainer( child: - _buildCredentialsTile(context, ref, credentials[index]), + _buildCredentialTile(context, ref, credentials[index]), ), ), ], ); - Widget _buildCredentialsTile( + Widget _buildCredentialTile( BuildContext context, WidgetRef ref, String credential, diff --git a/lib/features/home/home_page.dart b/lib/features/home/home_page.dart index edbaf9ec..160bcb47 100644 --- a/lib/features/home/home_page.dart +++ b/lib/features/home/home_page.dart @@ -1,4 +1,4 @@ -import 'package:didpay/features/account/account_balance_display.dart'; +import 'package:didpay/features/account/account_balance_card.dart'; import 'package:didpay/features/did/did_provider.dart'; import 'package:didpay/features/payment/payment_amount_page.dart'; import 'package:didpay/features/pfis/pfi.dart'; @@ -36,7 +36,7 @@ class HomePage extends HookConsumerWidget { child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ - _buildBalanceActions(context, pfis), + const AccountBalanceCard(), Expanded( child: pfis.isEmpty ? _buildGetStarted( @@ -57,74 +57,6 @@ class HomePage extends HookConsumerWidget { ); } - Widget _buildBalanceActions(BuildContext context, List pfis) => Padding( - padding: const EdgeInsets.symmetric( - vertical: Grid.xs, - horizontal: Grid.side, - ), - child: Container( - decoration: BoxDecoration( - color: Theme.of(context).colorScheme.surface, - borderRadius: BorderRadius.circular(Grid.radius), - ), - padding: const EdgeInsets.all(Grid.xs), - child: Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Text( - Loc.of(context).accountBalance, - style: Theme.of(context).textTheme.labelMedium?.copyWith( - color: Theme.of(context).colorScheme.onSurface, - ), - ), - const SizedBox(height: Grid.xxs), - const Center(child: AccountBalanceDisplay()), - const SizedBox(height: Grid.xs), - if (pfis.isNotEmpty) _buildDepositWithdrawButtons(context, pfis), - ], - ), - ), - ); - - Widget _buildDepositWithdrawButtons( - BuildContext context, - List pfis, - ) => - Row( - mainAxisAlignment: MainAxisAlignment.spaceEvenly, - children: [ - Expanded( - child: FilledButton( - onPressed: () { - Navigator.of(context).push( - MaterialPageRoute( - builder: (context) => const PaymentAmountPage( - transactionType: TransactionType.deposit, - ), - ), - ); - }, - child: Text(Loc.of(context).deposit), - ), - ), - const SizedBox(width: Grid.xs), - Expanded( - child: FilledButton( - onPressed: () { - Navigator.of(context).push( - MaterialPageRoute( - builder: (context) => const PaymentAmountPage( - transactionType: TransactionType.withdraw, - ), - ), - ); - }, - child: Text(Loc.of(context).withdraw), - ), - ), - ], - ); - Widget _buildTransactionsList( BuildContext context, WidgetRef ref, diff --git a/test/features/app/app_tabs_test.dart b/test/features/app/app_tabs_test.dart index 141e21c8..f947b6d3 100644 --- a/test/features/app/app_tabs_test.dart +++ b/test/features/app/app_tabs_test.dart @@ -1,4 +1,5 @@ import 'package:didpay/features/account/account_balance.dart'; +import 'package:didpay/features/account/account_balance_notifier.dart'; import 'package:didpay/features/account/account_page.dart'; import 'package:didpay/features/app/app_tabs.dart'; import 'package:didpay/features/did/did_provider.dart'; @@ -19,6 +20,8 @@ import '../../helpers/widget_helpers.dart'; void main() async { final did = await DidDht.create(); + final accountBalance = + AccountBalance(total: '101', currencyCode: 'USD', balancesMap: {}); const pfi = Pfi(did: 'did:web:x%3A8892:ingress'); late MockTbdexService mockTbdexService; @@ -51,6 +54,8 @@ void main() async { pfisProvider.overrideWith((ref) => mockPfisNotifier), vcsProvider.overrideWith((ref) => mockVcsNotifier), transactionProvider.overrideWith(MockTransactionNotifier.new), + accountBalanceProvider + .overrideWith(() => MockAccountBalanceNotifier(accountBalance)), ], ); diff --git a/test/features/home/home_page_test.dart b/test/features/home/home_page_test.dart index 61409d42..8772e92a 100644 --- a/test/features/home/home_page_test.dart +++ b/test/features/home/home_page_test.dart @@ -1,7 +1,7 @@ import 'dart:convert'; import 'package:didpay/features/account/account_balance.dart'; -import 'package:didpay/features/account/account_balance_display.dart'; +import 'package:didpay/features/account/account_balance_card.dart'; import 'package:didpay/features/account/account_balance_notifier.dart'; import 'package:didpay/features/did/did_provider.dart'; import 'package:didpay/features/home/home_page.dart'; @@ -79,8 +79,8 @@ void main() async { await tester.pumpWidget(homePageTestWidget()); await tester.pumpAndSettle(); - expect(find.widgetWithText(AccountBalanceDisplay, '101'), findsOneWidget); - expect(find.widgetWithText(AccountBalanceDisplay, 'USD'), findsOneWidget); + expect(find.widgetWithText(AccountBalanceCard, '101'), findsOneWidget); + expect(find.widgetWithText(AccountBalanceCard, 'USD'), findsOneWidget); }); testWidgets('should show deposit button', (tester) async {