Skip to content

Commit

Permalink
fix: transactions status and time rendering (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
ethan-tbd authored May 19, 2024
1 parent 511cc37 commit 6ba12c1
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 56 deletions.
3 changes: 1 addition & 2 deletions lib/features/home/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,7 @@ class HomePage extends HookConsumerWidget {
List<Exchange> exchanges,
) =>
ListView(
reverse: true,
children: exchanges.map((exchange) {
children: exchanges.reversed.map((exchange) {
final transaction = Transaction.fromExchange(exchange);

return ListTile(
Expand Down
28 changes: 13 additions & 15 deletions lib/features/home/transaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ class Transaction {
final double payoutAmount;
final String payinCurrency;
final String payoutCurrency;
final String status;
final DateTime createdAt;
final TransactionStatus status;
final TransactionType type;

Transaction({
required this.payinAmount,
required this.payoutAmount,
required this.payinCurrency,
required this.payoutCurrency,
required this.createdAt,
required this.status,
required this.createdAt,
required this.type,
});

Expand All @@ -26,32 +26,39 @@ class Transaction {
var payoutAmount = '0';
var payinCurrency = '';
var payoutCurrency = '';
var status = '';
var latestCreatedAt = DateTime.fromMillisecondsSinceEpoch(0);
var status = TransactionStatus.pending;
var type = TransactionType.send;

for (final msg in exchange) {
switch (msg.metadata.kind) {
case MessageKind.rfq:
status = 'Request submitted';
payinAmount = (msg as Rfq).data.payin.amount;
break;
case MessageKind.quote:
status = 'Quote received';
payinAmount = (msg as Quote).data.payin.amount;
payoutAmount = msg.data.payout.amount;
payinCurrency = msg.data.payin.currencyCode;
payoutCurrency = msg.data.payout.currencyCode;
break;
case MessageKind.order:
status = TransactionStatus.completed;
status = 'Order submitted';
break;
// TODO(ethan-tbd): add additional order statuses
case MessageKind.orderstatus:
status = TransactionStatus.completed;
status = (msg as OrderStatus).data.orderStatus;
break;
case MessageKind.close:
status = TransactionStatus.failed;
status = 'Canceled';
break;
}

final createdAt = DateTime.parse(msg.metadata.createdAt);
if (createdAt.isAfter(latestCreatedAt)) {
latestCreatedAt = createdAt;
}
}

type = (payinCurrency == 'STORED_BALANCE')
Expand Down Expand Up @@ -83,15 +90,6 @@ class Transaction {
}
}

enum TransactionStatus {
failed,
pending,
completed;

@override
String toString() => name.substring(0, 1).toUpperCase() + name.substring(1);
}

enum TransactionType {
deposit,
withdraw,
Expand Down
42 changes: 16 additions & 26 deletions lib/features/home/transaction_details_page.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import 'package:auto_size_text/auto_size_text.dart';
import 'package:didpay/features/home/transaction.dart';
import 'package:didpay/l10n/app_localizations.dart';
import 'package:didpay/shared/theme/grid.dart';
import 'package:didpay/shared/utils/currency_util.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:intl/intl.dart';

class TransactionDetailsPage extends HookWidget {
final Transaction txn;
Expand Down Expand Up @@ -132,7 +132,7 @@ class TransactionDetailsPage extends HookWidget {
),
const SizedBox(height: Grid.xxs),
Text(
'Mar 1 at 10:00 am',
DateFormat("MMM dd 'at' hh:mm a").format(txn.createdAt.toLocal()),
style: Theme.of(context).textTheme.titleMedium?.copyWith(
color: Theme.of(context).colorScheme.onSurface,
),
Expand All @@ -151,33 +151,23 @@ class TransactionDetailsPage extends HookWidget {
),
splashFactory: NoSplash.splashFactory,
),
child: _getStatusText(context, txn.status),
child: _formatTransactionStatus(context, txn.status),
),
);

Text _getStatusText(BuildContext context, TransactionStatus status) {
switch (status) {
case TransactionStatus.pending:
return Text(
Loc.of(context).pending,
style: Theme.of(context).textTheme.titleMedium?.copyWith(
color: Theme.of(context).colorScheme.onSurface,
),
);
case TransactionStatus.failed:
return Text(
Loc.of(context).failed,
style: Theme.of(context).textTheme.titleMedium?.copyWith(
color: Theme.of(context).colorScheme.error,
),
);
case TransactionStatus.completed:
return Text(
Loc.of(context).completed,
style: Theme.of(context).textTheme.titleMedium?.copyWith(
color: Theme.of(context).colorScheme.tertiary,
),
);
Text _formatTransactionStatus(BuildContext context, String status) {
var formattedStatus = status.replaceAll('_', ' ').toLowerCase();

if (formattedStatus.isNotEmpty) {
formattedStatus =
formattedStatus[0].toUpperCase() + formattedStatus.substring(1);
}

return Text(
formattedStatus,
style: Theme.of(context).textTheme.titleMedium?.copyWith(
color: Theme.of(context).colorScheme.tertiary,
),
);
}
}
11 changes: 7 additions & 4 deletions lib/features/payment/review_payment_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,13 @@ class ReviewPaymentPage extends HookConsumerWidget {
),
error: (error, _) => AsyncErrorWidget(
text: error.toString(),
onRetry: () => ref.read(quoteProvider.notifier).startPolling(
exchangeId,
paymentState.pfi,
),
onRetry: () => submitOrder(
context,
ref,
paymentState,
exchangeId,
sendOrderState,
),
),
)
: getQuoteState.when(
Expand Down
4 changes: 1 addition & 3 deletions lib/features/payout/payout.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ class Payout extends HookWidget {
WidgetsBinding.instance.addPostFrameCallback((_) {
if (selectedOffering.value == null) return;

payoutAmount.value = transactionType == TransactionType.deposit
? payinAmount / exchangeRate
: payinAmount * exchangeRate;
payoutAmount.value = payinAmount * exchangeRate;
});

return;
Expand Down
2 changes: 2 additions & 0 deletions lib/features/tbdex/tbdex_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class TbdexService {
),
);
await rfq.sign(did);
await Future.delayed(const Duration(seconds: 1));

final response =
await TbdexHttpClient.createExchange(rfq, replyTo: rfq.metadata.from);
Expand All @@ -93,6 +94,7 @@ class TbdexService {
Future<Order> submitOrder(BearerDid did, Pfi pfi, String exchangeId) async {
final order = Order.create(pfi.did, did.uri, exchangeId);
await order.sign(did);
await Future.delayed(const Duration(seconds: 1));

final response = await TbdexHttpClient.submitOrder(order);

Expand Down
12 changes: 6 additions & 6 deletions test/features/home/transaction_details_page_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ void main() {
payoutAmount: 0,
payinCurrency: 'USDC',
payoutCurrency: 'USDC',
status: '',
createdAt: DateTime.now(),
status: TransactionStatus.completed,
type: TransactionType.deposit,
),
),
Expand All @@ -36,8 +36,8 @@ void main() {
payoutAmount: 0,
payinCurrency: 'USDC',
payoutCurrency: 'USDC',
status: '',
createdAt: DateTime.now(),
status: TransactionStatus.completed,
type: TransactionType.withdraw,
),
),
Expand All @@ -56,8 +56,8 @@ void main() {
payoutAmount: 123,
payinCurrency: 'USDC',
payoutCurrency: 'MXN',
status: '',
createdAt: DateTime.now(),
status: TransactionStatus.completed,
type: TransactionType.deposit,
),
),
Expand All @@ -79,8 +79,8 @@ void main() {
payoutAmount: 123,
payinCurrency: 'USDC',
payoutCurrency: 'USDC',
status: 'Pending',
createdAt: DateTime.now(),
status: TransactionStatus.pending,
type: TransactionType.deposit,
),
),
Expand All @@ -99,8 +99,8 @@ void main() {
payoutAmount: 123,
payinCurrency: 'USDC',
payoutCurrency: 'USDC',
status: 'Completed',
createdAt: DateTime.now(),
status: TransactionStatus.completed,
type: TransactionType.deposit,
),
),
Expand All @@ -119,8 +119,8 @@ void main() {
payoutAmount: 123,
payinCurrency: 'USDC',
payoutCurrency: 'USDC',
status: 'Failed',
createdAt: DateTime.now(),
status: TransactionStatus.failed,
type: TransactionType.deposit,
),
),
Expand Down

0 comments on commit 6ba12c1

Please sign in to comment.