Skip to content

Commit

Permalink
feat: add account balance card (#202)
Browse files Browse the repository at this point in the history
  • Loading branch information
ethan-tbd authored Jun 12, 2024
1 parent db4a4c8 commit 22e2c1c
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 135 deletions.
132 changes: 132 additions & 0 deletions lib/features/account/account_balance_card.dart
Original file line number Diff line number Diff line change
@@ -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),
),
),
],
);
}
60 changes: 0 additions & 60 deletions lib/features/account/account_balance_display.dart

This file was deleted.

4 changes: 2 additions & 2 deletions lib/features/account/account_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
72 changes: 2 additions & 70 deletions lib/features/home/home_page.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -36,7 +36,7 @@ class HomePage extends HookConsumerWidget {
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
_buildBalanceActions(context, pfis),
const AccountBalanceCard(),
Expanded(
child: pfis.isEmpty
? _buildGetStarted(
Expand All @@ -57,74 +57,6 @@ class HomePage extends HookConsumerWidget {
);
}

Widget _buildBalanceActions(BuildContext context, List<Pfi> 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<Pfi> 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,
Expand Down
5 changes: 5 additions & 0 deletions test/features/app/app_tabs_test.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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;
Expand Down Expand Up @@ -51,6 +54,8 @@ void main() async {
pfisProvider.overrideWith((ref) => mockPfisNotifier),
vcsProvider.overrideWith((ref) => mockVcsNotifier),
transactionProvider.overrideWith(MockTransactionNotifier.new),
accountBalanceProvider
.overrideWith(() => MockAccountBalanceNotifier(accountBalance)),
],
);

Expand Down
6 changes: 3 additions & 3 deletions test/features/home/home_page_test.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 22e2c1c

Please sign in to comment.