Skip to content

Commit d0b1026

Browse files
piatoss3612Thunnini
authored andcommitted
Migrate amount displays to ZecAmount
1 parent 115d32e commit d0b1026

9 files changed

Lines changed: 99 additions & 48 deletions

File tree

lib/src/core/formatting/zec_amount.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class ZecAmount {
115115
signed ? zatoshi : abs,
116116
minFractionDigits: showFullFraction ? 0 : 2,
117117
maxFractionDigits: showFullFraction ? 8 : 2,
118-
denomStyle: ZecDenomStyle.lower,
118+
denomStyle: ZecDenomStyle.upper,
119119
signed: signed,
120120
trimTrailingZeros: showFullFraction,
121121
);

lib/src/features/activity/activity_row_mapper.dart

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'package:flutter/widgets.dart';
22

3+
import '../../core/formatting/zec_amount.dart';
34
import '../../core/theme/app_theme.dart';
45
import '../../core/widgets/app_icon.dart';
56
import '../../providers/sync_provider.dart';
@@ -73,7 +74,7 @@ ActivityRowData buildSyncActivityRow({
7374
leadingIconName: AppIcons.sync,
7475
leadingBackgroundColor: colors.background.neutralSubtleOpacity,
7576
leadingIconColor: colors.icon.regular,
76-
amountText: formatActivityZec(sync.totalBalance),
77+
amountText: ZecAmount.fromZatoshi(sync.totalBalance).activity.toString(),
7778
amountColor: colors.text.accent,
7879
statusText: 'Completed',
7980
statusColor: colors.text.secondary,
@@ -155,24 +156,9 @@ String _transactionAmountText({
155156
}) {
156157
if (amount == BigInt.zero) return '--';
157158
if (isFailed || isShielded) {
158-
return formatActivityZec(amount);
159+
return ZecAmount.fromZatoshi(amount).activity.toString();
159160
}
160-
return formatSignedActivityZec(signedAmount);
161-
}
162-
163-
String formatActivityZec(BigInt zatoshi) {
164-
final abs = zatoshi.abs();
165-
final whole = abs ~/ BigInt.from(100000000);
166-
final frac = (abs % BigInt.from(100000000)).toString().padLeft(8, '0');
167-
final digits = whole == BigInt.zero && int.parse(frac) < 1000000
168-
? frac
169-
: frac.substring(0, 2);
170-
return '$whole.$digits ZEC';
171-
}
172-
173-
String formatSignedActivityZec(BigInt zatoshi) {
174-
final sign = zatoshi >= BigInt.zero ? '+' : '-';
175-
return '$sign${formatActivityZec(zatoshi)}';
161+
return ZecAmount.fromZatoshi(signedAmount).signedActivity.toString();
176162
}
177163

178164
String formatActivityTimestamp(DateTime? timestamp) {

lib/src/features/activity/screens/activity_transaction_status_screen.dart

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
77
import 'package:go_router/go_router.dart';
88

99
import '../../../../main.dart' show log;
10+
import '../../../core/formatting/zec_amount.dart';
1011
import '../../../core/layout/app_desktop_shell.dart';
1112
import '../../../core/layout/app_layout.dart';
1213
import '../../../core/layout/app_main_sidebar.dart';
@@ -17,7 +18,6 @@ import '../../../providers/rpc_endpoint_provider.dart';
1718
import '../../../providers/sync_provider.dart';
1819
import '../../../rust/api/sync.dart' as rust_sync;
1920
import '../../send/widgets/transaction_receipt_view.dart';
20-
import '../activity_row_mapper.dart';
2121

2222
class ActivityTransactionStatusArgs {
2323
const ActivityTransactionStatusArgs({
@@ -237,7 +237,7 @@ class _ActivityTransactionStatusScreenState
237237
String _amountText(rust_sync.TransactionInfo? tx) {
238238
if (tx == null) return '--';
239239
if (tx.displayAmount == BigInt.zero) return '--';
240-
return formatActivityZec(tx.displayAmount);
240+
return ZecAmount.fromZatoshi(tx.displayAmount).activity.toString();
241241
}
242242

243243
String _dateText(rust_sync.TransactionInfo? tx) {
@@ -251,17 +251,7 @@ class _ActivityTransactionStatusScreenState
251251

252252
String _feeText(rust_sync.TransactionInfo? tx) {
253253
if (tx == null || tx.fee <= BigInt.zero) return '--';
254-
return '${_formatZec(tx.fee)} ZEC';
255-
}
256-
257-
String _formatZec(BigInt zatoshi) {
258-
final whole = zatoshi ~/ BigInt.from(100000000);
259-
var fraction = (zatoshi % BigInt.from(100000000)).toString().padLeft(
260-
8,
261-
'0',
262-
);
263-
fraction = fraction.replaceFirst(RegExp(r'0+$'), '');
264-
return fraction.isEmpty ? '$whole' : '$whole.$fraction';
254+
return ZecAmount.fromZatoshi(tx.fee).fee.toString();
265255
}
266256

267257
String _formatDate(DateTime value) {

lib/src/features/home/screens/home_screen.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class _HomeScreenState extends ConsumerState<HomeScreen> {
6161
}
6262

6363
String _formatZec(BigInt zatoshi) {
64-
return formatZecAmount(zatoshi, minFractionDigits: 2);
64+
return ZecAmount.fromZatoshi(zatoshi).balance.amountText;
6565
}
6666

6767
void _toggleBalanceVisibility() {

lib/src/features/send/screens/send_review_screen.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ class _SendReviewScreenState extends ConsumerState<SendReviewScreen> {
8989
}
9090

9191
String _formatReceiptAmount(BigInt zatoshi) {
92-
return '${formatZecAmount(zatoshi, minFractionDigits: 2)} zec';
92+
return ZecAmount.fromZatoshi(zatoshi).receipt.toString();
9393
}
9494

9595
String _formatFee(BigInt zatoshi) {
96-
return formatZecAmount(zatoshi);
96+
return ZecAmount.fromZatoshi(zatoshi).pretty().amountText;
9797
}
9898

9999
List<TextSpan> _addressSpans(BuildContext context, String line) {

lib/src/features/send/screens/send_screen.dart

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,9 @@ class _SendComposeBodyState extends ConsumerState<_SendComposeBody> {
356356
return;
357357
}
358358

359-
final amountText = formatZecAmount(estimate.amountZatoshi);
359+
final amountText = ZecAmount.fromZatoshi(
360+
estimate.amountZatoshi,
361+
).pretty().amountText;
360362
_programmaticAmountEdit = true;
361363
_amountController.value = TextEditingValue(
362364
text: amountText,
@@ -448,10 +450,9 @@ class _SendComposeBodyState extends ConsumerState<_SendComposeBody> {
448450

449451
final totalNeeded = zatoshi + fee;
450452
if (totalNeeded > spendable) {
451-
final feeZec = formatZecAmount(fee);
453+
final feeText = ZecAmount.fromZatoshi(fee).fee.toString();
452454
setState(
453-
() =>
454-
_amountError = 'Insufficient shielded balance (fee: $feeZec ZEC)',
455+
() => _amountError = 'Insufficient shielded balance (fee: $feeText)',
455456
);
456457
} else {
457458
setState(() => _amountError = null);
@@ -624,6 +625,9 @@ class _SendComposeBodyState extends ConsumerState<_SendComposeBody> {
624625
@override
625626
Widget build(BuildContext context) {
626627
final spendable = widget.spendableBalance;
628+
final spendableText = ZecAmount.fromZatoshi(
629+
spendable,
630+
).pretty(denomStyle: ZecDenomStyle.upper).toString();
627631
final colors = context.colors;
628632

629633
final addressTone = switch (_addressType) {
@@ -791,7 +795,7 @@ class _SendComposeBodyState extends ConsumerState<_SendComposeBody> {
791795
? null
792796
: _activateMaxMode,
793797
child: Text(
794-
'Max: ${formatZecAmount(spendable)} ZEC',
798+
'Max: $spendableText',
795799
style: AppTypography
796800
.labelMedium
797801
.copyWith(

lib/src/features/send/screens/send_status_screen.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,11 @@ class _SendStatusScreenState extends ConsumerState<SendStatusScreen> {
121121
}
122122

123123
String _formatReceiptAmount(BigInt zatoshi) {
124-
return '${formatZecAmount(zatoshi, minFractionDigits: 2)} zec';
124+
return ZecAmount.fromZatoshi(zatoshi).receipt.toString();
125125
}
126126

127127
String _formatFee(BigInt zatoshi) {
128-
return formatZecAmount(zatoshi);
128+
return ZecAmount.fromZatoshi(zatoshi).pretty().amountText;
129129
}
130130

131131
String _formatDate(DateTime value) {

test/activity_table_row_test.dart

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ import 'package:flutter/widgets.dart';
44
import 'package:flutter_test/flutter_test.dart';
55
import 'package:zcash_wallet/src/core/theme/app_theme.dart';
66
import 'package:zcash_wallet/src/core/widgets/app_icon.dart';
7+
import 'package:zcash_wallet/src/features/activity/activity_row_mapper.dart';
78
import 'package:zcash_wallet/src/features/activity/models/activity_row_data.dart';
89
import 'package:zcash_wallet/src/features/activity/widgets/activity_table.dart';
10+
import 'package:zcash_wallet/src/providers/sync_provider.dart';
11+
import 'package:zcash_wallet/src/rust/api/sync.dart' as rust_sync;
912

1013
void main() {
1114
testWidgets('transaction rows are keyboard activatable but sync row is not', (
@@ -47,6 +50,74 @@ void main() {
4750
await tester.pump();
4851
expect(txActivations, 2);
4952
});
53+
54+
testWidgets('activity rows render manipulated zatoshi values', (
55+
tester,
56+
) async {
57+
late final List<ActivityRowData> rows;
58+
59+
await tester.pumpWidget(
60+
MaterialApp(
61+
home: AppTheme(
62+
data: AppThemeData.light,
63+
child: Builder(
64+
builder: (context) {
65+
rows = buildActivityRows(
66+
context: context,
67+
sync: SyncState(totalBalance: BigInt.from(10000)),
68+
transactions: [
69+
_tx(
70+
txidHex: 'received',
71+
kind: 'received',
72+
amount: BigInt.from(123450000),
73+
),
74+
_tx(
75+
txidHex: 'sent',
76+
kind: 'sent',
77+
amount: BigInt.from(100000000),
78+
),
79+
_tx(
80+
txidHex: 'shielded',
81+
kind: 'shielded',
82+
amount: BigInt.from(10000),
83+
),
84+
],
85+
);
86+
return ActivityTable(rows: rows);
87+
},
88+
),
89+
),
90+
),
91+
);
92+
93+
expect(rows[0].amountText, '0.0001 ZEC');
94+
expect(rows[1].amountText, '+1.23 ZEC');
95+
expect(rows[2].amountText, '-1.00 ZEC');
96+
expect(rows[3].amountText, '0.0001 ZEC');
97+
expect(find.text('0.0001 ZEC'), findsNWidgets(2));
98+
expect(find.text('+1.23 ZEC'), findsOneWidget);
99+
expect(find.text('-1.00 ZEC'), findsOneWidget);
100+
});
101+
}
102+
103+
rust_sync.TransactionInfo _tx({
104+
required String txidHex,
105+
required String kind,
106+
required BigInt amount,
107+
}) {
108+
return rust_sync.TransactionInfo(
109+
txidHex: txidHex,
110+
minedHeight: BigInt.one,
111+
expiredUnmined: false,
112+
accountBalanceDelta: 0,
113+
fee: BigInt.zero,
114+
blockTime: BigInt.from(1800000000),
115+
isTransparent: false,
116+
txKind: kind,
117+
displayAmount: amount,
118+
displayPool: 'shielded',
119+
createdTime: BigInt.from(1800000000),
120+
);
50121
}
51122

52123
ActivityRowData _row({

test/zec_amount_test.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,29 +63,29 @@ void main() {
6363
test('preserves existing activity precision rules', () {
6464
expect(
6565
ZecAmount.fromZatoshi(BigInt.from(123450000)).activity.toString(),
66-
'1.23 zec',
66+
'1.23 ZEC',
6767
);
6868
expect(
6969
ZecAmount.fromZatoshi(BigInt.from(10000)).activity.toString(),
70-
'0.0001 zec',
70+
'0.0001 ZEC',
7171
);
7272
expect(
7373
ZecAmount.fromZatoshi(BigInt.zero).activity.toString(),
74-
'0.00 zec',
74+
'0.00 ZEC',
7575
);
7676
expect(
7777
ZecAmount.fromZatoshi(-BigInt.from(100000000)).activity.toString(),
78-
'1.00 zec',
78+
'1.00 ZEC',
7979
);
8080
expect(
8181
ZecAmount.fromZatoshi(
8282
-BigInt.from(100000000),
8383
).signedActivity.toString(),
84-
'-1.00 zec',
84+
'-1.00 ZEC',
8585
);
8686
expect(
8787
ZecAmount.fromZatoshi(BigInt.zero).signedActivity.toString(),
88-
'0.00 zec',
88+
'0.00 ZEC',
8989
);
9090
});
9191
});

0 commit comments

Comments
 (0)