all transactions sort groups properly

This commit is contained in:
julian 2023-10-23 13:29:30 -06:00
parent bd634e5564
commit 0396b7865f

View file

@ -47,6 +47,12 @@ import 'package:stackwallet/widgets/textfield_icon_button.dart';
import 'package:stackwallet/widgets/transaction_card.dart'; import 'package:stackwallet/widgets/transaction_card.dart';
import 'package:tuple/tuple.dart'; import 'package:tuple/tuple.dart';
typedef _GroupedTransactions = ({
String label,
DateTime startDate,
List<Transaction> transactions
});
class AllTransactionsView extends ConsumerStatefulWidget { class AllTransactionsView extends ConsumerStatefulWidget {
const AllTransactionsView({ const AllTransactionsView({
Key? key, Key? key,
@ -192,25 +198,24 @@ class _TransactionDetailsViewState extends ConsumerState<AllTransactionsView> {
.toList(); .toList();
} }
List<Tuple2<String, List<Transaction>>> groupTransactionsByMonth( List<_GroupedTransactions> groupTransactionsByMonth(
List<Transaction> transactions) { List<Transaction> transactions,
Map<String, List<Transaction>> map = {}; ) {
Map<String, _GroupedTransactions> map = {};
for (var tx in transactions) { for (var tx in transactions) {
final date = DateTime.fromMillisecondsSinceEpoch(tx.timestamp * 1000); final date = DateTime.fromMillisecondsSinceEpoch(tx.timestamp * 1000);
final monthYear = "${Constants.monthMap[date.month]} ${date.year}"; final monthYear = "${Constants.monthMap[date.month]} ${date.year}";
if (map[monthYear] == null) { if (map[monthYear] == null) {
map[monthYear] = []; map[monthYear] =
(label: monthYear, startDate: date, transactions: [tx]);
} else {
map[monthYear]!.transactions.add(tx);
} }
map[monthYear]!.add(tx);
} }
List<Tuple2<String, List<Transaction>>> result = []; return map.values.toList()
map.forEach((key, value) { ..sort((a, b) => b.startDate.compareTo(a.startDate));
result.add(Tuple2(key, value));
});
return result;
} }
@override @override
@ -504,7 +509,7 @@ class _TransactionDetailsViewState extends ConsumerState<AllTransactionsView> {
height: 12, height: 12,
), ),
Text( Text(
month.item1, month.label,
style: STextStyles.smallMed12(context), style: STextStyles.smallMed12(context),
), ),
const SizedBox( const SizedBox(
@ -523,14 +528,15 @@ class _TransactionDetailsViewState extends ConsumerState<AllTransactionsView> {
.extension<StackColors>()! .extension<StackColors>()!
.background, .background,
), ),
itemCount: month.item2.length, itemCount: month.transactions.length,
itemBuilder: (context, index) => itemBuilder: (context, index) =>
Padding( Padding(
padding: const EdgeInsets.all(4), padding: const EdgeInsets.all(4),
child: DesktopTransactionCardRow( child: DesktopTransactionCardRow(
key: Key( key: Key(
"transactionCard_key_${month.item2[index].txid}"), "transactionCard_key_${month.transactions[index].txid}"),
transaction: month.item2[index], transaction:
month.transactions[index],
walletId: walletId, walletId: walletId,
), ),
), ),
@ -541,7 +547,7 @@ class _TransactionDetailsViewState extends ConsumerState<AllTransactionsView> {
padding: const EdgeInsets.all(0), padding: const EdgeInsets.all(0),
child: Column( child: Column(
children: [ children: [
...month.item2.map( ...month.transactions.map(
(tx) => TransactionCard( (tx) => TransactionCard(
key: Key( key: Key(
"transactionCard_key_${tx.txid}"), "transactionCard_key_${tx.txid}"),